amazon-web-servicesaws-lambdaaws-cloudformationserverlessaws-sam

Validate AWS-SAM Template/Reduce turnaround time to find errors


I just got an error after deploying a large SAM template with a wrong keyword.

Instead of ServerSideEncryptionByDefault I wrote ServerSideEncryptionDefault (missing By).

    Type: AWS::S3::Bucket
    Properties:
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionDefault:
              SSEAlgorithm: AES256

The turnaround time to get this error is about a minute, because CloudFormation first handles all the other resources and finds this error late in my deployment.

Is it possible to have a linter or something check the code before I start a deployment?

I only found sam validate which doesn't find these kind of errors (https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-validate.html).


Solution

  • The CloudFormation Linter catches this with:

    E3002 Invalid Property Resources/Bucket/Properties/BucketEncryption/ServerSideEncryptionConfiguration/0/ServerSideEncryptionDefault

    The CloudFormation Linter Visual Studio Code extension runs the linter on every file save if you have both installed