Skip to content

cloudformation_include: TypeError when Lambda Layers defined with Condition in template #36140

@idm-ryou

Description

@idm-ryou

Describe the bug

When including a CloudFormation template via CfnInclude, if a Lambda function defines its Layers property using a Fn::If intrinsic function, CDK throws: TypeError: props.layers?.forEach is not a function.

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Library Version

2.226.0

Expected Behavior

cdk synth should succeed and produce a valid CloudFormation template.

$ cdk synth

Conditions:
  MyCondition: true
Resources:
  MyFunctionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
  MyFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: ""
      Handler: lambda_function.lambda_handler
      Layers:
        Fn::If:
          - MyCondition
          - []
          - - arn:123456789012:lambda:aaa:123456789012:layer:example-layer:1
      Role:
        Fn::GetAtt: MyFunctionRole.Arn
      Runtime: python3.12
Parameters:
  BootstrapVersion:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /cdk-bootstrap/hnb659fds/version
    Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]

Current Behavior

cdk synth fails

$ cdk synth

<..snip..>

TypeError: props.layers?.forEach is not a function
    at new CfnFunction (/workspaces/my-workspace/node_modules/aws-cdk-lib/aws-lambda/lib/lambda.generated.js:1:86020)
    at Function._fromCloudFormation (/workspaces/my-workspace/node_modules/aws-cdk-lib/aws-lambda/lib/lambda.generated.js:1:82530)
    at CfnInclude.getOrCreateResource (/workspaces/my-workspace/node_modules/aws-cdk-lib/cloudformation-include/lib/cfn-include.js:1:15441)
    at new CfnInclude (/workspaces/my-workspace/node_modules/aws-cdk-lib/cloudformation-include/lib/cfn-include.js:1:2904)
    at /workspaces/my-workspace/src/app.ts:5:1
    at ViteNodeRunner.runModule (file:///workspaces/my-workspace/node_modules/vite-node/dist/client.mjs:397:4)
    at ViteNodeRunner.directRequest (file:///workspaces/my-workspace/node_modules/vite-node/dist/client.mjs:375:3)
    at ViteNodeRunner.cachedRequest (file:///workspaces/my-workspace/node_modules/vite-node/dist/client.mjs:189:11)
    at ViteNodeRunner.executeFile (file:///workspaces/my-workspace/node_modules/vite-node/dist/client.mjs:162:10)
    at CAC.run (file:///workspaces/my-workspace/node_modules/vite-node/dist/cli.mjs:102:28)

Reproduction Steps

app.ts

import * as cdk from "aws-cdk-lib";

const app = new cdk.App();
const stack = new cdk.Stack(app, "MyStack");
new cdk.cloudformation_include.CfnInclude(stack, "MyCfnInclude", {
  templateFile: "my.template.yml",
});

my.template.yml

Conditions:
  MyCondition: true
Resources:
  MyFunction:
    Type: AWS::Lambda::Function
    Properties:
      Role: !GetAtt "MyFunctionRole.Arn"
      Handler: lambda_function.lambda_handler
      Layers: !If
        - MyCondition
        - []
        - ["arn:123456789012:lambda:aaa:123456789012:layer:example-layer:1"]
      Code:
        ZipFile: ""
      Runtime: python3.12
  MyFunctionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service: lambda.amazonaws.com

Possible Solution

No response

Additional Information/Context

Using vite-node to run CDK app

AWS CDK Library version (aws-cdk-lib)

2.227.0

AWS CDK CLI version

2.1022.0

Node.js Version

v22.21.1

OS

Linux 6.6.87.2 (WSL2)

Language

TypeScript

Language Version

TypeScript 5.9.2

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/cloudformation-includeIssues related to the "CFN include v.20" packagebugThis issue is a bug.effort/mediumMedium work item – several days of effortp0potential-regressionMarking this issue as a potential regression to be checked by team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions