CDK Template of Table Definition AWS Lambda for RDB

Overview

This sample deploys Amazon Aurora of PostgreSQL or MySQL with AWS Lambda that can define table declaratively and seed data by AWS Cloud Development Kit (CDK).

In addition to starting Amazon Aurora, you can also initialize the database, seed data, and declaratively modify tables at any time by running lambdas.

Architecture

There is only one CDK stack.The architecture is shown below.

Architecture

Directory Structures

.
├── bin
│   ├── db-definer.ts              # Define CDK Stack
│   └── db-settings.ts             # Define type of database family (PostgreSQL, MySQL) settings
├── lambda
│   └── db-definer
│       ├── schema                 # Table Definition. SQL file in this folder will be executed
│       └── seed
│           ├── mysql              # Seed SQL file for MySQL
│           └── postgresql         # Seed SQL file for PostgreSQL
└── lib
    ├── database.ts                # Amazon Aurora and AWS Lambda are defined here
    └── db-definer-stack.ts        # VPC and database

Main Libraries

Prerequisites

  • npm
  • aws cli v2
  • cdk
  • docker
  • configuration of aws profile

Getting Started

1. Clone the repository

  • Run git clone command to download the source code.

2. Deploy resources

  • Run npm ci command in the top of this directory.
  • Run npx cdk deploy to deploy these resouces.
  • When resouces are successfully deployed, outputs such as DbDefinerStack.DBDBLambdaName will be shown in the terminal. These values will be used to define tables.
  • You can edit DDL in ./lambda/db-definer/schema and DML in ./lambda/db-definer/seed before deploy.

Outputs:
DbDefinerStack.DBDBLambdaNameXXXXXXX = DbDefinerStack-DBDbDefinerXXXXXX
Stack ARN:
arn:aws:cloudformation:xyz-region-x:XXXXXXXXXXX:stack/DbDefinerStack/XXXXXXXXXXXXXXXXXX

3. Define Table

DbDefinerStack.DBDBLambdaName accept these command from event.

  1. init
    Drop and create database. You can use this command under developing.
    SQL file in ./lambda/db-definer/schema will be reflected.
  2. preview
    Preview change the definition of table from SQL file in ./lambda/db-definer/schema by using sqldef.
  3. sync
    Synchronize table in database to SQL file in ./lambda/db-definer/schema by using sqldef.
    You can preview SQL that will be excuted by sqldef.
  4. seed
    Run SQL file in ./lambda/db-definer/seed/mysql or ./lambda/db-definer/seed/postgresql.
    You can use this command for inserting seed data to database.

For example, when you want to initialize DB, run below command in the terminal.
You can excute this lambda from AWS Management Console.

aws lambda invoke --function-name [DbDefinerStack.DBDBLambdaName] --payload '{"command":"init"}' --cli-binary-format raw-in-base64-out res.txt

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.