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

`sam build` mangles inline AWS::Lambda::Function types

Open richardhboyd opened this issue 6 years ago • 6 comments

Description

when I run sam build on a template that includes an AWS::Lambda::Function resource that uses the ZipFile inline code definition, sam zips up the entire directory that contains my template.yaml file and uploads that to S3, then replaces my ZipFile definition with a pointer to that newly created S3 object. Needless to say, this does not work.

Steps to reproduce

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Resources:
  TestFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: |
          import boto3
          import cfnresponse
          import os
          def main(event, context):
            try:
              client = boto3.client('events')
              response = client.describe_rule(Name=os.environ.get('RuleName', '00'))
              json_event = json.loads(response['EventPattern'])
              json_event['detail']['version'] = [1]
              response = client.put_rule(Name=response['Name'],EventPattern=json.dumps(json_event),State=response['State'])
              cfnresponse.send(event, context, cfnresponse.SUCCESS, response, event["RequestId"])
            except Exception as e:
              cfnresponse.send(event, context, cfnresponse.SUCCESS, {}, event["RequestId"])
      Handler: index.main
      Role: !GetAtt 'LambdaExecutionRole.Arn'
      Runtime: python3.6
      Timeout: '30'

Then run

sam build or sam deploy

Observed result

Please provide command output with --debug flag set.

Expected result

resources that aren't in the AWS::Serverless::.... namespace shouldn't be changed by the CLI.

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

  1. OS: AL2012
  2. sam --version:
{'metrics': [{'commandRun': {'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'duration': 9840, 'exitReason': 'success', 'exitCode': 0, 'requestId': 'd5068bcd-73e5-48a5-b6f1-9d9a3422ce5a', 'installationId': '0a4e33d3-deb6-4c83-8057-dff2e29384aa', 'sessionId': 'c1dbf4b0-ebcd-429b-a406-c93dd400f35a', 'executionEnvironment': 'CLI', 'pyversion': '3.7.7', 'samcliVersion': '0.52.0'}}]}

Add --debug flag to command you are running

richardhboyd avatar Jun 02 '20 17:06 richardhboyd

This happens for AWS::Serverless::Functions as well, though it's not quite as broken. if I use InlineCode for a SAM Function, it still packages up the entire root of my directory, but it doesn't try to upload it to S3 and leaves the appropriate inline code in my template.

richardhboyd avatar Jun 02 '20 23:06 richardhboyd

"resources that aren't in the AWS::Serverless::.... namespace shouldn't be changed by the CLI." We package both AWS::Serverless::* and other raw CloudFormation resources. So changing things out of that namespace is expected.

What sam version are you running?

  • edit jfuss- 0.52.0

What is the built template look like?

jfuss avatar Jun 03 '20 14:06 jfuss

I completely agree that the sam transform should/does generate resources from any namespace. It shouldn't change other resources in the template that are not in the AWS::Serverless namespace.

If I have a template with a AWS::Serverless::Function resource and an AWS::DynamoDB::Table resource, I would expect that my DyanmoDB resource would be unchanged by running sam build

This is the generated template. The contents of this artifact are the entire directory where my template resides (all the other function code, the template, the .gitignore file for the repo, the README.md file for the app, etc ...)

  TestFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket: aws-sam-cli-managed-default-samclisourcebucket-4mjiayxci6nh
        S3Key: resize003/3c18e378849e46e9533a6b22b8cf9565
      Handler: index.main
      Role:
        Fn::GetAtt:
        - LambdaExecutionRole
        - Arn
      Runtime: python3.6
      Timeout: '30'

Also, using InlineCode causes the same weird bundling behavior (though SAM does leave the template intact). If I have 10 inline AWS::Serverless::Functions, I end up with a build directory containing 10 folders, each with a full copy of my application.

richardhboyd avatar Jun 03 '20 14:06 richardhboyd

Spoke offline. I'm willing to be wrong on whether or not SAM should touch existing non AWS::Serverless resources. There is a bug with the way that the sam build stage is processing AWS::lambda::Functions that use inline code which @jfuss is going to dig into where it is coming from.

richardhboyd avatar Jun 03 '20 18:06 richardhboyd

+1

superwese avatar Nov 11 '20 16:11 superwese

Closing this issue, as this was fixed in our 1.16 release with our inline improvement, #2549 @richardhboyd Please update the SAMCLI version and let me know if any further help is needed!

qingchm avatar Mar 02 '21 23:03 qingchm