Create RDS Postgres or MySQL with AWS CDK and Java

Hamza Sabljakovic
2 min readDec 12, 2022

Like in my other CDK-related posts, we will begin with generating a new AWS CDK project.

mkdir rds-postgres && cd rds-postgres && cdk init app --language java

Now that we have the skeleton generated we can start by modifying the stack class. The first thing we will do is configure Postgress version 13.06.

package com.myorg;

import software.amazon.awscdk.Stack;
import software.amazon.awscdk.StackProps;
import software.amazon.awscdk.services.ec2.*;
import software.amazon.awscdk.services.rds.*;
import software.constructs.Construct;

public class RdsPostgresStack extends Stack {
public RdsPostgresStack(final Construct scope, final String id) {
this(scope, id, null);
}

public RdsPostgresStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);

final Vpc vpc = Vpc.Builder.create(this, id + "-vpc")
.natGateways(0) // Do not create any gateways
.build();

final IInstanceEngine instanceEngine = DatabaseInstanceEngine.postgres(
PostgresInstanceEngineProps.builder()
.version(PostgresEngineVersion.VER_13_6)
.build()
);

final DatabaseInstance databaseInstance = DatabaseInstance.Builder.create(this, id + "-rds")
.vpc(vpc)
.vpcSubnets(SubnetSelection.builder().subnetType(SubnetType.PRIVATE_ISOLATED).build())
.instanceType(InstanceType.of(InstanceClass.BURSTABLE3, InstanceSize.MICRO))
.engine(instanceEngine)
.instanceIdentifier(id + "-rds")
.build();
}
}

The above CDK configuration gives us a ready to use RDS instance.

RDS instance in the AWS management console

By default, AWS CDK prevents data loss by automatically taking a final snapshot before deleting the RDS resource. In other words, the cdk destroy command, won’t remove all the artifacts as you might expect.

Final snapshot taken automatically by the CDK before removing the RDS instance

This behavior can be overwritten by defining the removal policy. However, keep in mind that your data will be forever lost, not recommended for production workloads.

final DatabaseInstance databaseInstance = DatabaseInstance.Builder.create(this, id + "-rds")
.vpc(vpc)
.vpcSubnets(SubnetSelection.builder().subnetType(SubnetType.PRIVATE_ISOLATED).build())
.instanceType(InstanceType.of(InstanceClass.BURSTABLE3, InstanceSize.MICRO))
.engine(instanceEngine)
.instanceIdentifier(id + "-rds")
.removalPolicy(RemovalPolicy.DESTROY) // If you want the destroy command to not take the final snapshot
.build();

Using a different RDS engine — MySQL

Database software, of the engine as it’s called in the AWS CDK terms can be interchanged with none to very minimal changes to the RDS configuration. Here is an example of how to change Postgress to MySQL.

final IInstanceEngine instanceEngine = DatabaseInstanceEngine.mysql(
MySqlInstanceEngineProps.builder()
.version(MysqlEngineVersion.VER_8_0_30)
.build()
);

Other supported database engines are:

  • SQL server
  • Oracle
  • MariaDB

The full source code is available on GitHub

--

--