Jonny Rimek
Jonny Rimek

Reputation: 1171

CDK RDS public accessible database

I want to create a database with cdk that is publicly accessible (dev db). I know how to do it through the Web UI. I need to set the Public accessibility to true and allow my IP as Inbound on the Security Group.


        const vpc = new ec2.Vpc(this, 'Vpc')

        const postgres = new rds.DatabaseInstance(this, 'Postgres', {
            engine: rds.DatabaseInstanceEngine.POSTGRES,
            instanceClass: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
            masterUsername: 'postgres',
            vpc,
        })

this is my current status, it works fine, but no public access

Upvotes: 5

Views: 3054

Answers (1)

Jonny Rimek
Jonny Rimek

Reputation: 1171

        const vpc = new ec2.Vpc(this, 'Vpc')

        const postgres = new rds.DatabaseInstance(this, 'Postgres', {
            engine: rds.DatabaseInstanceEngine.POSTGRES,
            instanceClass: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
            masterUsername: 'postgres',
            vpc,
            vpcPlacement: { subnetType: ec2.SubnetType.PUBLIC }
        })

        postgres.connections.allowFromAnyIpv4(ec2.Port.tcp(5432))

vpcPlacement: { subnetType: ec2.SubnetType.PUBLIC } sets public access to true postgres.connections.allowFromAnyIpv4(ec2.Port.tcp(5432)) allows inbound traffic from port 5432

Upvotes: 14

Related Questions