26

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?

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
F. Werkmeister
  • 263
  • 1
  • 3
  • 5

2 Answers2

47

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.

jogold
  • 6,667
  • 23
  • 41
  • 1
    I also tried this solution deploying both in one stack. This leads to an exception... `aws-cdk/node_modules/constructs/lib/private/dependency.ts:86 throw new Error(`${instance} does not implement DependableTrait`); Error: [object Object] does not implement DependableTrait at Function.get ...node_modules/constructs/lib/private/dependency.ts:86:13)` – F. Werkmeister Apr 17 '20 at 08:38
  • Which version of the CDK are you using? All modules use the same exact version? – jogold Apr 17 '20 at 10:13
  • Just double checked: It's version 1.32.2 (build e19e206) and also all dependencies in package.json have this version. – F. Werkmeister Apr 17 '20 at 11:23
  • Just went through the answer again and I oversaw that in `rdsInstance.node.addDependency(instance);` instance did not have the `.node`. So it works like @jogold described. Thanks a lot!! – F. Werkmeister Apr 20 '20 at 07:35
  • also will be able to add multiple dependencies as array to addDependency method? – Khwaja Sanjari Dec 23 '20 at 17:41
  • 1
    @Sanjari you can do `resource.node.addDependency(...otherResources)` – jogold Dec 24 '20 at 09:39
  • 1
    Isn't the last line of the code block the wrong way around? `rdsInstance.node.addDependency(instance);` looks like it's declaring `instance` as a dependency of `rdsInstance` (I.e. `instance` starts first), whereas the question appears to want `rdsInstance` to be a dependency of `instance` (i.e. `rdsInstance should start first). – David P Aug 11 '23 at 09:59
  • 1
    @DavidP you are right, updated the answer. Thx. – jogold Aug 17 '23 at 14:53
2

Hope the following helps you.

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

instance.addDependsOn(rdsInstance);
Sam
  • 4,046
  • 8
  • 31
  • 47
  • Thanks for the answer, but this somehow does not work, because `ec2.Instance` and `rds.DatabaseInstance` do not have the method `getInstance()`. But somehow we would need to get the underlying `CfnRessource` to call `addDependsOn(otherResource)` if I understood correctly [from the docs](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.CfnResource.html). – F. Werkmeister Apr 17 '20 at 08:34