How can I create a DependsOn relation between EC2 and RDS using aws-cdk
Apr 16
I am currently using the aws-cdk (TypeScript) to create a stack that consists of an 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 resources.
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 run into the same issue?
1 answer
Accepted answer · original discussion
Apr 16
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', {...});
instance.node.addDependency(rdsInstance);
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.
0 question comments
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.