Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cloudfront-apigw-rest-api-lambda-dynamodb-sam/README.md
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
Copy link
Contributor Author

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.


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.

Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:
```
Expand All @@ -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]:
Expand Down Expand Up @@ -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.

Expand Down
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Empty file.
13 changes: 8 additions & 5 deletions cloudfront-apigw-rest-api-lambda-dynamodb-sam/src/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) => {

Expand Down Expand Up @@ -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,
Expand All @@ -56,7 +58,8 @@ function recordObject(message) {
Address: message.address,
RequestTime: new Date().toISOString(),
},
}).promise();
});
return ddb.send(command);
}

function toUrlString(buffer) {
Expand All @@ -77,4 +80,4 @@ function errorResponse(errorMessage, awsRequestId, callback) {
'Access-Control-Allow-Origin': '*',
},
});
}
}
5 changes: 3 additions & 2 deletions cloudfront-apigw-rest-api-lambda-dynamodb-sam/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Resources:
Handler: function.handler
PackageType: "Zip"
Tracing: Active
Runtime: "nodejs16.x"
Runtime: "nodejs22.x"
Environment:
Variables:
DatabaseTable: !Ref DynamoDBTable
Expand Down Expand Up @@ -314,7 +314,8 @@ Resources:
Properties:
ResourceArn: !Sub arn:aws:apigateway:${AWS::Region}::/restapis/${API}/stages/dev
WebACLArn: !GetAtt APIWebACL.Arn

DependsOn: APIdevStage
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: The error occurred.

CREATE_FAILED                                     AWS::WAFv2::WebACLAssociation                     APIWAFAssociation                                 Resource handler returned message: "AWS WAF
                                                                                                                                                      couldn?t perform the operation because your
                                                                                                                                                      resource doesn?t exist. (Service: Wafv2, Status
                                                                                                                                                      Code: 400, Request ID: 041ad0fb-
                                                                                                                                                      bad6-4a40-bce1-167f1218846f) (SDK Attempt
                                                                                                                                                      Count: 1)" (RequestToken:
                                                                                                                                                      acdc9901-b12e-7a14-f6c6-01eaa767eba3,
                                                                                                                                                      HandlerErrorCode: NotFound)


Outputs:
CloudfrontDomain:
Value: !GetAtt CloudFront.DomainName
Expand Down