Is it possible or any way around to deploy multiple stack from a single stack? For example, I define three stacks namely DynamoDBStack
, IAMRoleStack
and LambdaStack
, now I want these stacks to be deployed under, lets say DeployAllStacks
, so that it's identified as single stack in AWS CloudFormation stack list? I realised the following approach is wrong and cdk ls
recognise as 4 stacks instead of one, I'm trying something like this:
public DeployAllStacks(Construct scope, string id, IStackProps props = null):base(scope,id, props)
{
new DynamoDBStack(this, "DataBase");
new IAMRoleStack(this, "IAMRole");
new LambdaStack(this, "Lambda")
}
I understand that if I write all the resources in one single stack then it's doable, however I want the code to be neater and write separate stack to reused elsewhere.
CodePudding user response:
You can't. Stacks are the unit of deployment in the CDK. They can be grouped, but deploy separately in the cloud.
No matter. If code reuse is your goal, construct subclasses are the idiomatic CDK approach to composition. This is how the CDK itself is built. Multi-stack Apps also have a role for bigger projects, although this adds friction to deploys and cross-stack references*. See the CDK best practices guidance on Constructs and Stacks for design considerations. If I am interpreting your stack names correctly, one stack seems a good option, with construct subclasses for code org/reuse if needed.
* The CDK also supports CloudFormation nested stacks, which is a non-CDK native approach to reuse. But the child stacks are still stacks that "show up in the list".