Cloud Formation Logic Split Parameter Array Elements

0

I have a could formation template with a parameter that contains a list of elements e.g.

Parameter_Name: "a,b,c,d"

I need to split the list into single elements so that I can specify a list of values for a Resource entity. So far I have used the !Select and !Split functions to separate the list of elements into single values e.g.

- !Select [0, !Split [",", !Ref Paramter_Name]]
- !Select [1, !Split [",", !Ref Paramter_Name]]
- !Select [2, !Split [",", !Ref Paramter_Name]]
- !Select [3, !Split [",", !Ref Paramter_Name]]

This works providing I know the number of elements in the list. Can anyone suggest a way to handle lists with variable numbers of elements?

질문됨 일 년 전642회 조회
3개 답변
0
수락된 답변

Hi, AFAIK, the best of what you can do is use !If like in HACK VI of https://garbe.io/blog/2017/07/17/cloudformation-hacks/ I would not say that it is ideal in syntax ;-) but at least you can deal with a list of variable length...

Parameters:
  Env1:
    Type: String
    Description: An item of possible environment variables
    Default: ''

  Env2:
    Type: String
    Description: An item of possible environment variables
    Default: ''

  Env3:
    Type: String
    Description: An item of possible environment variables
    Default: ''


Conditions:
  Env1Exist: !Not [ !Equals [!Ref Env1, '']]
  Env2Exist: !Not [ !Equals [!Ref Env2, '']]
  Env3Exist: !Not [ !Equals [!Ref Env3, '']]


  TaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      TaskRoleArn: !Ref TaskRole
      ContainerDefinitions:
      - Name: !Ref AWS::StackName
        Image: !Ref ImageName

        Environment:
          'Fn::If':
            - Env1Exist
            -
              - Name: !Select [0, !Split ["|", !Ref Env1]]
                Value: !Select [1, !Split ["|", !Ref Env1]]
              - 'Fn::If':
                - Env2Exist
                -
                  Name: !Select [0, !Split ["|", !Ref Env2]]
                  Value: !Select [1, !Split ["|", !Ref Env2]]
                - !Ref "AWS::NoValue"
              - 'Fn::If':
                - Env3Exist
                -
                  Name: !Select [0, !Split ["|", !Ref Env3]]
                  Value: !Select [1, !Split ["|", !Ref Env3]]
                - !Ref "AWS::NoValue"

            - !Ref "AWS::NoValue"
profile pictureAWS
전문가
답변함 일 년 전
0

I "solved" this once by just including a "reserved" word for empty entries, like "none", and passed the full array in. Then tested whether the element was the reserved word or not. Not very elegant but got the job done. Also, I used a parameter type of CommaDelimitedList.

profile pictureAWS
전문가
kentrad
답변함 일 년 전
0

Thanks @Diddler_AWS the solution using conditions will work for me.

답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠