This pattern creates an AWS Lambda function and an Amazon Aurora PostgreSQL DB in an Aurora Serverless v2 DB cluster with RDS Data API and a Secrets Manager secret.
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-aurora-serverlessv2-postgresql
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
- Git Installed
- AWS Serverless Application Model (AWS SAM) installed
-
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
-
Change directory to the pattern directory:
cd lambda-aurora-serverlessv2-postgresql
-
From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
sam build sam deploy --guided
-
During the prompts:
- Enter a stack name
- Enter the desired AWS Region (choose a region where the RDS Data API is available)
- Allow SAM CLI to create IAM roles with the required permissions.
Once you have run
sam deploy --guided
mode once and saved arguments to a configuration file (samconfig.toml), you can usesam build && sam deploy
in future to use these defaults. -
Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
This pattern creates an AWS Lambda function and an Amazon Aurora PostgreSQL DB in an Aurora Serverless v2 DB cluster with RDS Data API and a Secrets Manager secret. The function creates an example table named "music", inserts a row with data from the event object, then returns the results of a select query.
Once the application is deployed, navigate to the Lambda function and configure a test event using the sample event available in this repo. Alternatively, use sam remote invoke
including the event-file
option, e.g:
sam remote invoke LambdaFunction --stack-name STACK_NAME --event-file src/event.json
Invoke the function to execute SQL statements against the database. Modify the test event before subsequent invocations to insert new data into the example "music" table. Review the Amazon CloudWatch Logs for details on the function invocation.
Example test event:
{
"body": {
"artist": "The Beatles",
"album": "Abbey Road"
}
}
Response:
{
"statusCode": 200,
"body": "[{\"id\":1,\"artist\":\"The Beatles\",\"album\":\"Abbey Road\"}]"
}
- Using the Data API for Aurora Serverless
- Data API - ExecuteStatement
- Data API - ExecuteStatement Response Elements
- AWS Lambda - the Basics
- Lambda Function Handler
- Function Event Object - Overview
- Function Environment Variables
- Delete the stack
sam delete
- Confirm the stack has been deleted
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"
Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0