默认VPC中不存在安全组

时间:2017-12-06 16:17:54

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

我正在通过cloudformation启动p2.xlarge实例,但有时会因错误而失败"安全组在默认VPC中不存在" (不总是)。我认为这可能是一场竞争。

这是我的cloudformation模板:

"Resources":{
"MyInstance":{
    "Type":"AWS::EC2::Instance",
    "Properties":{
        "ImageId":"ami-xxxxxxxx",
        "InstanceType":{
            "Ref":"InstanceType"
        },
        "SecurityGroups":[
            {
                "Ref":"MySecurityGroup"
            }
        ],
        "KeyName":{
            "Ref":"KeyName"
        },
        "UserData":{
            "Fn::Base64":{
                "Fn::Join":[
                    "",
                    [
                        "#!/bin/bash -x\n"
                    ]
                ]
            }
        }
    }
},
"MySecurityGroup":{
    "Type":"AWS::EC2::SecurityGroup",
    "Properties":{
        "GroupDescription":"Enable ports",
        "SecurityGroupIngress":[
            {
                "IpProtocol":"tcp",
                "FromPort":22,
                "ToPort":22,
                "CidrIp":"0.0.0.0/0"
            },
            {
                "IpProtocol":"tcp",
                "FromPort":80,
                "ToPort":80,
                "CidrIp":"0.0.0.0/0"
            },
            {
                "IpProtocol":"tcp",
                "FromPort":443,
                "ToPort":443,
                "CidrIp":"0.0.0.0/0"
            }
        ]
    }
}

1 个答案:

答案 0 :(得分:0)

您需要指定应在EC2实例之前创建安全组。这是通过DependsOn属性完成的。