Cloudformation LayerVersion S3Bucket Using Parameter

923 views Asked by At

I'm trying to use Cloudformation to package and deploy a simple "hello world" serverless app that uses a single Lambda Layer. The issue I'm having is that the LayerVersion section in my CF template file doesn't seem to like the fact that I'm using a !Ref to specify the S3Bucket and S3Key values. I don't want to hard-code these; nothing I've found in the documentation suggests that what I'm trying to do won't work, but it doesn't work :(

Here's the output of the deploy command that's failing:

aws cloudformation deploy --template-file out.yml --stack-name cftest-lambda --parameter-overrides S3BucketNameParameter=cftest-0eddf3f0b289f2c2 S3LambdaLayerNameParameter=cftest-lambda-layer-1602434332.zip --capabilities CAPABILITY_NAMED_IAM

Waiting for changeset to be created..

Failed to create the changeset: 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 [libs] is invalid. property Content not defined for resource of type AWS::Serverless::LayerVersion

Here is the full CF template file:

cat template.yml 
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Lambda application
Parameters: 
  S3BucketNameParameter: 
    Type: String
    Description: Bucket name for deployment artifacts
  S3LambdaLayerNameParameter:
    Type: String
    Description: Object name for lambda layer deployment artifact
Resources:
  helloworldfunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: lambda_function.lambda_handler
      Runtime: python3.8
      CodeUri: hello-world-with-layer/.
      Description: Hello world function to test cf using layers
      Timeout: 10
      # Function's execution role
      Policies:
        - AWSLambdaBasicExecutionRole
        - AWSLambdaReadOnlyAccess
        - AWSXrayWriteOnlyAccess
      Tracing: Active
      Layers:
        - !Ref libs
  libs:
    Type: AWS::Serverless::LayerVersion
    Properties:
      Content:
        S3Bucket: !Ref S3BucketNameParameter
        S3Key: !Ref S3LambdaLayerNameParameter
      CompatibleRuntimes:
        - python3.8
      LayerName: hello-world-lib
      Description: Dependencies for the hello-world-with-layer app.

Any suggestions on how to approach this correctly?

1

There are 1 answers

2
Marcin On BEST ANSWER

The correct properties for LayerContent are:

  Bucket: String
  Key: String
  Version: String

However, you are using (different names):

 S3Bucket: String
 S3Key: String