Search code examples
javascripttypescriptamazon-web-servicesaws-cloudformationaws-cdk

AWS-CDK: TypeError: Cannot read properties of undefined (reading 'Parameters')


I'm using aws cdk 2.132.1 to instrument a simple Lambda application.

I have one stack named AllStack.ts that's the parent stack of all other stacks (DynamoDB, SNS, SQS, StepFunction, etc.) which looks like below:

import * as cfn_inc from 'aws-cdk-lib/cloudformation-include';
import {CfnStateMachine} from 'aws-cdk-lib/aws-stepfunctions';
import {Duration} from 'aws-cdk-lib';

interface AllStackProps {
    readonly env: DeploymentEnvironment;
    readonly lambdaFunction: IFunction;
}

export class AllStack extends DeploymentStack {
    constructor(scope: Construct, id: string, readonly props: AllStackProps) {
        super(scope, id, {
            env: props.env,
            softwareType: SoftwareType.INFRASTRUCTURE,
        });

        const cfnInclude = new cfn_inc.CfnInclude(this, 'Template', {
            templateFile: 'templates/root.yaml',
            // loadNestedStacks: {
            //   StepFunctionStack: {
            //     templateFile: 'templates/step_function.yaml',
            //     preserveLogicalIds: true,
            //     parameters: { 'Domain': props.env }
            //   },
            // },
            // parameters: {
            //     'Domain': props.env
            // }
        });
    }
}

It could build fine with loadNestedStacks part commented out, but when I uncommented these lines out, it failed to build with errors like this:

TypeError: Cannot read properties of undefined (reading 'Parameters')
    at CfnInclude.createNestedStack (ROOT_TO_MY_WORK_DIR/node_modules/aws-cdk-lib/cloudformation-include/lib/cfn-include.js:1:15080)
    at CfnInclude.getOrCreateResource (ROOT_TO_MY_WORK_DIR/node_modules/aws-cdk-lib/cloudformation-include/lib/cfn-include.js:1:13018)
    at CfnInclude.loadNestedStack (ROOT_TO_MY_WORK_DIR/node_modules/aws-cdk-lib/cloudformation-include/lib/cfn-include.js:1:5353)
    at new AllStack (ROOT_TO_MY_WORK_DIR/dist/lib/allStack.js:26:20)
    at Object.<anonymous> (ROOT_TO_MY_WORK_DIR/dist/lib/app.js:41:18)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)

Here's what my whole folder looks like:

-FunServiceCDK
--lib
---allStack.ts
---app.ts
--templates
---root.yaml
---step_function.yaml

Here's what my root.yaml looks like:

AWSTemplateFormatVersion: '2010-09-09'

Parameters:
  Domain:
    Type: String

Resources:
  StepFunctionStack:
    Type: AWS::CloudFormation::Stack

Here's what my step_function.yaml looks like:

AWSTemplateFormatVersion: '2010-09-09'

Parameters:
  Domain:
    Type: String

Resources:
  ComputationStepFunction:
    Type: AWS::StepFunctions::StateMachine

Looking at the CDK CfnInclude doc, parameters is an optional field, and I've supplied this field in both .ts file and .yaml file, somehow it's still not working, could someone help in this case!

Many thanks!


Solution

  • In your root CloudFormation template, the AWS::CloudFormation::Stack does not have its required fields set. From the docs, these are StackName and one of TemplateBody, TemplateURL.

    You can either specify an no-op template body or a placeholder template URL. CDK will replace it with the stack that you specify.

    From the CDK docs:

    the TemplateURL property of the nested stack resource will be modified to point to that asset.