From c1069533d6f57a1664c455ac4833ae340542eda7 Mon Sep 17 00:00:00 2001 From: kakakakakku Date: Thu, 18 Sep 2025 08:04:51 +0900 Subject: [PATCH 1/5] cloudfront-apigw-rest-api-lambda-dynamodb-sam: Fix README.md --- .../README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cloudfront-apigw-rest-api-lambda-dynamodb-sam/README.md b/cloudfront-apigw-rest-api-lambda-dynamodb-sam/README.md index 0c1060f29..d5234c197 100644 --- a/cloudfront-apigw-rest-api-lambda-dynamodb-sam/README.md +++ b/cloudfront-apigw-rest-api-lambda-dynamodb-sam/README.md @@ -1,6 +1,6 @@ -# Amazon Cloudfront distribution in front of Amazon S3 and an Amazon API Gateway REST API to AWS Lambda to Amazon DynamoDB +# Amazon CloudFront distribution in front of Amazon S3 and an Amazon API Gateway REST API to AWS Lambda to Amazon DynamoDB -This pattern demonstrates a simple webform application sitting in Cloudfront and S3. The application stores data in DynamoDB via a ```POST``` request to an API Gateway which triggers a simple Lambda function. +This pattern demonstrates a simple webform application sitting in CloudFront and S3. The application stores data in DynamoDB via a ```POST``` request to an API Gateway which triggers a simple Lambda function. Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. @@ -15,11 +15,11 @@ Important: this application uses various AWS services and there are costs associ 1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: ``` - git clone https://gitlab.aws.dev/jrdwyer/aws-serverless-webform-sample.git + git clone https://github.com/aws-samples/serverless-patterns ``` 1. Change directory to the pattern directory: ``` - cd serverless-webform-sample + cd cloudfront-apigw-rest-api-lambda-dynamodb-sam ``` 1. From the command line, use AWS SAM to deploy the AWS resources for the workflow as specified in the template.yaml file: ``` @@ -34,7 +34,7 @@ Important: this application uses various AWS services and there are costs associ Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults. ``` - Stack Name [sam-app]: serverless-webform-sample + Stack Name [sam-app]: cloudfront-apigw-rest-api-lambda-dynamodb-sam AWS Region [us-east-1]: Parameter HTTPGetFloodRateParam [10000]: Parameter HTTPPostFloodRateParam [1000]: @@ -65,7 +65,7 @@ The web interface allows an end user to enter their personal details and click ` ## Testing -Open the Cloudfront URL in a browser on your local machine. Fill out the form with data and click ```Submit```. Upon submitting, your browser will send a ```POST``` request to the API Gateway and trigger the Lambda to store the data in DynamoDB. +Open the CloudFront URL in a browser on your local machine. Fill out the form with data and click ```Submit```. Upon submitting, your browser will send a ```POST``` request to the API Gateway and trigger the Lambda to store the data in DynamoDB. To view the data in DynamoDB, navigate to the DynamoDB service in the AWS console, select ```Tables``` from the left navigation pane and click on the ```serverless-webform-sample-table``` table. In the ```View Table``` page click on ```Explore Table Items``` button and scroll down to ```Items Returned``` to view the data stored in the table. This data should match the data entered in the previous step. From 50f2e7e19c5707455e656d25eeaaefa803535aee Mon Sep 17 00:00:00 2001 From: kakakakakku Date: Sun, 21 Sep 2025 09:59:24 +0900 Subject: [PATCH 2/5] cloudfront-apigw-rest-api-lambda-dynamodb-sam: Update runtime to nodejs22.x --- cloudfront-apigw-rest-api-lambda-dynamodb-sam/template.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudfront-apigw-rest-api-lambda-dynamodb-sam/template.yaml b/cloudfront-apigw-rest-api-lambda-dynamodb-sam/template.yaml index cc3e3caa1..f6ba64f81 100644 --- a/cloudfront-apigw-rest-api-lambda-dynamodb-sam/template.yaml +++ b/cloudfront-apigw-rest-api-lambda-dynamodb-sam/template.yaml @@ -32,7 +32,7 @@ Resources: Handler: function.handler PackageType: "Zip" Tracing: Active - Runtime: "nodejs16.x" + Runtime: "nodejs22.x" Environment: Variables: DatabaseTable: !Ref DynamoDBTable From 2f5429c226ff1bcd792067259740360b288690c8 Mon Sep 17 00:00:00 2001 From: kakakakakku Date: Sun, 21 Sep 2025 10:51:24 +0900 Subject: [PATCH 3/5] cloudfront-apigw-rest-api-lambda-dynamodb-sam: Fix permission for deploy_frontend.sh --- cloudfront-apigw-rest-api-lambda-dynamodb-sam/deploy_frontend.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 cloudfront-apigw-rest-api-lambda-dynamodb-sam/deploy_frontend.sh diff --git a/cloudfront-apigw-rest-api-lambda-dynamodb-sam/deploy_frontend.sh b/cloudfront-apigw-rest-api-lambda-dynamodb-sam/deploy_frontend.sh old mode 100644 new mode 100755 From dd2494d8d4f799247aef4cb7a0e2d2be750d2648 Mon Sep 17 00:00:00 2001 From: kakakakakku Date: Sun, 21 Sep 2025 10:58:34 +0900 Subject: [PATCH 4/5] cloudfront-apigw-rest-api-lambda-dynamodb-sam: Add DependsOn for AWS::WAFv2::WebACLAssociation --- cloudfront-apigw-rest-api-lambda-dynamodb-sam/template.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cloudfront-apigw-rest-api-lambda-dynamodb-sam/template.yaml b/cloudfront-apigw-rest-api-lambda-dynamodb-sam/template.yaml index f6ba64f81..81f37f283 100644 --- a/cloudfront-apigw-rest-api-lambda-dynamodb-sam/template.yaml +++ b/cloudfront-apigw-rest-api-lambda-dynamodb-sam/template.yaml @@ -314,7 +314,8 @@ Resources: Properties: ResourceArn: !Sub arn:aws:apigateway:${AWS::Region}::/restapis/${API}/stages/dev WebACLArn: !GetAtt APIWebACL.Arn - + DependsOn: APIdevStage + Outputs: CloudfrontDomain: Value: !GetAtt CloudFront.DomainName From 16b3ec05f20a9974a7d3ccc928f5c30ca8f68b42 Mon Sep 17 00:00:00 2001 From: kakakakakku Date: Sun, 21 Sep 2025 11:08:30 +0900 Subject: [PATCH 5/5] cloudfront-apigw-rest-api-lambda-dynamodb-sam: Update SDK to v3 for nodejs22.x runtime --- .../src/function.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cloudfront-apigw-rest-api-lambda-dynamodb-sam/src/function.js b/cloudfront-apigw-rest-api-lambda-dynamodb-sam/src/function.js index 89fa1275f..c8227e237 100644 --- a/cloudfront-apigw-rest-api-lambda-dynamodb-sam/src/function.js +++ b/cloudfront-apigw-rest-api-lambda-dynamodb-sam/src/function.js @@ -3,9 +3,11 @@ const randomBytes = require('crypto').randomBytes; -const AWS = require('aws-sdk'); +const { DynamoDBClient } = require('@aws-sdk/client-dynamodb'); +const { DynamoDBDocumentClient, PutCommand } = require('@aws-sdk/lib-dynamodb'); -const ddb = new AWS.DynamoDB.DocumentClient(); +const client = new DynamoDBClient({}); +const ddb = DynamoDBDocumentClient.from(client); exports.handler = (event, context, callback) => { @@ -46,7 +48,7 @@ exports.handler = (event, context, callback) => { }; function recordObject(message) { - return ddb.put({ + const command = new PutCommand({ TableName : process.env.DatabaseTable, Item: { Name: message.name, @@ -56,7 +58,8 @@ function recordObject(message) { Address: message.address, RequestTime: new Date().toISOString(), }, - }).promise(); + }); + return ddb.send(command); } function toUrlString(buffer) { @@ -77,4 +80,4 @@ function errorResponse(errorMessage, awsRequestId, callback) { 'Access-Control-Allow-Origin': '*', }, }); -} \ No newline at end of file +}