AWS CDK use `dependsOn` across different .yaml templates

40 Views Asked by At

I have a few different .yaml files defining various stacks: dynamodb.yaml, sns.yaml, sqs.yaml, batch.yaml, gateway.yaml, stepfunction.yaml, users_roles.yaml, etc.

In the old world (prior to use the modern aws-cdk-lib), I used to have a hacky way to read all these yaml-defined resources using bash script, now I'd like to upgrade it to use modern way, one of the issues that I encountered is the dependsOn fields across these .yaml files, e.g.

in my gateway.yaml file:

AWSTemplateFormatVersion: '2020-02-09'
Resources:
  NiceComputeAPI:
    DependsOn:
      - ComputeRole
    Type: AWS::ApiGateway::RestApi

in which ComputeRole is an iam role that's defined in this users_roles.yaml file:

AWSTemplateFormatVersion: '2020-02-09'
Resources:
  ComputeRole:
    Type: AWS::IAM::Role

Of all the resources/code examples that I can find, it's all using dependsOn within the same .yaml template file instead of cross different .yaml files.

I saw this question and its two comments, looks like not possible, but still wanted to raise this as a separate question to be sure.

If not possible, is the only possible approach is to merge all of the different .yaml files into one monolith .yaml file? In other words, AWS-CDK is forcing developers to go with a monolithic approach instead of various mini template files?

Many thanks!

1

There are 1 best solutions below

2
therealdakotal On

So the short answer is no. CDK is not forcing you to build one monolithic stack. The opposite is true. CDK allows you to build abstractions that can be reused across stacks, as well as the ability to build and maintain stacks in a more sensible way (see the NestedStack construct).

So rather than using scripts to staple your little stacks together into one stack and using dependsOn to maintain the build order, you can keep them in their respective stacks and orchestrate them with an overlying stack and the node.addDependency API.

See this page for an explanation of constructs and the various levels.

See this page for a tutorial on deploying multiple stacks in one project.