使用 AWS CDK 访问 Glue Connection 的 RDS 实例子网 Python

Accessing RDS Instance subnet for Glue Connection using AWS CDK Python

在 Python 中使用 CDK,我在两个可用性区域中创建了一个具有 Public 和私有子网的 VPC,并将 RDS 数据库实例部署到私有子网中。 如何检索 RDS 实例所在的子网 ID,以便我不必将其硬编码到此粘合连接中?我已经尝试了 CfnOutputs 和 CDK 构造的几种不同迭代,但无法获得我需要的那个。感谢观看!

        glue_connection = aws_glue.CfnConnection(
        self,
        connection_id,
        catalog_id = self.account_name,
        connection_input = aws_glue.CfnConnection.ConnectionInputProperty(
            name = str(self.glue_connection_name),
            connection_type = 'JDBC',
            physical_connection_requirements = aws_glue.CfnConnection.PhysicalConnectionRequirementsProperty(
                subnet_id = 'PrivateSubnet2',
                security_group_id_list = [self.rds_SG.security_group_id, self.ec2_SG.security_group_id],
                availability_zone = 'us-east-1b',
            ),
            connection_properties = {
                'JDBC_CONNECTION_URL': f'jdbc:mysql://{self.DBInstance.db_instance_endpoint_address}:{self.DBInstance.db_instance_endpoint_port}/test',
                'PASSWORD': self.DBInstance.secret.secret_value_from_json("password").to_string(),
                'USERNAME': self.db_username,
                'JDBC_ENFORCE_SSL': 'false',
            },
        ),
    )

使用 L2 Connection 构造 - 它更简单 - 并从实例所在的子网列表中获取第一个子网:


glue_connection = aws_glue.Connection(
  self,
  "my_connection",
  type=aws_glue.ConnectionType.JDBC,
  properties={
     'JDBC_CONNECTION_URL': f'jdbc:mysql://{self.DBInstance.db_instance_endpoint_address}:{self.DBInstance.db_instance_endpoint_port}/test',
     'PASSWORD': self.DBInstance.secret.secret_value_from_json("password").to_string(),
     'USERNAME': self.db_username,
     'JDBC_ENFORCE_SSL': 'false',
   },
   security_groups=[my_rds_instance.connections.security_groups],
   subnet=my_vpc.private_subnets[0]
)