Skip to content

Commit

Permalink
Merge pull request #174 from tungbq/issue-163
Browse files Browse the repository at this point in the history
[Issue-163] Module 4 - Create a Data Table
  • Loading branch information
tungbq authored Jul 14, 2023
2 parents efd7040 + b89b306 commit 2df25b1
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
33 changes: 29 additions & 4 deletions tutorial/serverless-basic-application/assets/lamda_function.py
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 tutorial/serverless-basic-application/assets/lamda_function_v1.py
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)
}
17 changes: 17 additions & 0 deletions tutorial/serverless-basic-application/dynamodb.tf
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"
}
}
34 changes: 34 additions & 0 deletions tutorial/serverless-basic-application/lamda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"dynamodb:PutItem",
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:Scan",
"dynamodb:Query",
"dynamodb:UpdateItem"
],
"Resource": "${aws_dynamodb_table.basic_dynamodb_table.arn}"
}
]
}
EOF
}
7 changes: 7 additions & 0 deletions tutorial/serverless-basic-application/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ output "base_url" {

value = aws_api_gateway_stage.lambda.invoke_url
}


output "dynamo_db_arn" {
description = "Base URL for API Gateway stage."

value = aws_dynamodb_table.basic_dynamodb_table.arn
}

0 comments on commit 2df25b1

Please sign in to comment.