如何在 CDK 应用程序中获取 RDS 实例主机名?
Posted
技术标签:
【中文标题】如何在 CDK 应用程序中获取 RDS 实例主机名?【英文标题】:How to get RDS instance hostname in CDK app? 【发布时间】:2020-04-21 13:54:02 【问题描述】:我正在使用适用于 .NET 的 AWS CDK 创建一个堆栈,该堆栈由一个 VPC、一个 RDS 数据库实例和 ECS 中的应用程序负载平衡 Fargate 服务组成。我部署到 ECS 的容器需要通过环境变量设置 RDS 数据库的连接信息,但我不确定如何访问该信息。下面是我的 CDK 代码。
using Amazon.CDK;
using EC2 = Amazon.CDK.AWS.EC2;
using ECS = Amazon.CDK.AWS.ECS;
using RDS = Amazon.CDK.AWS.RDS;
namespace PersonalSecOps
public class PersonalSecOpsStack : Stack
internal PersonalSecOpsStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
var vpc = new EC2.Vpc(this, "PersonalSecOpsVpc", new EC2.VpcProps
MaxAzs = 3 // Default is all AZs in region
);
var mysql = new RDS.DatabaseInstance(this, "PersonalSecOpsRds", new RDS.DatabaseInstanceProps
Engine = RDS.DatabaseInstanceEngine.MYSQL,
PreferredBackupWindow = "05:00-06:00",
BackupRetention = Duration.Days(7),
RemovalPolicy = RemovalPolicy.DESTROY,
DeletionProtection = false,
MasterUsername = "admin",
InstanceClass = EC2.InstanceType.Of(EC2.InstanceClass.BURSTABLE2, EC2.InstanceSize.MICRO),
Vpc = vpc,
InstanceIdentifier = "PersonalSecOpsRds"
);
var ecsCluster = new ECS.Cluster(this, "PersonalSecOpsEcs", new ECS.ClusterProps
Vpc = vpc
);
var nextCloudService = new ECS.Patterns.ApplicationLoadBalancedFargateService(this, "NextcloudService", new ECS.Patterns.ApplicationLoadBalancedFargateServiceProps
Cluster = ecsCluster,
DesiredCount = 1,
TaskImageOptions = new ECS.Patterns.ApplicationLoadBalancedTaskImageOptions
Image = ECS.ContainerImage.FromRegistry("nextcloud"),
Secrets =
"MYSQL_PASSWORD", ???
,
Environment =
"MYSQL_DATABASE", "Nextcloud" ,
"MYSQL_USER", "admin" ,
"MYSQL_HOST", ???
,
MemoryLimitMiB = 2048,
PublicLoadBalancer = true
);
注意两个“???”我试图在容器上设置MYSQL_PASSWORD
和MYSQL_HOST
环境变量。我在某处读到 RDS 会自动生成 MasterUserPassword
并将其存储在 Secret Manager 中,但我不确定如何将其取出。另外,我需要 RDS 实例的主机名。在创建实例时,我无法找到一种方法来显式设置它,否则我只会这样做。
我这样做对吗?
【问题讨论】:
【参考方案1】:感谢 Reddit 上的 kingofpoptart 帮助回答 this question。
using Amazon.CDK;
using EC2 = Amazon.CDK.AWS.EC2;
using ECS = Amazon.CDK.AWS.ECS;
using RDS = Amazon.CDK.AWS.RDS;
namespace PersonalSecOps
public class PersonalSecOpsStack : Stack
internal PersonalSecOpsStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
var vpc = new EC2.Vpc(this, "PersonalSecOpsVpc", new EC2.VpcProps
MaxAzs = 3 // Default is all AZs in region
);
var mySqlPassword = new Secret(this, "PersonalSecOpsMySqlPassword", new SecretProps
GenerateSecretString = new SecretStringGenerator
PasswordLength = 20
);
var mySql = new RDS.DatabaseInstance(this, "PersonalSecOpsRds", new RDS.DatabaseInstanceProps
Engine = RDS.DatabaseInstanceEngine.MYSQL,
PreferredBackupWindow = "05:00-06:00",
BackupRetention = Duration.Days(7),
RemovalPolicy = RemovalPolicy.DESTROY,
DeletionProtection = false,
MasterUsername = "admin",
MasterUserPassword = mySqlPassword.SecretValue,
InstanceClass = EC2.InstanceType.Of(EC2.InstanceClass.BURSTABLE2, EC2.InstanceSize.MICRO),
Vpc = vpc,
InstanceIdentifier = "PersonalSecOpsRds"
);
var ecsCluster = new ECS.Cluster(this, "PersonalSecOpsEcs", new ECS.ClusterProps
Vpc = vpc
);
var nextCloudService = new ECS.Patterns.ApplicationLoadBalancedFargateService(this, "NextcloudService", new ECS.Patterns.ApplicationLoadBalancedFargateServiceProps
Cluster = ecsCluster,
DesiredCount = 1,
TaskImageOptions = new ECS.Patterns.ApplicationLoadBalancedTaskImageOptions
Image = ECS.ContainerImage.FromRegistry("nextcloud"),
Secrets =
"MYSQL_PASSWORD", ECS.Secret.FromSecretsManager(mySqlPassword)
,
Environment =
"MYSQL_DATABASE", "Nextcloud" ,
"MYSQL_USER", "admin" ,
"MYSQL_HOST", mySql.DbInstanceEndpointAddress
,
MemoryLimitMiB = 2048,
PublicLoadBalancer = true
);
【讨论】:
简而言之:mySql.DbInstanceEndpointAddress
以上是关于如何在 CDK 应用程序中获取 RDS 实例主机名?的主要内容,如果未能解决你的问题,请参考以下文章
在 CloudFormation/CDK 中获取 Sagemaker Notebook 实例 IP 地址
在 aws-cdk 上的 aws-rds 上,使数据库可公开访问的设置在哪里?
如何将 VPC 和安全组分配给 AWS CDK 中的 Lambda?