gpt4 book ai didi

aws-cloudformation - AWS CloudFormation - AWS::ElasticLoadBalancingV2::LoadBalancer - 安全组

转载 作者:行者123 更新时间:2023-12-03 07:46:58 26 4
gpt4 key购买 nike

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

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

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

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

最佳答案

您可以使用Condition来实现这一点和 AWS::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

关于aws-cloudformation - AWS CloudFormation - AWS::ElasticLoadBalancingV2::LoadBalancer - 安全组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52976363/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com