Integration & Automation
Use AWS CDK to initialize Amazon RDS instances
In this blog post, we provide you with infrastructure as code (IaC) resources using the Amazon Web Services Cloud Development Kit (AWS CDK) framework. We describe how to initialize Amazon Relational Database Service (Amazon RDS) instances using AWS Lambda functions and AWS CloudFormation custom resources. Although we focus on MySQL, the concepts in this post can be applied to other Amazon RDS–supported environments.
After provisioning Amazon RDS instances, it’s common for infrastructure engineers to require initialization or management processes, usually through SQL scripts. The goal is to bootstrap or maintain the database server with a structure that matches the requirements of dependent applications or services.
Within the initialization process of an Amazon RDS instance, you can optionally address the following aspects:
- Initialize databases with corresponding schema or table structures.
- Initialize and maintain users and permissions.
- Initialize and maintain stored procedures, views, or other database resources.
- Run custom code.
When you provision your infrastructure on the AWS Cloud, custom initialization strategies require you to run code on a compute layer. To provision your infrastructure, we recommend using AWS Lambda or Amazon Elastic Container Service (Amazon ECS) combined with AWS Fargate because of the serverless lifecycle.
About this blog post | |
Time to read | ~10 min. |
Time to complete | ~15 min. |
Cost to complete | $0 |
Learning level | Advanced (300) |
AWS services | AWS CDK AWS CloudFormation Amazon RDS AWS Lambda |
The following architecture diagram shows a generic Amazon RDS–instances initialization process that is based in AWS Lambda, which is managed by AWS CDK and AWS CloudFormation. The architecture uses the AWS CloudFormation custom resources framework to run custom code during the provisioning process.

Figure 1. Amazon RDS architecture diagram
This deployment’s architecture sets up the following services and resources, as shown in figure 1:
- AWS CloudFormation, which invokes the creation of custom resources through a Lambda function (custom resource proxy).
- A highly available architecture that spans two Availability Zones.
- A virtual private cloud (VPC) configured with private subnets.
- In the private subnets:
- A Lambda function (initialization logic).
- An Amazon RDS instance where the initialization logic runs.
- AWS Secrets Manager for storing credentials.
Prerequisites
To complete this walkthrough, you must have the following:
- AWS CDK version 1.122 or later installed and configured on your local machine. The approach we document here is not yet compatible with CDK v2.x.
- Node.js version 14 or later installed on your local machine.
- Docker installed on your local machine.
- AWS CDK version 1.122 or later installed and configured on your local machine.
- A basic understanding of AWS CloudFormation.
- A basic understanding of AWS CDK constructs and stacks.
- Software development experience with TypeScript and JavaScript.
Walkthrough
The following sections describe how to initialize an Amazon RDS for MySQL instance. If you want to download and evaluate the code, see the associated GitHub repository.
Use TypeScript to create an empty CDK project
TypeScript is a fully supported client language for AWS CDK and is considered stable. Let’s proceed with creating an empty CDK project where we can develop our solution.
To create a new CDK project using TypeScript, follow these steps:
- From the AWS Command Line Interface (AWS CLI), navigate to your working folder.
- Install or update TypeScript:
- Create the project folder:
- Navigate to the project folder:
- Initialize the AWS CDK project for TypeScript:
Install software dependencies for your AWS CDK project
You must install aws-cdk
–related dependencies that provide the base constructs. To install all of the required source-code dependencies, run the following command:
Note: Depending on your configuration, you may need to restart your IDE.
Create the CdkResourceInitializer
construct
CDKResourceInitializer
is the AWS CDK construct that implements the initialization of AWS resources, such as Amazon RDS instances. To create the CDK construct, follow these steps:
- Create an empty
lib/
folder in your project’s root folder. - Create the
resource-initializer.ts
file inside the/lib
folder. Copy and then paste the following content inside the file:
To avoid unnecessary software dependencies, we recommend using Docker container images to package the Amazon RDS initialization function code. In this context, initialization function code is the RDS initialization process itself. For simplicity, we run a basic SQL script. The function implementation is based in Node.js and JavaScript.
To create the AWS Lambda function code:
- Create an empty
demos/
folder in your project’s root folder. - Create an empty
demos/rds-init-fn-code/
folder. - Create the Docker file inside the
rds-init-fn-code
folder. Copy and then paste the following content inside the file:
- Create the
index.js
file inside therds-init-fn-code
folder, and paste the following content inside the file:
- Create the
package.json
file inside therds-init-fn-code
folder, and paste the following content inside the file:
- Create the
script.sql
file inside therds-init-fn-code
folder, and paste the following content inside:
Example Amazon RDS stack with initialization support
Now create an AWS CDK stack to deploy the entire solution composed of a custom VPC, Amazon RDS instance, and a function-based initialization script. For simplicity, we use a hard-coded configuration within the RdsInitStackExample
CDK stack.
To create and provision an example RDS Stack with initialization support:
- Create the
rds-init-example.ts
file inside thedemos/
folder, and paste the following content inside:
- Update the target
bin.ts
file defined incdk.json
with the following content:
- Provision the example RDS stack in your default AWS account by running the following command and its subsequent steps:
Figure 2 shows the expected output. Note that the first time you run this, it may take a few minutes.

Figure 2. RDS stack output
Cleanup
To avoid incurring future charges, delete the provisioned RdsInitStackExample
CDK Stack and related resources by running the following command:
Conclusion
In this blog post, we guided you through an Amazon RDS initialization approach using AWS CDK and AWS CloudFormation custom resources. We also presented the CdkResourceInitializer
construct implementation in TypeScript to support AWS resource initialization, such as RDS instances. In the same way, we presented a complete CDK stack and configuration, which contains all the necessary technical steps to provision the described solution.
For simplicity, we limited this demonstration to an RDS initialization using a basic script.sql
file. While this approach may be sufficient for most use cases, you can extend this behavior to support more complex initialization processes that satisfy your requirements. You can use the CdkResourceInitializer
construct to initialize or integrate logic for other resources and processes, such as the following:
- Populating initial Amazon Simple Storage Service (Amazon S3) bucket structure and files.
- Managing users and permissions for a new ActiveMQ broker.
- Restoring backups on self-managed database instances.
Use the comments section to let us know if you have any questions.