Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sfosterDunelm opened this issue Aug 4, 2023 · 5 comments
Closed
Assignees
Labels
aws-cdk-lib Related to the aws-cdk-lib package bug This issue is a bug. effort/medium Medium work item – several days of effort p0

Comments

@sfosterDunelm
Copy link

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

@sfosterDunelm sfosterDunelm added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 4, 2023
@github-actions github-actions bot added the aws-cdk-lib Related to the aws-cdk-lib package label Aug 4, 2023
@pahud pahud self-assigned this Aug 4, 2023
@pahud pahud added p1 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Aug 4, 2023
@pahud
Copy link
Contributor

pahud commented Aug 4, 2023

Yes I can confirm this error exists back in 2.88

probably related to:

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

@peterwoodworth
Copy link
Contributor

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

@peterwoodworth peterwoodworth added p0 and removed p1 labels Aug 4, 2023
@pahud pahud removed their assignment Aug 4, 2023
@pahud pahud changed the title (aws-cdk-lib.aws_sam): CfnFunction does not generate events in template after v2.87.0 sam: CfnFunction does not generate events in template after v2.87.0 Aug 4, 2023
@rix0rrr rix0rrr self-assigned this Aug 8, 2023
rix0rrr added a commit that referenced this issue Aug 9, 2023
Because of a mistake introduced into the SAM schema, the `AlexaSkill`
event type doesn't have any required properties anymore.

When the `CfnFunction` is trying all the different event types in the
type union that it supports, it will go through every type in
alphabetical order and pick the first type that doesn't fail its
validation.

After the schema change, the first type (`Alexa` which starts with an
`A`) would therefore accept all types: no required fields, and for
JavaScript compatibility purposes we allow superfluous fields, and so
we pick a type that doesn't render anything.

This change reorders the alternatives in the union such that stronger
types are tried first.

`HttpApiEvent` and `AlexaSkillEvent` both have no required
properties, and this now reverses the problem: `AlexaSkillEvent` can
no longer be specified because `HttpApiEvent` will match first.

But that's the more common use case, so better for now, while we wait
for the spec fix to come in, to prefer the HTTP API.

Relates to #26637.
mergify bot pushed a commit that referenced this issue Aug 10, 2023
Because of a mistake introduced into the SAM schema, the `AlexaSkill` event type doesn't have any required properties anymore.

When the `CfnFunction` is trying all the different event types in the type union that it supports, it will go through every type in alphabetical order and pick the first type that doesn't fail its validation.

After the schema change, the first type (`Alexa` which starts with an `A`) would therefore accept all types: no required fields, and for JavaScript compatibility purposes we allow superfluous fields, and so we pick a type that doesn't render anything.

This change reorders the alternatives in the union such that stronger types are tried first.

`HttpApiEvent` and `AlexaSkillEvent` both have no required properties, and this now reverses the problem: `AlexaSkillEvent` can no longer be specified because `HttpApiEvent` will match first.

But that's the more common use case, so better for now, while we wait for the spec fix to come in, to prefer the HTTP API.

Relates to #26637.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@theimowski
Copy link

A workaround using Raw overrides:

    writeFunction.AddPropertyOverride($"Events.{sqsEventSourceName}.Properties.Queue", queue.QueueArn)
    writeFunction.AddPropertyOverride($"Events.{sqsEventSourceName}.Properties.BatchSize", 1000)

@comcalvi
Copy link
Contributor

This has been fixed by #26679 and cdklabs/awscdk-service-spec#459

@github-actions
Copy link

⚠️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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aws-cdk-lib Related to the aws-cdk-lib package bug This issue is a bug. effort/medium Medium work item – several days of effort p0
Projects
None yet
Development

No branches or pull requests

6 participants