【问题标题】:AWS CloudFormation - AWS::ElasticLoadBalancingV2::LoadBalancer - SecurityGroupsAWS CloudFormation - AWS::ElasticLoadBalancingV2::LoadBalancer - SecurityGroups
【发布时间】:2019-03-29 07:17:39
【问题描述】:

CFN 模板中是否有可能根据参数向 ALB 添加一些特定的安全组?

我遇到两个安全组添加到 ALB 的情况:

ALB
  Type: AWS::ElasticLoadBalancingV2::LoadBalancer
  Properties:
    ...
    SecurityGroups:
      - !Ref 'SecurityGroup1'
      - !Ref 'SecurityGroup2'

现在有一个SecurityGroup3,只有在某些参数具有特定值时,我才会最终添加它。假设如果参数add_sg3 等于yes,则将第三个 SG 添加到 ALB。我总是在类似的情况下使用"!If,但有超过 2 个 SG。任何的建议都受欢迎。谢谢!

【问题讨论】:

    标签: amazon-cloudformation


    【解决方案1】:

    您可以使用ConditionAWS::NoValue 伪参数来实现。下面是一个完整的例子:

    Parameters:
        Environment:
            Type: String
            Default: dev
            AllowedValues: ["dev", "prod"]
        VpcId:
            Type: 'AWS::EC2::VPC::Id'
        Subnet1:
            Type: 'AWS::EC2::Subnet::Id'
        Subnet2:
            Type: 'AWS::EC2::Subnet::Id'
    
    Conditions:
        MyTest: !Equals ["dev", !Ref Environment]
    
    Resources:
        ALB:
            Type: 'AWS::ElasticLoadBalancingV2::LoadBalancer'
            Properties:
                SecurityGroups:
                - !Ref SecurityGroup1
                - !If [ MyTest, !Ref SecurityGroup2, !Ref 'AWS::NoValue' ]
                Subnets:
                - !Ref Subnet1
                - !Ref Subnet2
    
        SecurityGroup1:
            Type: 'AWS::EC2::SecurityGroup'
            Properties:
                GroupDescription: 'Group 1'
                VpcId: !Ref VpcId
    
        SecurityGroup2:
            Type: 'AWS::EC2::SecurityGroup'
            Properties:
                GroupDescription: 'Group 2'
                VpcId: !Ref VpcId
    

    【讨论】:

    • 抱歉回复晚了。我之前没有机会测试它。您的解决方案完美运行。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2021-12-28
    • 2015-09-22
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多