9

I'm looking for a way to specify the deployment order of my stacks in my project. I know I could add dependencies, however, these stacks do not depend on each other and I might want to delete some at a later time.

I have a framework of services that are needed globally for every other stack (e.g. rds). These are being deployed at the very start.

  • App-Networking
  • App-GlobalResources

Now I want to add stacks for new customers, these stacks will depend on the 2 global stacks, and on each other, but not on any of the other customer's stacks.

E.g.

  • App-Customer-A-EC2 (depends on App-Networking)
  • App-Customer-A-Lambdas (depends on App-Customer-A-EC2)
  • App-Customer-A-Settings (depends on App-GlobalResources, App-Customer-A-Lambdas)

and

  • App-Customer-B-EC2 (depends on App-Networking)
  • App-Customer-B-Lambdas (depends on App-Customer-B-EC2)
  • App-Customer-B-Settings (depends on App-GlobalResources, App-Customer-B-Lambdas)

I would like all these stacks to be deployed in this order. First the global stacks, then all of customer a, then all of customer b.

However, this is what cdk is doing:

  • App-Networking
  • App-GlobalResources
  • App-Customer-A-EC2
  • App-Customer-B-EC2
  • App-Customer-A-Lambdas
  • App-Customer-B-Lambdas
  • App-Customer-A-Settings
  • App-Customer-B-Settings

Which means customer A has to wait until all other customer resources have been generated before he can use the system and so on. As there are no cross dependencies between the customer stacks, they don't have to be deployed in the order cdk does it.

So, what are my options here? Apart from adding dependencies? I thought initially it would be alphabetically ordered by stack name, or maybe by construct path, but it doesn't seem so.

Thank you!

Edit: I went through the code of the cdk app and found the sorting code. There currently is no way in my case. The type of sorting used by CDK will always result in the observed pattern.

I am now working around by adding dependencies. When deleting stacks that are "in the middle" and have dependencies, I have to destroy them with the -e argument.

Archmede
  • 1,592
  • 2
  • 20
  • 37
Lightning303
  • 91
  • 1
  • 1
  • 3
  • Apart from explicitly defining dependencies between stacks I don't know of any other onboard possibility (but please keep in mind that I am no CDK expert). But one alternative I can think of is creating a Build Pipeline that uses the CDK in each of its steps to deploy the stacks in order. You can read how CDK is used to deploy a stack via CDK from within a Build Pipeline here: https://docs.aws.amazon.com/cdk/latest/guide/codepipeline_example.html – Milo Jan 22 '20 at 10:13
  • As far as I am aware `aws-cdk deploy` is a stand alone deployment per stack. So even if you define multiple stacks within your root you still need to specify which stack you are deploying. For me I would create a deployment script that can check if certain deployments have already been made prior and then deploy in the order you wish. – amwill04 Jan 23 '20 at 14:42
  • Thanks @Milo good read, however not quite what i was looking for. – Lightning303 Jan 24 '20 at 12:14
  • @amwill04 you can define multiple stacks, and you can also use wildcards. So `cdk deploy *` will deploy all your stacks in the "correct" order. – Lightning303 Jan 24 '20 at 12:15
  • Ah - thats great to know. – amwill04 Jan 27 '20 at 12:51

2 Answers2

9

From the Stack Dependencies section of the documentation:

Two different stack instances can have a dependency on one another. This happens when an resource from one stack is referenced in another stack. In that case, CDK records the cross-stack referencing of resources, automatically produces the right CloudFormation primitives, and adds a dependency between the two stacks. You can also manually add a dependency between two stacks by using the stackA.addDependency(stackB) method.

Anjan Biswas
  • 7,746
  • 5
  • 47
  • 77
Tahir Rauf
  • 484
  • 6
  • 16
0

You could just invoke cdk deploy [stack_name] multiple times to deploy the "necessary" stacks first (e.g. one that deploys RDS or Networking components) and finally invoke cdk deploy * to provision all the remaining stacks.

Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118
  • You'll still most likely get a cyclic reference (I did), but running one stack at a time (while others are potentially commented out) is really helpful in debugging. – The Onin Oct 21 '22 at 04:57