How do I use nested lists or append to a list in Cloudformation?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-08 09:25:10

问题


I want to give this resource 2 security groups that exist outside the stack, plus one that was created as part of the stack...

I have tried the below and received the error:

Value of property SecurityGroups must be of type List of String

SecurityGroups: 
- !FindInMap [ envMap, !Ref env, securityGroups ]
- !GetAtt SG.GroupId

for reference, here is my map

Mappings:
  envMap: 
    qa:
      "securityGroups":
        - sg-xxxxxxxx
        - sg-yyyyyyyy

and here is the resource

LoadBalancer:
    Type: 'AWS::ElasticLoadBalancingV2::LoadBalancer'
    Properties:
      Name: !Join
      - '-'
      - - 'OR'
        - 'ALB'
        - !Ref env
      Scheme: internal
      SecurityGroups: !FindInMap [ envMap, !Ref env, securityGroups ]
      Subnets: !FindInMap [ envMap, !Ref env, subnets ]
      Type: application
      IpAddressType: ipv4

EDIT: here is my fixed code

 "securityGroups": 'sg-xxxxxx,sg-yyyyyy'

      LoadBalancer:
        Type: 'AWS::ElasticLoadBalancingV2::LoadBalancer'
        Properties:
          Name: !Join
          - '-'
          - - !Ref appname
            - 'ALB2'
            - !Ref env
          Scheme: !FindInMap [ envMap, !Ref env, inorex ]
          SecurityGroups: !Split
            - ','
            - !Join
              - ','
              - - !Ref SG
                - !FindInMap [ envMap, !Ref env, securityGroups ]
          Subnets: !FindInMap [ envMap, !Ref env, exsubnets ]
          Type: application
          IpAddressType: ipv4`

回答1:


In order to add an additional security group to the list of string values provided by Fn::FindInMap function we need to construct a new list of string values using the return value of Fn::FindInMap and add the additional security group using the Fn::Sub function.

Parameters:
  env:
    Default: qa
    Type: String
Mappings:
  envMap:
    qa:
      securityGroups: 'sg-xxxxxxxx,sg-xxxxxxxx'
    sub:
      subnets: 'subnet-xxxxxxxx,subnet-xxxxxxxx'
Resources:
  LoadBalancer:
    Type: 'AWS::ElasticLoadBalancingV2::LoadBalancer'
    Properties:
      Name: !Join
        - '-'
        - - OR
          - ALB
          - !Ref env
      Scheme: internal
      SecurityGroups: !Split
        - ','
        - !Sub
          - 'sg-xxxxxxx,${mappedGroup}'
          - mappedGroup: !FindInMap
              - envMap
              - !Ref env
              - securityGroups
      Subnets: !Split
        - ','
        - !FindInMap
          - envMap
          - sub
          - subnets
      Type: application
      IpAddressType: ipv4
``


来源:https://stackoverflow.com/questions/49035760/how-do-i-use-nested-lists-or-append-to-a-list-in-cloudformation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!