【问题标题】:Cloudformation AWS: choose between Security GroupsCloudformation AWS:在安全组之间进行选择
【发布时间】:2020-02-26 14:25:28
【问题描述】:

我正在设计一个 cloudformation 模板,但我需要在几个安全组之间进行选择

我已经定义了一个参数和两个条件来启用一个或另一个具有相同名称的策略(以维护依赖关系)

但模板不适用于这两个选项,

当参数有 True 选项时,堆栈工作,并且值为 False 时显示以下错误:

Template format error: Unresolved resource dependencies [mySecurityGroup] in the Resources block of the template

这是模板的片段:

 Parameters:
  KeyName:
    Description: EC2 KeyPair 
    Type: 'AWS::EC2::KeyPair::KeyName'
  Reception:
    Description: Enable reception
    Default: False
    Type: String
    AllowedValues: 
      - True
      - False
Conditions:
  Enable:
    !Equals [True, !Ref Reception]
  Disable:
    !Equals [False, !Ref Reception]
Resources:
  myVPC:
    Type: 'AWS::EC2::VPC'
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsSupport: 'true'
      EnableDnsHostnames: 'true'
      InstanceTenancy: default
      Tags:
        - Key: Name
          Value: myVPC
  mySubNet:
    Type: 'AWS::EC2::Subnet'
    Properties:
      VpcId: !Ref myVPC
      CidrBlock: 10.0.0.0/24
      Tags:
        - Key: Name
          Value: mySubNet
  mySecurityGroup:
    Condition: Disable
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      VpcId: !Ref myVPC
      GroupDescription: Security Group for EC2 
      SecurityGroupIngress:
        - IpProtocol: udp
          FromPort: 4114
          ToPort: 4114
          CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: mySecurityGroup
  mySecurityGroup:
    Condition: Enable
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      VpcId: !Ref myVPC
      GroupDescription: Security Group for EC2
      SecurityGroupIngress:
        - IpProtocol: udp
          FromPort: 5683
          ToPort: 5683
          CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: mySecurityGroup
  myEC2:
    Type: 'AWS::EC2::Instance'
    Properties:
      KeyName: !Ref KeyName
      ImageId: ami-028188d9b49b32a80
      InstanceType: t2.nano
      NetworkInterfaces:
        - SubnetId: !Ref mySubNet
          AssociatePublicIpAddress: 'true'
          DeviceIndex: 0
          GroupSet:
            - !Ref mySecurityGroup
      Tags:
        - Key: Name
          Value: myEC2


我不确定哪种方法是正确的

【问题讨论】:

  • 什么意思它不起作用,请解释错误或结果。
  • 当参数有 True 选项时,堆栈工作,并且值为 False 显示以下错误:``` 模板格式错误:模板资源块中未解决的资源依赖项 [mySecurityGroup] ` ``
  • 您不需要创建两个完全不同的安全组,您可以使用相同的安全组 - 但有条件地添加入口规则(资源类型为 AWS::EC2::SecurityGroupIngress)。然后您可以使用 Fn::IF 函数来确定是否添加入口规则。请参阅此页面上的 Fn::If:docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…。我认为您的方法会遇到问题,因为 cloudformation 将首先尝试创建新资源,然后再删除冗余资源 - 造成冲突。

标签: amazon-web-services amazon-ec2 amazon-cloudformation aws-security-group


【解决方案1】:

好吧,我不能发表评论,因为我的代表是 48 岁,而不是 50 岁。:(

无论如何,我对你的问题没有实际的答案,但我希望我在这里的内容能帮助你。

  1. 在您的参数部分,您有以下内容:
Conditions:
  Enable:
    !Equals [True, !Ref Reception]
  Disable:
    !Equals [False, !Ref Reception]

1a。这是行不通的。你只需要一个条件语句:

Conditions: # Checks to see if Conditional Values are True
  ReceptionYes: !Equals [ !Ref Reception, True]
  1. 这就是它变得粘稠的地方。

您需要在资源中设置一个条件行,例如(我被卡住的地方,是把这行放在下面的位置):

          !If [ReceptionYes, !Ref mySecurityGroup2, !Ref mySecurityGroup1]

如果为真,则使用第一个 !Ref,否则,使用第二个 !Ref

现在,在理论中,您应该能够做到以下几点:

  mySecurityGroup1:
    Condition: Disable
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      VpcId: !Ref myVPC
      GroupDescription: Security Group for EC2 
      SecurityGroupIngress:
        - IpProtocol: udp
          FromPort: 4114
          ToPort: 4114
          CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: mySecurityGroup
  mySecurityGroup2:
    Condition: Enable
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      VpcId: !Ref myVPC
      GroupDescription: Security Group for EC2
      SecurityGroupIngress:
        - IpProtocol: udp
          FromPort: 5683
          ToPort: 5683
          CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: mySecurityGroup

好吧,如果这不起作用,我希望它能让你更接近答案。 :D

【讨论】:

    猜你喜欢
    • 2020-12-20
    • 2022-08-17
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多