AWS云形成-使用安全组名称创建EC2实例

时间:2018-09-19 14:41:26

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

我正在尝试创建云形成模板来创建EC2实例。我想使用安全组名称代替安全组ID。当我使用它时,出现错误“遇到不受支持的属性SecurityGroup”。通过云形成创建EC2实例时如何使用安全组名称

"Resources": 
{
    "EC2Instance":
    {
        "Type" : "AWS::EC2::Instance",
        "Properties":
        {
            "InstanceType": 
            {
                "Ref": "InstanceType"
            },
            "SecurityGroup":
            [
                {
                    "Ref" : "InstanceSecurityGroup"
                }
            ],
            "KeyName":
            {
                "Ref" : "AWS::Region"
            },
            "ImageId":
            {
                "Ref": "AMI"
            }
        }
    },
    "InstanceSecurityGroup":
    {
        "Type":"AWS::EC2::SecurityGroup",
        "Properties": 
        {
            "GroupDescription": "Enable SSH access via port 22",
            "GroupName": 
            {
                "Fn::FindInMap":
                [
                    "EnvironmentConfig",
                    {
                        "Ref": "Environment"
                    },
                    "SGGroupName"
                ]
            },
            "SecurityGroupIngress":
            [
                {
                    "IpProtocol": "tcp",
                    "FromPort": 22,
                    "ToPort": 22,
                    "CidrIp":"10.252.0.0/16"
                },
                {
                    "IpProtocol": "tcp",
                    "FromPort": 22,
                    "ToPort": 22,
                    "CidrIp":"10.251.0.0/16"
                }           
            ],
            "VpcId": 
            {
                "Fn::FindInMap":
                [
                    "EnvironmentConfig",
                    {
                        "Ref":"Environment"
                    },
                    "VPC"
                ]
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

应该是SecurityGroups而不是SecurityGroup,即

"SecurityGroups" : [{ "Ref" : "InstanceSecurityGroup" }]

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-ec2.html