aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.65k stars 3.91k forks source link

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

Closed sfosterDunelm closed 1 year ago

sfosterDunelm commented 1 year ago

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

pahud commented 1 year ago

Yes I can confirm this error exists back in 2.88

probably related to:

aws-cdk-lib: use new L1 codegen (https://github.com/aws/aws-cdk/issues/26318) (f15ed23) cfnspec: cloudformation spec v130.0.0 (https://github.com/aws/aws-cdk/issues/26278) (d316af7) cfnspec: cloudformation spec v130.1.0 (https://github.com/aws/aws-cdk/issues/26362) (52e20c9)

peterwoodworth commented 1 year ago

I can reproduce this on Python as well, thanks for reporting

theimowski commented 1 year ago

A workaround using Raw overrides:

    writeFunction.AddPropertyOverride($"Events.{sqsEventSourceName}.Properties.Queue", queue.QueueArn)
    writeFunction.AddPropertyOverride($"Events.{sqsEventSourceName}.Properties.BatchSize", 1000)
comcalvi commented 1 year ago

This has been fixed by #26679 and https://github.com/cdklabs/awscdk-service-spec/pull/459

github-actions[bot] commented 1 year ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.