Skip to content

Execute Nested Stacks from the Root Stack using AWS CDK C#

0

I have used the AWS CDK using C# language. And below is my main method, which initiates the main stack

public static void Main(string[] args)
        {
            var app                                     = new App();
            new MainStack(app, "MainStack", new StackProps { });
            app.Synth();
        }

Below is my main stack, where I have initiated the nested stacks.

public class MainStack : Stack
    {
        internal MainStack(Construct scope, string id, StackProps stackProps = null) : base(scope, id, stackProps)
        {            
            // common resources stack
            var commonStack                 = new CommonStack(this, "Common-Stack", stackProps);
            
            // lambda resources stack
            var lambdaStack                 = new LambdaStack(this, "Lambda-Stack", stackProps);

            // iam resources stack
            var iamStack                    = new IamStack(this, "Iam-Stack", stackProps);

            // gateway resources stack
            var gatewayResourceStack        = new ApiGatewayResourceStack(this, "Gateway-Resource-Stack", stackProps);

            // add dependencies for stacks
            lambdaStack.AddDependency(commonStack);
            iamStack.AddDependency(commonStack);
            gatewayResourceStack.AddDependency(commonStack);
        }
    }

The nested stacks are properly defined. And each nested stack inherits the "Stack" class, as an example, "LambdaStack : Stack". And when I run the "synth" individually on each nested stack, it outputs the expected CloudFormation template for each stack.

However when I run the "synth" command for "MainStack", I was expecting references to the each nested stacks in the CloudFormation template in the "Resources" section. But however it comes empty (does not have the nested stack references). And even when I deploy the "MainStack" it does not create those nested stack, but creates an empty stack in the CloudFormation console.

There are no error messages in the console as well. What am I doing wrong in here.

1 Answer
1
Accepted Answer

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:

  1. First, ensure that your nested stack classes (CommonStack, LambdaStack, IamStack, and ApiGatewayResourceStack) inherit from NestedStack instead of Stack. For example:
public class CommonStack : NestedStack
{
    internal CommonStack(Construct scope, string id, NestedStackProps props = null) : base(scope, id, props)
    {
        // Your stack definition here
    }
}
  1. In your MainStack, create instances of these nested stacks using the NestedStack constructor. You'll also need to pass a NestedStackProps object instead of StackProps. 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

answered 10 months ago
AWS
EXPERT
reviewed 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.