- Newest
- Most votes
- Most comments
The issue you're experiencing is likely due to how you're creating and referencing your nested stacks within the main stack. In AWS CDK, when you want to create nested stacks, you need to use the NestedStack class instead of the regular Stack class for your nested stacks.
Here's how you can modify your code to properly create nested stacks:
- First, ensure that your nested stack classes (CommonStack, LambdaStack, IamStack, and ApiGatewayResourceStack) inherit from
NestedStackinstead ofStack. For example:
public class CommonStack : NestedStack { internal CommonStack(Construct scope, string id, NestedStackProps props = null) : base(scope, id, props) { // Your stack definition here } }
- In your MainStack, create instances of these nested stacks using the
NestedStackconstructor. You'll also need to pass aNestedStackPropsobject instead ofStackProps. Here's how your MainStack should look:
public class MainStack : Stack { internal MainStack(Construct scope, string id, StackProps props = null) : base(scope, id, props) { // common resources stack var commonStack = new CommonStack(this, "Common-Stack", new NestedStackProps()); // lambda resources stack var lambdaStack = new LambdaStack(this, "Lambda-Stack", new NestedStackProps()); // iam resources stack var iamStack = new IamStack(this, "Iam-Stack", new NestedStackProps()); // gateway resources stack var gatewayResourceStack = new ApiGatewayResourceStack(this, "Gateway-Resource-Stack", new NestedStackProps()); // add dependencies for stacks lambdaStack.AddDependency(commonStack); iamStack.AddDependency(commonStack); gatewayResourceStack.AddDependency(commonStack); } }
By making these changes, your nested stacks will be properly referenced in the main stack's CloudFormation template when you run the cdk synth command. The main stack will include references to the nested stacks in its Resources section, and when deployed, it will create all the nested stacks as expected.
Remember that when using nested stacks, the CDK will generate separate CloudFormation templates for each nested stack, and the main stack's template will reference these nested stack templates.
If you still encounter issues after making these changes, double-check that you've imported the necessary modules and that your CDK version supports nested stacks in C#.
Sources
Deploying Lambda functions with the AWS CDK - AWS Lambda
Community | Multi-region Deployments with CDK
Relevant content
- asked 8 months ago
