aws / serverless-application-model

The AWS Serverless Application Model (AWS SAM) transform is a AWS CloudFormation macro that transforms SAM templates into CloudFormation templates.
https://aws.amazon.com/serverless/sam
Apache License 2.0
9.33k stars 2.38k forks source link

Provide passthrough `Code` property for `AWS::Serverless::Function`. #3358

Closed garretwilson closed 1 year ago

garretwilson commented 1 year ago

SAM's AWS::Serverless::Function removes the ability to use imported values in referring to the bucket and filename of my uploaded ZIP file for AWS Lambda. This is an inherent issue with the SAM transform design; see e.g. aws/serverless-application-model#2533 and a long comment in another ticket explaining all the gory details.

As explained in detail in aws/serverless-application-model#3264, I can do the following just fine using AWS::Lambda::Function, which uses Code instead of CodeUri. It looks like this:

      Code:
        S3Bucket:
          Fn::ImportValue:
            !Sub "other-stack-${env}:stagingBucket"
        S3Key: !Sub "foo-${ver}-aws-lambda.zip"

But I can't do that with AWS::Serverless::Function:

      CodeUri: !Sub
        - "s3://${bucket}/foo-${ver}-aws-lambda.zip"
        - bucket: my-staging-bucket
            Fn::ImportValue:
              !Sub "other-stack-${env}:stagingBucket"

That produces:

Error: Failed to create changeset for the stack: my-stack, ex: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state: For expression "Status" we matched expected path: "FAILED" Status: FAILED. Reason: Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyFunction] is invalid. 'CodeUri' requires Bucket and Key properties to be specified.

Please add a Code pass-through property to AWS::Serverless::Function so we can at least work around this shortcoming in the design of SAM.

GavinZZ commented 1 year ago

Hi thanks for creating a issue. I apologize for the trouble you've been through. I'll bring it up for discussion with the team and get back to you soon.

GavinZZ commented 1 year ago

Hi @garretwilson, I've brought this up to the team and we're having discussion on potential workaround/solution. Meanwhile I just want to quickly confirm that you've also tried with the following syntax and it doesn't work for you.

  CodeUri:
    Bucket:
      Fn::ImportValue:
        !Sub "other-stack-${env}:stagingBucket"
    Key: !Sub "foo-${ver}-aws-lambda.zip"
garretwilson commented 1 year ago

Well I'd like to answer your question, but suddenly SAM is doing all sorts of crazy things, as I just reported in aws/aws-sam-cli#6014. (Sigh. If it's not one thing, it's another thing breaking.)

garretwilson commented 1 year ago

@GavinZZ , I have wonderful news! (Note that the SAM traceback in aws/aws-sam-cli#6014 is still happening, but it doesn't seem to impede the actual deployment.) The format you asked about worked:

  CodeUri:
    Bucket:
      Fn::ImportValue:
        !Sub "other-stack-${env}:stagingBucket"
    Key: !Sub "foo-${ver}-aws-lambda.zip"

It seems that CodeUri allows not only a String but also a FunctionCode object, the latter of which has a Bucket and a Key. These seems to be passthrough properties for CloudFormation S3Bucket and S3Key. Yay! šŸŽ‰ Just what I need.

I wonder why nobody mentioned this during the long discussion in aws/serverless-application-model#3264? šŸ¤”

Is the CodeUri > FunctionCode type new for SAM, or has it always been there?

GavinZZ commented 1 year ago

Great to hear that and hopefully this syntax will unblock you. šŸŽ‰šŸŽ‰šŸŽ‰

CodeUri supported both String syntax and FunctionCode object syntax and it's always been this way.

GavinZZ commented 1 year ago

Going to mark this issue as resolved. Feel free to re-open if you have any additional questions.

garretwilson commented 1 year ago

'CodeUri' supported both 'String' syntax and 'FunctionCode' object syntax and it's always been this way.

In that case, feel free to close this current ticket as "invalid", because SAM already has exactly what I was requesting!

Note also that aws/serverless-application-model#3264 therefore doesn't impact me, since I can always use the passthrough properties.

I'm so happy to find this out. Thanks for taking the time to suggest that.