Skip to content

Commit

Permalink
add: lambda with dynamodb
Browse files Browse the repository at this point in the history
  • Loading branch information
1chooo committed Dec 24, 2023
1 parent a3e343c commit 7ea9f37
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloNameFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambda_handler
Runtime: python3.9
44 changes: 44 additions & 0 deletions test/lambda_dynamodb_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import boto3
import os

aws_access_key_id = 'YOUR_ACCESS_KEY_ID'
aws_secret_access_key = 'YOUR_SECRET_ACCESS_KEY'
region_name = 'YOUR_REGION'

# 創建 DynamoDB client
dynamodb = boto3.resource(
'dynamodb',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region_name
)

def lambda_handler(event: any, context: any):

user: str = event["user"]
visit_count: int = 0

# Prepare the DynamoDB client
dynamodb = boto3.resource("dynamodb")
table_name = "lambda-dynamodb-test"
# table_name = os.environ["TABLE_NAME"]
table = dynamodb.Table(table_name)

# Get the visit count from the DynamoDB table
response = table.get_item(Key={"user": user})
if "Item" in response:
visit_count = response["Item"]["visit_count"]

# Increment the visit count and put the item into DynamoDB table.
visit_count += 1
table.put_item(Item={"user": user, "visit_count": visit_count})

message: str = f"Hello {user}! You have visited us {visit_count} times."
return {"message": message}


if __name__ == "__main__":
# os.environ["TABLE_NAME"] = "lambda-dynamodb-test"
test_event = {"user": "1chooo"}
result = lambda_handler(test_event, None)
print(result)

0 comments on commit 7ea9f37

Please sign in to comment.