aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

SAM generated templates for inline lambdas no longer valid for Cloudformation

Open superwese opened this issue 6 years ago • 5 comments

Description:

Yesterday night I was able to publish a cloudformation stack It is generated with sam cli and defines among other things 2 Lambda functions with inlineCode. The sam generated template file specifies also an CodeUri property:

  IncrementFunction:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: nodejs12.x
      Handler: index.handler
      InlineCode: 'exports.handler = async (event) =>  ++event;

        '
      CodeUri: IncrementFunction

Yesterday I was able to deploy and update this stack. From today on updating this stack was refused with the error message:

 Resource with id [IncrementFunction] is invalid. 'CodeUri' is not a valid S3 Uri of the form 's3://bucket/key' with optional versionId query parameter.

This also happens, if I c&p the template from CloudFormation console and try to update the stack.

Steps to reproduce:

create a stack with this template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs12.x
      InlineCode: |
        exports.handler = async (event) =>  ++event;

Observed result:

Status: FAILED. Reason: Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HelloWorldFunction] is invalid. 'CodeUri' is not a valid S3 Uri of the form 's3://bucket/key' with optional versionId query parameter.

Expected result:

the stack to be created

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: MacOS 10.15.7
  2. sam --version: SAM CLI, version 1.10.0

superwese avatar Nov 18 '20 17:11 superwese

Hi @superwese, I can deploy the template you provided:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs12.x
      InlineCode: |
        exports.handler = async (event) =>  ++event;

I can also see the error if this line is added in the template: CodeUri: IncrementFunction

The reason is the CodeUri need to be a S3 Uri s3://bucket/key. Please check this document: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-codeuri

Please let us know if you still have problem with s3 uri. Thanks!

wchengru avatar Nov 20 '20 17:11 wchengru

Please let us know if you still have problem with s3 uri. Thanks!

The thing is, sam cli adds the "invalid" CodeUri to its's generated template. I do provide my template without the CodeUri parameter. This behaviour may somehow related to the fact, that SAM CLI copies the whole project directory to it's .aws-sam/build dir.

When I try to deploy to eu-central-1 an us-west-1 the error occurs.

When I change my code to have the one-liner in an index.js file and provide a CodeUri everything works as expected.

superwese avatar Nov 20 '20 19:11 superwese

I am also seeing this issue with a series of python deployments. This simple template also has the issue:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  Prove that sam-cli does not work for inline code

Resources:
  InlineFunctionExample:
    Type: 'AWS::Serverless::Function'
    Properties:
      InlineCode: "
      def handler(*args):
        print('hello world')
      "
      Handler: index.handler
      Runtime: python3.7
      FunctionName: 'prove-bug'

Result:

Waiting for changeset to be created..
Error: Failed to create changeset for the stack: prove-sam-is-bugged, ex: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [InlineFunctionExample] is invalid. 'CodeUri' is not a valid S3 Uri of the form 's3://bucket/key' with optional versionId query parameter.

The template that sam build created shows the issue:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'Prove that sam-cli does not work for inline code

  '
Resources:
  InlineFunctionExample:
    Type: AWS::Serverless::Function
    Properties:
      InlineCode: ' def handler(*args): print(''hello world'') '
      Handler: index.handler
      Runtime: python3.7
      FunctionName: prove-bug
      CodeUri: InlineFunctionExample

Per the documentation that @wchengru posted, "Note: Either CodeUri or InlineCode is required." As @superwese said, sam build is causing these errors.

brewneaux avatar Nov 20 '20 23:11 brewneaux

This issue should be resolved now and the previous resolution order should be in place.

awood45 avatar Nov 21 '20 02:11 awood45

Closing as the issue has been resolved, if you see this again please reopen.

awood45 avatar Nov 21 '20 02:11 awood45