Skip to content

Latest commit

 

History

History

apigw-http-lambda-documentdb-cdk

Amazon API Gateway to AWS Lambda to Amazon DocumentDB

This AWS CDK stack deploys an API Gateway HTTP API that integrates with a Lambda function and a DocumentDB cluster. The Lambda function is connected to the DocumentDB cluster through a VPC. The Lambda implements the CRUD operations function for this REST API.

You should use this pattern if you want to migrate your application to DocumentDB using a serverless REST API application layer.

Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-http-lambda-documentdb-cdk

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.

Requirements

Deployment Instructions

  1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
git clone https://github.com/aws-samples/serverless-patterns
  1. Change directory to the pattern directory:
cd apigw-http-lambda-documentdb-cdk
  1. run npm install to install npm packages:
npm install
  1. From the command line, configure AWS CDK with:
cdk bootstrap
  1. From the command line, use AWS CDK to deploy the AWS resources for the pattern:
cdk deploy

How it works

This will create an API Gateway HTTP API, a Lambda function, and a DocumentDB cluster. The output of the command will include the URL of the API Gateway HTTP API.

Security

The Lambda function is granted access to the DocumentDB cluster through a VPC security group. The security group only allows traffic from the Lambda function to the DocumentDB cluster.

The Lambda function is also granted access to the AWS Secrets Manager secret for the DocumentDB cluster. The secret contains the credentials for accessing the DocumentDB cluster.

Troubleshooting

If you are having trouble deploying or using the stack, please refer to the following resources:

AWS CDK documentation: https://docs.aws.amazon.com/cdk/latest/guide/

DocumentDB documentation: https://docs.aws.amazon.com/documentdb/latest/developerguide/

Lambda documentation: https://docs.aws.amazon.com/lambda/latest/dg/welcome.html

Amazon API Gateway HTTP documentation: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api.html

Testing

To use the API Gateway HTTP API, send a GET request to the root path (/). The Lambda function will be invoked and will return the contents of the DocumentDB cluster.

In the stack output, you can see ApiGatewayUrl. This URL can be used with the curl commands below to interact with the DocumentDB.

Example

  1. GET - Retrieve data from the DocumentDB collection:
curl -X GET "YOUR_API_ENDPOINT" -H "CONTENT-TYPE: application/json"
  1. POST - Insert data into the DocumentDB collection:
curl -X POST "YOUR_API_ENDPOINT" -H "CONTENT-TYPE: application/json" -d '{"key": "value"}'
  1. PUT - Update data in the DocumentDB collection:
curl -X PUT "YOUR_API_ENDPOINT" -H "CONTENT-TYPE: application/json" -d '{"_id": "A_VALID_ID_FROM_THE_DATABASE", "key": "newvalue"}'
  1. DELETE - Delete data from the DocumentDB collection:
curl -X DELETE "YOUR_API_ENDPOINT?id=A_VALID_ID_FROM_THE_DATABASE"

Cleanup

Delete the stack

cdk destroy

Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0