How to automate RDS database creation with AWS CodeBuild
Here I use postgres as RDS database you can use any database according to your favor.

Introductions

Step 1

Create below files and push to aws code-commit

buildspec.yml

create_cname.json

buildspec.yml

We can use buildspec.yml file in AWS codebuild project for automate tasks. Because we want to create buildspec.yml file first before create the codebuild project.

Note: Here we create RDS database and assign DNS name for Database that would be easy to you when you connect db with applications, otherwise RDS create random aws url(weird) as database connection url. if you don’t have any route53 dns domain ignore below bold colour command steps.

buildspec.yml

version: 0.2 phases: install: commands: - export AWS_REGION=eu-west-2 - export SECURITY_GROUP_NAME=test_sec_group - export HOSTED_ZONE="rote53domainhostedzoneid" - export RDS_DATABASE_NAME="your_db_name" - export RDS_USER_NAME="your_username" - export RDS_PASSWORD="your_password" - SECURITY_GROUP_ID=$(aws ec2 create-security-group --description ${SECURITY_GROUP_NAME} --group-name ${SECURITY_GROUP_NAME} --region ${AWS_REGION} --output text) - aws ec2 authorize-security-group-ingress --group-id ${SECURITY_GROUP_ID} --protocol tcp --port 5432 --cidr 0.0.0.0/0 --region ${AWS_REGION} - aws rds create-db-instance --db-instance-identifier ${RDS_DATABASE_NAME} --db-name ${RDS_DATABASE_NAME} --vpc-security-group-ids ${SECURITY_GROUP_ID} --allocated-storage 20 --db-instance-class db.t2.small --engine postgres --master-username ${DS_USER_NAME} --master-user-password ${RDS_PASSWORD} --region ${AWS_REGION} - aws rds wait db-instance-available --db-instance-identifier ${RDS_DATABASE_NAME} - DB_URL=$(aws rds describe-db-instances --db-instance-identifier ${RDS_DATABASE_NAME} --region ${AWS_REGION} --output text | grep rds.amazonaws.com | awk '{print $2}') - | a=$(echo '"ResourceRecords": [{ "Value": "'$DB_URL'"}]') - | - sed -i "s/insert/$a/g" create_cname.json - aws route53 change-resource-record-sets --hosted-zone-id $HOSTED_ZONE --change-batch file://create_cname.json

create_cname.json

if you are not using route 53 dns name as db connection url ignore this step

create_cname.json

{ "Comment": "CREATE a record ", "Changes": [{ "Action": "CREATE", "ResourceRecordSet": { "Name": "your_dns_name.com", "Type": "CNAME", "TTL": 300, insert }}] }

Step 2

We can use AWS codebuild to automate this task. this step will describe how to create codebuild project and how to run it.

create an IAM role as below

Go to the IAM service → select Roles → click “create role” button

click on codebuild

Click “Next: Permission” button

Select below access policies and press “Next: Tags” button

Add the tags as below and click the “Next: Review” button

finally add the role name and click “Create role” button then IAM role will create for you. we can use that role in next step (2. Login to aws codebuild)

2. Create CodeBuild project

AWS Management console → Codebuild (Under the services)→ Click “Create build project” button.

3. Fill the all requirement as below

use previously created IAM role

Finally create the project.

Now you are ready to provision RDS database infrastructure.

3. Run CodeBuild project

Go to the AWS CodeBuild → select the Project → click “Start build” button → Select Branch and again click “Start build” button.

From few minutes Check the RDS database.

Now you can login to the postgres database using your defined DNS address or AWS endpoint url.

Good luck !!!!
['Milinda Bandara Gamawelagedara']
['Postgres', 'Aws Rds', 'Route 53', 'AWS', 'Codebuild']