简体   繁体   English

如何使用 aws-cdk 在 EC2 和 RDS 之间创建 DependsOn 关系

[英]How can I create a DependsOn relation between EC2 and RDS using aws-cdk

I am currently using the aws-cdk (typescript) to create a stack that consists of a EC2 instance and a RDS databaseInstance.我目前正在使用 aws-cdk (typescript) 创建一个由 EC2 实例和 RDS databaseInstance 组成的堆栈。 The RDS instance needs to be setup before the EC2 instance can be started and userdata will be executed.需要先设置 RDS 实例,然后才能启动 EC2 实例并执行 userdata。

The Problem I have is, that I could not find a way to define the DepensOn (Cloudformation) attribute between the two ressources.我遇到的问题是,我找不到在两个资源之间定义 DepensOn (Cloudformation) 属性的方法。 The workaround is, that I am using netsted stacks.解决方法是,我正在使用 netsted 堆栈。

The code looks something like this:代码看起来像这样:

const instance = new ec2.Instance(this, 'Instance', {...})
const rdsInstance = new rds.DatabaseInstance(this, 'DbInstance', {...})

Now I would like to define something like instance.dependsOn(rdsInstance) .现在我想定义类似instance.dependsOn(rdsInstance)的东西。

Did anybody ran into the same issue?有人遇到同样的问题吗?

Thanks, Felix谢谢,菲利克斯

The solution here is to use addDependency() on the node , this will handle all the necessary CloudFormation DependsOn for you:这里的解决方案是在node上使用addDependency() ,这将为您处理所有必要的 CloudFormation DependsOn

const instance = new ec2.Instance(this, 'Instance', {...});
const rdsInstance = new rds.DatabaseInstance(this, 'DbInstance', {...});

rdsInstance.node.addDependency(instance);

From the JSDoc of addDependency() : Add an ordering dependency on another Construct.来自addDependency()的 JSDoc:添加对另一个 Construct 的排序依赖。 All constructs in the dependency's scope will be deployed before any construct in this construct's scope.依赖项的 scope 中的所有构造都将在此构造的 scope 中的任何构造之前部署。

Hope the following helps you.希望以下内容对您有所帮助。

const instance = new ec2.Instance(this, 'Instance', { /* ... */ }).getInstance();
const rdsInstance = new rds.DatabaseInstance(this, 'DbInstance', { /* ... */ }).getInstance();

instance.addDependsOn(rdsInstance);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM