-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from tungbq/issue-163
[Issue-163] Module 4 - Create a Data Table
- Loading branch information
Showing
5 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
33 changes: 29 additions & 4 deletions
33
tutorial/serverless-basic-application/assets/lamda_function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,36 @@ | ||
# import the JSON utility package since we will be working with a JSON object | ||
# import the json utility package since we will be working with a JSON object | ||
import json | ||
# import the AWS SDK (for Python the package name is boto3) | ||
import boto3 | ||
# import time | ||
import time | ||
# import two packages to help us with dates and date formatting | ||
|
||
# create a DynamoDB object using the AWS SDK | ||
dynamodb = boto3.resource('dynamodb') | ||
# use the DynamoDB object to select our table | ||
table = dynamodb.Table('DebugTableHelloWorld') # TODO, use this from env variable to align with the terraform instead of harccoding here | ||
|
||
# define the handler function that the Lambda service will use as an entry point | ||
def lambda_handler(event, context): | ||
# extract values from the event object we got from the Lambda service | ||
# Get the current GMT time | ||
gmt_time = time.gmtime() | ||
|
||
# store the current time in a human readable format in a variable | ||
# Format the GMT time string | ||
now = time.strftime('%a, %d %b %Y %H:%M:%S +0000', gmt_time) | ||
|
||
|
||
# extract values from the event object we got from the Lambda service and store in a variable | ||
name = event['firstName'] +' '+ event['lastName'] | ||
# write name and time to the DynamoDB table using the object we instantiated and save response in a variable | ||
response = table.put_item( | ||
Item={ | ||
'ID': name, | ||
'LatestGreetingTime':now | ||
}) | ||
# return a properly formatted JSON object | ||
return { | ||
'statusCode': 200, | ||
'body': json.dumps('Hello from Lambda, ' + name) | ||
'statusCode': 200, | ||
'body': json.dumps('Hello from Lambda, ' + name) | ||
} |
11 changes: 11 additions & 0 deletions
11
tutorial/serverless-basic-application/assets/lamda_function_v1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# import the JSON utility package since we will be working with a JSON object | ||
import json | ||
# define the handler function that the Lambda service will use as an entry point | ||
def lambda_handler(event, context): | ||
# extract values from the event object we got from the Lambda service | ||
name = event['firstName'] +' '+ event['lastName'] | ||
# return a properly formatted JSON object | ||
return { | ||
'statusCode': 200, | ||
'body': json.dumps('Hello from Lambda, ' + name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
resource "aws_dynamodb_table" "basic_dynamodb_table" { | ||
name = "DebugTableHelloWorld" | ||
billing_mode = "PROVISIONED" | ||
read_capacity = 5 | ||
write_capacity = 5 | ||
hash_key = "ID" | ||
|
||
attribute { | ||
name = "ID" | ||
type = "S" | ||
} | ||
|
||
tags = { | ||
Name = "tungbq-dynamodb-table-prod" | ||
Environment = "production" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters