简体   繁体   中英

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. The RDS instance needs to be setup before the EC2 instance can be started and userdata will be executed.

The Problem I have is, that I could not find a way to define the DepensOn (Cloudformation) attribute between the two ressources. The workaround is, that I am using netsted stacks.

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) .

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:

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. All constructs in the dependency's scope will be deployed before any construct in this construct's 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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