Home > Blockchain >  Merge two lists of objects in cloudformation template
Merge two lists of objects in cloudformation template

Time:03-03

I'm trying to write a clouformation template to deploy to ECS. For this I also want to define environment variables for the task definition. However the problem I am facing is that part of the environment is determined by a mapping, and another part is determined by template parameters.

Here's what I am currently trying (template reduced for brevity).

Parameters:
  Version:
    AllowedPattern: '^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(((0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\ ([0-9a-zA-Z-] (\.[0-9a-zA-Z-] )*))?$'
    Description: The version number
    Type: String
  ParameterEnvironment:
    Description: The environment
    Type: String
    AllowedValues:
      - Beta
      - Live
    Default: Beta    

Mappings:
  EnvironmentData:
    Beta:
      Environment:
        - Name: SOME_VARIABLE
          Value: some_variable_value
    Live:
      Environment:
        - Name: SOME_VARIABLE
          Value: some_variable_value_when_live

Resources:
  WebTaskDefinition:
    Properties:
      ContainerDefinitions:
          Environment:
            - Name: ENVIRONMENT
              Value: !Ref Version
            !FindInMap [ EnvironmentData, !Ref ParameterEnvironment, Environment ]
    Type: 'AWS::ECS::TaskDefinition'

The goal is to "merge" the !FindInMap [ EnvironmentData, !Ref ParameterEnvironment, Environment ] result with a single Name,Value pair object that holds the version.

However this causes the entire template to fail.

I have also tried:

Mappings:
  EnvironmentData:
    Beta:
      Environment:
        - Name: VERSION
          Value: !Ref Version
        - Name: SOME_VARIABLE
          Value: some_variable_value
    Live:
      Environment:
        - Name: VERSION
          Value: !Ref Version
        - Name: SOME_VARIABLE
          Value: some_variable_value_when_live

but this also seems to be failing since I don't think !Ref is allowed to be used in the mappings.

I have searched around for solutions but all I could find were solutions on using !Join and then !Split which doesn't work for me because I have a list of objects rather than strings.

CodePudding user response:

You could only do that through a macro. If you don't want to develop the macro, you have to re-organize your templates and setup so that you don't have to merge any lists.

  • Related