通过Cloudformation将多个安全组传递到网络接口时出错

问题描述 投票:0回答:1

我正在尝试构建一个简单的cloudformation模板,该模板创建一个EC2实例和2个网络接口,并将它们附加到Ec2实例。当我将安全组传递给ENI时,我收到的错误是安全组ID确实存在时不存在。

我认为在将安全组转换为字符串列表并将它们传递给AWS :: EC2 :: NetworkInterface的groupSet属性时,问题就出现了。当我只选择一个安全组时,此模板工作正常,但一旦我选择多个SG就不能正常工作。

Cloudformation模板

{
    "AWSTemplateFormatVersion":"2010-09-09",
    "Description":"AWS Cloudformation Sample Template",
    "Parameters":{

        "WebServerSecurityGroup" : {
          "Type" : "List<AWS::EC2::SecurityGroup::Id>",
          "Description" : "The list of security groups in your Virtual Private Cloud (VPC)",
          "ConstraintDescription" : "must be the security group id  in an existing Virtual Private Cloud."
        },
        "Subnet" : {
            "Type" : "AWS::EC2::Subnet::Id",

            "Description" : "The subet in which to launch the instance"
        },


        "InstanceType":{
            "Description":"Webserver EC2 instance type",
            "Type":"String",
            "Default":"t2.small",
            "AllowedValues":[ "t1.micro", "t2.nano", "t2.micro", "t2.small", "t2.medium", "t2.large","t2.2xlarge", "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "m3.medium", "m3.large", "m3.xlarge", "m3.2xlarge", "m4.large", "m4.xlarge", "m4.2xlarge", "m4.4xlarge", "m4.10xlarge", "c1.medium", "c1.xlarge", "c3.large", "c3.xlarge", "c3.2xlarge", "c3.4xlarge", "c3.8xlarge", "c4.large", "c4.xlarge", "c4.2xlarge", "c4.4xlarge", "c4.8xlarge", "g2.2xlarge", "g2.8xlarge", "r3.large", "r3.xlarge", "r3.2xlarge", "r3.4xlarge", "r3.8xlarge", "i2.xlarge", "i2.2xlarge", "i2.4xlarge", "i2.8xlarge", "d2.xlarge", "d2.2xlarge", "d2.4xlarge", "d2.8xlarge", "hi1.4xlarge", "hs1.8xlarge", "cr1.8xlarge", "cc2.8xlarge", "cg1.4xlarge"],
            "ConstraintDescription":"must be a valid EC2 instance type"
        },
        "KeyName":{
            "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances",
            "Type" : "AWS::EC2::KeyPair::KeyName",
            "MinLength": "1",
            "MaxLength": "255",
            "AllowedPattern" : "[\\x20-\\x7E]*",
            "ConstraintDescription" : "can contain only ASCII characters."
        }
    },
    "Resources":{
    "NIC1" : {
      "Type" : "AWS::EC2::NetworkInterface",
      "Properties" : {
        "SubnetId" : { "Ref" : "Subnet" },
        "GroupSet":[ 
            {"Fn::Join": 
            [",", 
            {"Ref": "WebServerSecurityGroup"}
            ]
            } 
            ]
      }
    },
    "NIC2" : {
      "Type" : "AWS::EC2::NetworkInterface",
      "Properties" : {
        "SubnetId" : { "Ref" : "Subnet" },
        "GroupSet":[ 
            {"Fn::Join": 
            [",", 
            {"Ref": "WebServerSecurityGroup"}
            ]
            } 
            ]
      }
    },
    "MyEC2Instance" : {
    "Type" : "AWS::EC2::Instance",
    "Properties" : {
        "ImageId" :"ami-059ab56ffb17ed971",
        "KeyName" : { "Ref" : "KeyName" },
        "InstanceType" : { "Ref" : "InstanceType" }, 
        "NetworkInterfaces" : [
          { "NetworkInterfaceId" : { "Ref" : "NIC1" }, "DeviceIndex" : "0" },
          { "NetworkInterfaceId" : { "Ref" : "NIC2" }, "DeviceIndex" : "1" }
        ]

    }
}

}
}

错误

Error Log

请帮忙。

amazon-web-services amazon-cloudformation
1个回答
2
投票

由于您的参数已经是一个列表,因此您不再需要join。请在下面找到模板

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "AWS Cloudformation Sample Template",
  "Parameters": {
    "WebServerSecurityGroup": {
      "Type": "List<AWS::EC2::SecurityGroup::Id>",
      "Description": "The list of security groups in your Virtual Private Cloud (VPC)",
      "ConstraintDescription": "must be the security group id  in an existing Virtual Private Cloud."
    },
    "Subnet": {
      "Type": "AWS::EC2::Subnet::Id",
      "Description": "The subet in which to launch the instance"
    },

    "InstanceType": {
      "Description": "Webserver EC2 instance type",
      "Type": "String",
      "Default": "t2.small",
      "AllowedValues": [
        "t1.micro",
        "t2.nano",
        "t2.micro",
        "t2.small",
        "t2.medium",
        "t2.large",
        "t2.2xlarge",
        "m1.small",
        "m1.medium",
        "m1.large",
        "m1.xlarge",
        "m2.xlarge",
        "m2.2xlarge",
        "m2.4xlarge",
        "m3.medium",
        "m3.large",
        "m3.xlarge",
        "m3.2xlarge",
        "m4.large",
        "m4.xlarge",
        "m4.2xlarge",
        "m4.4xlarge",
        "m4.10xlarge",
        "c1.medium",
        "c1.xlarge",
        "c3.large",
        "c3.xlarge",
        "c3.2xlarge",
        "c3.4xlarge",
        "c3.8xlarge",
        "c4.large",
        "c4.xlarge",
        "c4.2xlarge",
        "c4.4xlarge",
        "c4.8xlarge",
        "g2.2xlarge",
        "g2.8xlarge",
        "r3.large",
        "r3.xlarge",
        "r3.2xlarge",
        "r3.4xlarge",
        "r3.8xlarge",
        "i2.xlarge",
        "i2.2xlarge",
        "i2.4xlarge",
        "i2.8xlarge",
        "d2.xlarge",
        "d2.2xlarge",
        "d2.4xlarge",
        "d2.8xlarge",
        "hi1.4xlarge",
        "hs1.8xlarge",
        "cr1.8xlarge",
        "cc2.8xlarge",
        "cg1.4xlarge"
      ],
      "ConstraintDescription": "must be a valid EC2 instance type"
    },
    "KeyName": {
      "Description": "Name of an existing EC2 KeyPair to enable SSH access to the instances",
      "Type": "AWS::EC2::KeyPair::KeyName",
      "MinLength": "1",
      "MaxLength": "255",
      "AllowedPattern": "[\\x20-\\x7E]*",
      "ConstraintDescription": "can contain only ASCII characters."
    }
  },
  "Resources": {
    "NIC1": {
      "Type": "AWS::EC2::NetworkInterface",
      "Properties": {
        "SubnetId": { "Ref": "Subnet" },
        "GroupSet": { "Ref": "WebServerSecurityGroup" }
      }
    },
    "NIC2": {
      "Type": "AWS::EC2::NetworkInterface",
      "Properties": {
        "SubnetId": { "Ref": "Subnet" },
        "GroupSet": { "Ref": "WebServerSecurityGroup" }
      }
    },
    "MyEC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": "ami-059ab56ffb17ed971",
        "KeyName": { "Ref": "KeyName" },
        "InstanceType": { "Ref": "InstanceType" },
        "NetworkInterfaces": [
          { "NetworkInterfaceId": { "Ref": "NIC1" }, "DeviceIndex": "0" },
          { "NetworkInterfaceId": { "Ref": "NIC2" }, "DeviceIndex": "1" }
        ]
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.