3
votes

im looking for a way to specifiy 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 customers 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 untill all other customers resources have been generated before he can use the system and so on. As there are no cross dependencies between the customer stacks, they dont 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 cunstruct path, but it doesnt 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.

1
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: docs.aws.amazon.com/cdk/latest/guide/codepipeline_example.htmlMilo
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
Thanks @Milo good read, however not quite what i was looking for.Lightning303
@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
Ah - thats great to know.amwill04

1 Answers

0
votes

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.