Sunday 13 August 2023

AWS RDS Connect Compute Resource via CDK

I basically just want to add a Compute Resource (EC2 instance) to an RDS DB cluster with the AWS CDK as described here. https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/ec2-rds-connect.html. But I can't find any examples online or in the CDK Docs (https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_rds.DatabaseCluster.html).

I can manually add compute resources from the AWS console no problem but I want to do it programmatically with AWS CDK. How would I do this, or what are my options?

Here is my current AWS CDK code for RDS. Here I am trying to add an EC2 instance resource that I created in another stack called my_ec2_stack.my_ec2 as a compute resource for my_rds_cluster. But if this is a wrong approach or you know a different way to do this, let me know. Examples would be appreciated as well.

Additional details: The rds DB cluster below and my EC2 instance that I want to add as a Compute Resource are in the same VPC and subnet.

my_rds_cluster = rds.DatabaseCluster(self, "MyRDSDatabaseCluster",
            cluster_identifier="my-postgres",
            engine=rds.DatabaseClusterEngine.aurora_postgres(
                version=rds.AuroraPostgresEngineVersion.VER_11_18
            ),
            instance_props=rds.InstanceProps(
                vpc=shared_vpc_stack.vpc,
                instance_type=ec2.InstanceType.of(ec2.InstanceClass.R6G, ec2.InstanceSize.XLARGE2),
                vpc_subnets=ec2.SubnetSelection(
                   availability_zones=["us-west-2a", "us-west-2b", "us-west-2c", "us-west-2d"],
                   subnets=[private_subnet_a, private_subnet_b, private_subnet_c, private_subnet_d],
                ),
                auto_minor_version_upgrade=True,
                enable_performance_insights=True,
                publicly_accessible=False,
                security_groups=[my_rds_security_group, my_ec2_stack.searchy_security_group, my_ec2_stack.soary_security_group],
            ),
            backup=rds.BackupProps(
                retention=Duration.days(7),
            ),
      )

# My failed attempt to add compute resources EC2. (No error it just doesn't do anything)

        my_rds_connections = my_rds_cluster.connections.allow_from(
            other=my_ec2_stack.my_ec2,
            port_range=ec2.Port.tcp(5432),
            description="My RDS connection to My EC2"
        )

Screenshot below shows what it looks like in the AWS RDS Console when a compute resource is connected:

enter image description here



from AWS RDS Connect Compute Resource via CDK

No comments:

Post a Comment