Skip to content

Commit e275fc5

Browse files
authored
Merge pull request #2837 from kakakakakku/cloudfront-apigw-rest-api-lambda-dynamodb-sam
cloudfront-apigw-rest-api-lambda-dynamodb-sam: Update runtime to nodejs22.x
2 parents 2e1cec9 + 16b3ec0 commit e275fc5

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

cloudfront-apigw-rest-api-lambda-dynamodb-sam/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Amazon Cloudfront distribution in front of Amazon S3 and an Amazon API Gateway REST API to AWS Lambda to Amazon DynamoDB
1+
# Amazon CloudFront distribution in front of Amazon S3 and an Amazon API Gateway REST API to AWS Lambda to Amazon DynamoDB
22

3-
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.
3+
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.
44

55
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.
66

@@ -15,11 +15,11 @@ Important: this application uses various AWS services and there are costs associ
1515

1616
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
1717
```
18-
git clone https://gitlab.aws.dev/jrdwyer/aws-serverless-webform-sample.git
18+
git clone https://github.com/aws-samples/serverless-patterns
1919
```
2020
1. Change directory to the pattern directory:
2121
```
22-
cd serverless-webform-sample
22+
cd cloudfront-apigw-rest-api-lambda-dynamodb-sam
2323
```
2424
1. From the command line, use AWS SAM to deploy the AWS resources for the workflow as specified in the template.yaml file:
2525
```
@@ -34,7 +34,7 @@ Important: this application uses various AWS services and there are costs associ
3434
3535
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.
3636
```
37-
Stack Name [sam-app]: serverless-webform-sample
37+
Stack Name [sam-app]: cloudfront-apigw-rest-api-lambda-dynamodb-sam
3838
AWS Region [us-east-1]:
3939
Parameter HTTPGetFloodRateParam [10000]:
4040
Parameter HTTPPostFloodRateParam [1000]:
@@ -65,7 +65,7 @@ The web interface allows an end user to enter their personal details and click `
6565
6666
## Testing
6767
68-
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.
68+
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.
6969
7070
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.
7171

cloudfront-apigw-rest-api-lambda-dynamodb-sam/deploy_frontend.sh

100644100755
File mode changed.

cloudfront-apigw-rest-api-lambda-dynamodb-sam/src/function.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
const randomBytes = require('crypto').randomBytes;
55

6-
const AWS = require('aws-sdk');
6+
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb');
7+
const { DynamoDBDocumentClient, PutCommand } = require('@aws-sdk/lib-dynamodb');
78

8-
const ddb = new AWS.DynamoDB.DocumentClient();
9+
const client = new DynamoDBClient({});
10+
const ddb = DynamoDBDocumentClient.from(client);
911

1012
exports.handler = (event, context, callback) => {
1113

@@ -46,7 +48,7 @@ exports.handler = (event, context, callback) => {
4648
};
4749

4850
function recordObject(message) {
49-
return ddb.put({
51+
const command = new PutCommand({
5052
TableName : process.env.DatabaseTable,
5153
Item: {
5254
Name: message.name,
@@ -56,7 +58,8 @@ function recordObject(message) {
5658
Address: message.address,
5759
RequestTime: new Date().toISOString(),
5860
},
59-
}).promise();
61+
});
62+
return ddb.send(command);
6063
}
6164

6265
function toUrlString(buffer) {
@@ -77,4 +80,4 @@ function errorResponse(errorMessage, awsRequestId, callback) {
7780
'Access-Control-Allow-Origin': '*',
7881
},
7982
});
80-
}
83+
}

cloudfront-apigw-rest-api-lambda-dynamodb-sam/template.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Resources:
3232
Handler: function.handler
3333
PackageType: "Zip"
3434
Tracing: Active
35-
Runtime: "nodejs16.x"
35+
Runtime: "nodejs22.x"
3636
Environment:
3737
Variables:
3838
DatabaseTable: !Ref DynamoDBTable
@@ -314,7 +314,8 @@ Resources:
314314
Properties:
315315
ResourceArn: !Sub arn:aws:apigateway:${AWS::Region}::/restapis/${API}/stages/dev
316316
WebACLArn: !GetAtt APIWebACL.Arn
317-
317+
DependsOn: APIdevStage
318+
318319
Outputs:
319320
CloudfrontDomain:
320321
Value: !GetAtt CloudFront.DomainName

0 commit comments

Comments
 (0)