This pattern demonstrates how to deploy a CDK application consisting of Amazon API Gateway, AWS Lambda, and Amazon DynamoDB. It makes use of the DynamoDB package in AWS Go SDK and the Go bindings for AWS CDK.
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns
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 for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Go (
1.16
or above) installed - AWS Cloud Development Kit (AWS CDK) installed
- Git installed
-
Clone the project to your local working directory
git clone https://github.com/aws-samples/serverless-patterns
-
Start by packaging the Lambda function in a zip file
cd apigw-lambda-dynamodb-cdk-go/function GOOS=linux go build -o my-func main.go zip function.zip my-func
-
Deploy the stack to your default AWS account and region
cd ../cdk cdk deploy
Enter
y
when prompted Do you wish to deploy these changes (y/n)?
Note the output from the CDK deployment process - it contains the API Gateway endpoint which will be used to test the integration.
When an HTTP POST
request is sent to the Amazon API Gateway endpoint, the AWS Lambda function is invoked and inserts an item into the Amazon DynamoDB table (named users-table
).
Testing the integration involves sending an HTTP POST
request to the Amazon API Gateway endpoint. This example used the curl CLI, but you can also use other options.
Export the Amazon API Gateway endpoint as an environment variable.
export APIGW_REST_ENDPOINT=<CDK deployment output>
e.g. export APIGW_REST_ENDPOINT=https://abc12345ff.execute-api.us-east-1.amazonaws.com/prod/
Invoke the endpoint:
curl -i -X POST -d '{"email":"user1@foo.com", "username":"user-1"}' $APIGW_REST_ENDPOINT
curl -i -X POST -d '{"email":"user2@foo.com", "username":"user-2"}' $APIGW_REST_ENDPOINT
You should get an HTTP 201 Created
response in both the cases. This indicates that the items have been added to the users
table in DynamoDB.
In this example, we will use the AWS CLI to check DynamoDB records:
aws dynamodb scan --table-name users-table
You should get a response:
{
"Items": [
{
"email": {
"S": "user1@foo.com"
},
"user_name": {
"S": "user-1"
}
},
{
"email": {
"S": "user2@foo.com"
},
"user_name": {
"S": "user-2"
}
}
],
"Count": 2,
"ScannedCount": 2,
"ConsumedCapacity": null
}
Try to insert the same record again:
curl -i -X POST -d '{"email":"user2@foo.com", "username":"user-2"}' $APIGW_REST_ENDPOINT
The Lambda function returns an HTTP 409 Conflict
to API Gateway in this case since a Condition Expression (attribute_not_exists(email)
) prevents an existing item (with the same email
) from being overwritten by the PutItem
call.
To delete the resources that were created:
cdk destroy
It might take some time for the CloudFormation stack to get deleted.
Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0