AWS CloudFormation 使用现有的安全组

Posted

技术标签:

【中文标题】AWS CloudFormation 使用现有的安全组【英文标题】:AWS CloudFormation use existing security group 【发布时间】:2016-04-18 08:39:24 【问题描述】:

我想在 cloudformation 模板上使用现有的安全组。 现在我有创建 2 个 SG 的模板,

 "InstanceMember1": 
      "Type": "AWS::EC2::Instance",
      "Properties": 
        "SubnetId": 
          "Ref": "privateSubnetA"
        ,
        "SecurityGroupIds": [
          
            "Ref": "MongoSg"
          ,
          
            "Ref": "mongoTrafficSG"
           
        ],
    


"MongoSg": 
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": 
        "GroupDescription": "MongoDB security group",
        "SecurityGroupIngress": [
          
            "IpProtocol": "tcp",
            "FromPort": "22",
            "ToPort": "22",
            "SourceSecurityGroupId": 
              "Ref": "bastionSG"
            
          
        ],
        "VpcId": "%%vpc-id%%",
      


现在我想给实例添加安全组id,有什么建议吗?

【问题讨论】:

【参考方案1】:

您可以直接指定安全组名称: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-securitygroups

 "InstanceMember1": 
      "Type": "AWS::EC2::Instance",
      "Properties": 
        "SubnetId": 
          "Ref": "privateSubnetA"
        ,
        "SecurityGroups": [ "mysuperawesomealreadyexistinggroup"],
    

【讨论】:

如果不明显,SecurityGroup也可以作为参数传入,也可以和安全组在同一个CloudFormation模板中创建。有几种方法可以解决这个问题。 不再可能使用 VPC。 在大量的 cloudformation 组件文档中,我总是很难找到方向。这是文档的链接 - docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/… 此解决方案仅适用于 EC2 经典版。实例位于 VPC(引用子网)中,因此此解决方案在 VPC 中不起作用。 仍然可以在 VPC 中以相同的方式完成:只需将 SecurityGroups 替换为 SecurityGroupId 并在数组中提供一个或多个安全组 ID。但在这种情况下,只有安全组 ID 有效,组名无效。

以上是关于AWS CloudFormation 使用现有的安全组的主要内容,如果未能解决你的问题,请参考以下文章