aws / aws-cli

Universal Command Line Interface for Amazon Web Services
Other
15.6k stars 4.14k forks source link

aws cloudformation package mixes two lambda functions with wrong indentation #8816

Open deleugpn opened 4 months ago

deleugpn commented 4 months ago

Describe the bug

When running aws cloudformation package the output template mixes two lambda functions if the second one has an indentation bug.

Expected Behavior

abort with a validation error

Current Behavior

All properties of the 2nd lambda overrides the properties of the 1st lambda. The 2nd lambda becomes null.

Reproduction Steps

Create a template called indentation-bug.yml

AWSTemplateFormatVersion: 2010-09-09
Transform: [AWS::Serverless-2016-10-31]

Description: AWS Lambda bug

Resources:
  LambdaOne:
    Type: AWS::Serverless::Function
    Properties:
      InlineCode: |
        exports.handler = async (event) => {
          return {
            statusCode: 200,
            body: JSON.stringify('Hello from LambdaOne!'),
          };
        };
      MemorySize: 1024
      EventInvokeConfig:
        MaximumRetryAttempts: 0
      Events:
        SignupResource:
          Type: Api
          Properties:
            Path: /one
            Method: ANY

    LambdaTwo:
    Type: AWS::Serverless::Function
    Properties:
      InlineCode: |
        exports.handler = async (event) => {
          return {
            statusCode: 200,
            body: JSON.stringify('Hello from LambdaOne!'),
          };
        };
      MemorySize: 1024
      EventInvokeConfig:
        MaximumRetryAttempts: 0
      Events:
        SignupResource:
          Type: Api
          Properties:
            Path: /two
            Method: ANY

run aws cloudformation package

aws cloudformation package --template-file ./indentation-bug.yml --s3-bucket "your-s3-bucket" --output-template indentation-bug-package.yml --profile your-profile

Possible Solution

No response

Additional Information/Context

The current output from package:

AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
Description: AWS Lambda bug
Resources:
  LambdaOne:
    Type: AWS::Serverless::Function
    Properties:
      InlineCode: "exports.handler = async (event) => {\n  return {\n    statusCode:\
        \ 200,\n    body: JSON.stringify('Hello from LambdaOne!'),\n  };\n};\n"
      MemorySize: 1024
      EventInvokeConfig:
        MaximumRetryAttempts: 0
      Events:
        SignupResource:
          Type: Api
          Properties:
            Path: /two
            Method: ANY
      CodeUri: s3://dev-develop-augmentt-source-code-storage/9cba226edf2a273d23d226a3927776a9
    LambdaTwo: null

Instead of silently mixing LambdaOne with LambdaTwo and causing a bug, it would be better to just error out on invalid template syntax.

CLI version used

2.17.14

Environment details (OS name and version, etc.)

ubuntu-latest (GitHub Actions), Mac M1

tim-finnigan commented 4 months ago

Thanks for reporting. I could reproduce the same output with the SAM CLI as well (by running sam package --template-file indentation-bug.yml)

The package command is part of the CLI CloudFormation customization so the team maintaining the customization would need to take a look here. But I'm also not sure why running the validate-template command doesn't result in a ValidationError. So I'll try asking the CloudFormation team about the ValidateTemplate API as well.