Member-only story
Automate and Configure Your RDS Database With Terraform
Execute post-deployment scripts based on your needs
I’ve been using a lot CloudFormation and enjoying the journey so far. But as you all know, Terraform is trendy so I thought giving a shot at the tool and the different available options to automate a deployment may be worth it.
In this article, I will explain how you can deploy an RDS database, and run post deployment script such as create database, tables… I will assume that you already have your aws credentials configured, and terraform installed as well, and you’re also familiar with configuring an S3 bucket as Terraform backend — so I will mainly focus on the modules sections, and go thru the deployment part briefly at the end.
The project will deploy few items
- One bastion host group which will be located in a public subnet, that will allow you to ssh tunnel to your database + the associated security group
- One RDS PostGres database and associated security groups to allow only your VPC subnets to access the db
- One Lambda deployed within your VPC, because you want the lambda to be able to connect to the database in a secure way
- Some SSM parameters such as username / password for your database by the Lambda first and your application in the future
- A Lambda invocation trigger after all the previous items are deployed

Resources creation
This code block snippet (main.tf
— folder module/rds-database-lambda
) will create for you some of the items I mentioned previously:
- Your bastion security group. Note that you will need to allow your IP line 17 instead of the dummy one — l5
- The ec2 instance which requires as input a keypair (not…