diff --git a/tutorial/serverless-basic-application/assets/lamda_function.py b/tutorial/serverless-basic-application/assets/lamda_function.py index f511897..89d6afe 100644 --- a/tutorial/serverless-basic-application/assets/lamda_function.py +++ b/tutorial/serverless-basic-application/assets/lamda_function.py @@ -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) } \ No newline at end of file diff --git a/tutorial/serverless-basic-application/assets/lamda_function_v1.py b/tutorial/serverless-basic-application/assets/lamda_function_v1.py new file mode 100644 index 0000000..213acd8 --- /dev/null +++ b/tutorial/serverless-basic-application/assets/lamda_function_v1.py @@ -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) + } \ No newline at end of file diff --git a/tutorial/serverless-basic-application/dynamodb.tf b/tutorial/serverless-basic-application/dynamodb.tf new file mode 100644 index 0000000..19731bc --- /dev/null +++ b/tutorial/serverless-basic-application/dynamodb.tf @@ -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" + } +} diff --git a/tutorial/serverless-basic-application/lamda.tf b/tutorial/serverless-basic-application/lamda.tf index 482c545..f241f88 100644 --- a/tutorial/serverless-basic-application/lamda.tf +++ b/tutorial/serverless-basic-application/lamda.tf @@ -66,3 +66,37 @@ resource "aws_iam_role_policy_attachment" "lambda_policy" { role = aws_iam_role.lambda_exec.name policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" } + +# resource "aws_iam_role_policy_attachment" "dynamodb_task_permissions" { +# role = aws_iam_role.lambda_exec.name +# policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaDynamoDBExecutionRole" +# } + +resource "aws_iam_role_policy_attachment" "dynamodb_task_permissions" { + role = aws_iam_role.lambda_exec.name + policy_arn = aws_iam_policy.dynamodb_access.arn +} + +resource "aws_iam_policy" "dynamodb_access" { + name = "dynamodb-access" + policy = <