-
Notifications
You must be signed in to change notification settings - Fork 1k
cloudfront-apigw-rest-api-lambda-dynamodb-sam: Update runtime to nodejs22.x #2837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c106953
50f2e7e
2f5429c
dd2494d
16b3ec0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: Fix to correct URL. |
||
``` | ||
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. | ||
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: The error occurred. ./deploy_frontend.sh
zsh: permission denied: ./deploy_frontend.sh |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
Comment on lines
+6
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: Some error occurs. Because in Node.js 18 and later runtimes, AWS SDK v3 is bundled, so I rewrote it to v3. See articles below. |
||
|
||
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': '*', | ||
}, | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ Resources: | |
Handler: function.handler | ||
PackageType: "Zip" | ||
Tracing: Active | ||
Runtime: "nodejs16.x" | ||
Runtime: "nodejs22.x" | ||
Environment: | ||
Variables: | ||
DatabaseTable: !Ref DynamoDBTable | ||
|
@@ -314,7 +314,8 @@ Resources: | |
Properties: | ||
ResourceArn: !Sub arn:aws:apigateway:${AWS::Region}::/restapis/${API}/stages/dev | ||
WebACLArn: !GetAtt APIWebACL.Arn | ||
|
||
DependsOn: APIdevStage | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: The error occurred.
|
||
|
||
Outputs: | ||
CloudfrontDomain: | ||
Value: !GetAtt CloudFront.DomainName | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: Fix to correct service name.