skip to Main Content

How to convert CommaDelimitedList Parameter to String for passing it as environment variable to Lamda Function ?

Below is sample CommaDelimitedList Parameter containing list of AWS regions which I need to pass it as Environment variable to AWS Lambda function in cloud formation template. The reason is when I try to pass it as it is !Ref ExternalRegions it gives error when create/update of the stack:

Properties validation failed for resource LambdaFunction with message: #/Environment/Variables/EXTERNAL_REGIONS: expected type: String, found: JSONArray

Parameters:
  ExternalRegions:
    Description: CSV delimited account regions 
    Type: CommaDelimitedList

Resources:
    LambdaFunction:
        Type: AWS::Lambda::Function
        Properties:
          Environment:
            Variables:
              EXTERNAL_REGIONS: !Ref ExternalRegions

Parameter Template:

[{
    "ParameterKey": "ExternalRegions",
    "ParameterValue": "us-east-1,us-west-2,ap-southeast-1"
}]

Could you please help, I am looking for a way to cast csv list into string so that I don’t have to create another String variable with same values and always bother to keep them in sync during future changes.

Thanks in advace.

Scrub

Can’t use comma in environment variable value in Lambda?

Comma separator in Lambda function environment settings using the Serverless Framework

If this is the case I would like to know a way where I could convert the comma delimited list parameter to string and replace comma with say alternative delimiter for ex. semi-colon (;) and pass it as lambda function variable. Is this possible?

2

Answers


  1. Chosen as BEST ANSWER

    I figured out :)

    Resources:
      LambdaFunction:
        Type: AWS::Lambda::Function
          Properties:
            Environment:
              Variables:
                EXTERNAL_REGIONS: !Join [";", !Ref ExternalRegions]
    

  2. I don’t the issue you linked is still accurate as I was able to create Environment variables containing , in the web console env vars

    My recommendation would be to just changes the Parameter to Type: String since it looks like your lambda function will have to handle the ; delimiter anyway.

    Additionally, I’d be wary of using a semicolon as a delimiter in environment variables since it can have unexpected/unintended side effects… (particularly in a shell)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search