Search code examples
amazon-web-servicesaws-lambdaaws-cloudformationaws-samaws-lambda-layers

AWS SAM/Cloudformation for multi environment - convert comma delimited list to ARNs


I am working with AWS SAM to generate infra code for a multi-environment setup. I want to use the same template.yaml file for dev/test/prod with a separate configuration file (i.e samconfig.yaml). How do I assign existing layer ARNs to a lambda function as these layers have different names and versions?

SAM template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Parameters:
  Client:
    Type: String
  Stage:
    Type: String
  Layers:
    Type: CommaDelimitedList
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub ${Client}-${Stage}-lambda
      Layers:
        # - !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:request-layer:2'

samconfig.toml:

version = 0.1
[default]
[default.deploy]
[default.deploy.parameters]
.
.
.
parameter_overrides = "Layers=\"request-layer:2,pandas-layer:5\""

My approach is to have a comma-delimited parameter which I would like to convert into an ARN list using intrinsic functions but I can't.


Solution

  • Since Layers is your parameter of type CommaDelimitedList, you can use it as follows:

    Resources:
      HelloWorldFunction:
        Type: AWS::Serverless::Function
        Properties:
          FunctionName: !Sub ${Client}-${Stage}-lambda
          Layers: !Ref Layers