如何使用 aws-cdk 在 EC2 和 RDS 之间创建 DependsOn 关系 [英] How can I create a DependsOn relation between EC2 and RDS using aws-cdk

查看:26
本文介绍了如何使用 aws-cdk 在 EC2 和 RDS 之间创建 DependsOn 关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 aws-cdk (TypeScript) 创建一个堆栈,该堆栈由一个 EC2 实例和一个 RDS 数据库实例组成.

I am currently using the aws-cdk (TypeScript) to create a stack that consists of an EC2 instance and a RDS databaseInstance.

RDS 实例需要先设置好,EC2 实例才能启动并执行 userdata.

The RDS instance needs to be setup before the EC2 instance can be started and userdata will be executed.

我遇到的问题是,我找不到在两个资源之间定义 DepensOn (Cloudformation) 属性的方法.

The problem I have is, that I could not find a way to define the DepensOn (Cloudformation) attribute between the two resources.

解决方法是,我使用的是 netsted 堆栈.

The workaround is, that I am using netsted stacks.

代码如下所示:

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

现在我想定义类似 instance.dependsOn(rdsInstance) 的东西.

Now I would like to define something like instance.dependsOn(rdsInstance).

有人遇到过同样的问题吗?

Did anybody run into the same issue?

推荐答案

这里的解决方案是在 node 上使用 addDependency(),这将处理所有必要的CloudFormation DependsOn 适合您:

The solution here is to use addDependency() on the node, this will handle all the necessary CloudFormation DependsOn for you:

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

rdsInstance.node.addDependency(instance);

来自 addDependency() 的 JSDoc:添加对另一个构造的排序依赖.依赖项范围内的所有构造将在此构造范围内的任何构造之前部署.

From the JSDoc of addDependency(): Add an ordering dependency on another Construct. All constructs in the dependency's scope will be deployed before any construct in this construct's scope.

这篇关于如何使用 aws-cdk 在 EC2 和 RDS 之间创建 DependsOn 关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆