/aws-s3-lambda-aurora-datasync

Sync data from S3 to RDS Aurora

Primary LanguagePython

AWS Lambda and Amazon Aurora Serverless v2

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.

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
    
  2. Change directory to the pattern directory:

    cd lambda-aurora-serverlessv2-postgresql
    
  3. 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
    
  4. 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 use sam build && sam deploy in future to use these defaults.

  5. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.

How it works

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.

Testing

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\"}]"
}

Documentation

Cleanup

  1. Delete the stack
    sam delete
  2. 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