Skip to content

sam: CfnFunction does not generate events in template after v2.87.0 #26637

@sfosterDunelm

Description

@sfosterDunelm

Describe the bug

Upgrading from v2.87.0 to v2.89.0 causes the events properties for a CfnFunction to no longer be generated:

Screenshot 2023-08-04 at 13 55 01

Expected Behavior

It should generate this template.json

{
 "Transform": [
  "AWS::Serverless-2016-10-31"
 ],
 "Resources": {
  "ApiGateway": {
   "Type": "AWS::Serverless::Api",
   "Properties": {
    "StageName": "Prod"
   },
   "Metadata": {
    "aws:cdk:path": "MyStack/ApiGateway"
   }
  },
  "MyAPI": {
   "Type": "AWS::Serverless::Function",
   "Properties": {
    "CodeUri": "build/",
    "Events": {
     "GetResource": {
      "Properties": {
       "Method": "POST",
       "Path": "/myPath",
       "RestApiId": {
        "Ref": "ApiGateway"
       }
      },
      "Type": "Api"
     }
    }
   },
   "Metadata": {
    "aws:cdk:path": "MyStack/MyAPI"
   }
  },
  "CDKMetadata": {
   "Type": "AWS::CDK::Metadata",
   "Properties": {
    "Analytics": "v2:deflate64:H4sIAAAAAAAA/yWJTQ5AMBBGz2KvQy2wFYkDcACptpLxMxXTshB3R6ze976XQVlAGqmThTazWHCAq/NKz/F79axWuOqRqg3jF00g7dHR/Ulr2YVd22/Xjgz+hZyxMHFyyBJkDjKaGFHsgTyuFtqfD0n6rSh2AAAA"
   },
   "Metadata": {
    "aws:cdk:path": "MyStack/CDKMetadata/Default"
   },
   "Condition": "CDKMetadataAvailable"
  }
 },
 "Conditions": {
  "CDKMetadataAvailable": {
   "Fn::Or": [
    {
     "Fn::Or": [
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "af-south-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ap-east-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ap-northeast-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ap-northeast-2"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ap-south-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ap-southeast-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ap-southeast-2"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ca-central-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "cn-north-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "cn-northwest-1"
       ]
      }
     ]
    },
    {
     "Fn::Or": [
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "eu-central-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "eu-north-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "eu-south-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "eu-west-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "eu-west-2"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "eu-west-3"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "me-south-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "sa-east-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "us-east-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "us-east-2"
       ]
      }
     ]
    },
    {
     "Fn::Or": [
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "us-west-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "us-west-2"
       ]
      }
     ]
    }
   ]
  }
 },
 "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]"
  }
 },
 "Rules": {
  "CheckBootstrapVersion": {
   "Assertions": [
    {
     "Assert": {
      "Fn::Not": [
       {
        "Fn::Contains": [
         [
          "1",
          "2",
          "3",
          "4",
          "5"
         ],
         {
          "Ref": "BootstrapVersion"
         }
        ]
       }
      ]
     },
     "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
    }
   ]
  }
 }
}

Current Behavior

But it generates this template.json missing the data in MyAPI.Events.GetResource.Properties:

{
 "Transform": [
  "AWS::Serverless-2016-10-31"
 ],
 "Resources": {
  "ApiGateway": {
   "Type": "AWS::Serverless::Api",
   "Properties": {
    "StageName": "Prod"
   },
   "Metadata": {
    "aws:cdk:path": "MyStack/ApiGateway"
   }
  },
  "MyAPI": {
   "Type": "AWS::Serverless::Function",
   "Properties": {
    "CodeUri": "build/",
    "Events": {
     "GetResource": {
      "Properties": {},
      "Type": "Api"
     }
    }
   },
   "Metadata": {
    "aws:cdk:path": "MyStack/MyAPI"
   }
  },
  "CDKMetadata": {
   "Type": "AWS::CDK::Metadata",
   "Properties": {
    "Analytics": "v2:deflate64:H4sIAAAAAAAA/yWJTQ5AMBBGz2KvQy2EpUgcgANItZWMn6mYloW4O2L1vve9DIoS0kidLLSZxYIDXJ1Xeo7fq2e1wlWPVG0Yv2gCaY+O7k9ayy7s2n67dmTwL+SMhYmTQxYgc5DRxIhiD+RxtdD+fABF4YAMdgAAAA=="
   },
   "Metadata": {
    "aws:cdk:path": "MyStack/CDKMetadata/Default"
   },
   "Condition": "CDKMetadataAvailable"
  }
 },
 "Conditions": {
  "CDKMetadataAvailable": {
   "Fn::Or": [
    {
     "Fn::Or": [
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "af-south-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ap-east-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ap-northeast-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ap-northeast-2"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ap-south-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ap-southeast-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ap-southeast-2"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "ca-central-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "cn-north-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "cn-northwest-1"
       ]
      }
     ]
    },
    {
     "Fn::Or": [
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "eu-central-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "eu-north-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "eu-south-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "eu-west-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "eu-west-2"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "eu-west-3"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "me-south-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "sa-east-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "us-east-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "us-east-2"
       ]
      }
     ]
    },
    {
     "Fn::Or": [
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "us-west-1"
       ]
      },
      {
       "Fn::Equals": [
        {
         "Ref": "AWS::Region"
        },
        "us-west-2"
       ]
      }
     ]
    }
   ]
  }
 },
 "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]"
  }
 },
 "Rules": {
  "CheckBootstrapVersion": {
   "Assertions": [
    {
     "Assert": {
      "Fn::Not": [
       {
        "Fn::Contains": [
         [
          "1",
          "2",
          "3",
          "4",
          "5"
         ],
         {
          "Ref": "BootstrapVersion"
         }
        ]
       }
      ]
     },
     "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
    }
   ]
  }
 }
}

Reproduction Steps

Creating this same stack that worked in 2.87.0 has issues in 2.89.0

import * as cdk from 'aws-cdk-lib'

const app = new cdk.App()

class MyStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props)

    const restApi = new cdk.aws_sam.CfnApi(this, 'ApiGateway', {
      stageName: 'Prod',
    })

    new cdk.aws_sam.CfnFunction(this, 'MyAPI', {
      codeUri: 'build/',
      events: {
        GetResource: {
          type: 'Api',
          properties: {
            restApiId: cdk.Fn.ref(restApi.logicalId),
            path: '/myPath',
            method: 'POST',
          },
        },
      },
    })
  }
}

new MyStack(app, 'MyStack')

app.synth()

Possible Solution

This won't deploy to AWS as the template.json isn't valid any more

Additional Information/Context

No response

CDK CLI Version

2.89.0

Framework Version

No response

Node.js Version

18.16.1

OS

macOS Ventura 13.5

Language

Typescript

Language Version

Typescript (4.7.4)

Other information

No response

Metadata

Metadata

Labels

aws-cdk-libRelated to the aws-cdk-lib packagebugThis issue is a bug.effort/mediumMedium work item – several days of effortp0

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions