参数:[KeyPair]必须具有值

时间:2016-11-30 23:28:35

标签: amazon-web-services amazon-cloudformation

我正在尝试使用cloudformation镜像我的EC2实例。目前,我已经能够使用对流层创建以下JSON,并且我遇到了错误'参数:[KeyPair]必须具有值'。我不确定这个'值得'必须?

我理解错误但不确定解决方案是什么。

{
    "Outputs": {
        "InstanceAccess": {
            "Description": "",
            "Value": {
                "Fn::Join": [
                    "",
                    [
                        "ssh -i ",
                        {
                            "Ref": "KeyPair"
                        },
                        " ubuntu@",
                        {
                            "Fn::GetAtt": [
                                "MyInstance",
                                "PublicDnsName"
                            ]
                        }
                    ]
                ]
            }
        }
    },
    "Parameters": {
        "KeyPair": {
            "Description": "jj",
            "Type": "AWS::EC2::KeyPair::launch"
        }
    },
    "Resources": {
        "MyInstance": {
            "Properties": {
                "ImageId": "< my image id goes here>",
                "InstanceType": "t1.micro",
                "KeyName": {
                    "Ref": "KeyPair"
                },
                "SecurityGroups": [
                    {
                        "Ref": "SecurityGroup"
                    }
                ]
            },
            "Type": "AWS::EC2::Instance"
        },
        "SecurityGroup": {
            "Properties": {
                "GroupDescription": "Allow access to MyInstance",
                "SecurityGroupIngress": [
                    {
                        "CidrIp": "0.0.0.0/0",
                        "FromPort": "22",
                        "IpProtocol": "tcp",
                        "ToPort": "22"
                    },
                    {
                        "CidrIp": "0.0.0.0/0",
                        "FromPort": "80",
                        "IpProtocol": "tcp",
                        "ToPort": "80"
                    },
                    {
                        "CidrIp": "0.0.0.0/0",
                        "FromPort": "8080",
                        "IpProtocol": "tcp",
                        "ToPort": "8080"
                    },
                    {
                        "CidrIp": "0.0.0.0/0",
                        "FromPort": "443",
                        "IpProtocol": "tcp",
                        "ToPort": "443"
                    }
                ]
            },
            "Type": "AWS::EC2::SecurityGroup"
        }
    }
}

2 个答案:

答案 0 :(得分:1)

要展开已接受的答案,您可以为参数添加默认值。这也可以解决您看到的错误。如果KeyPair具有静态值,您可能更喜欢默认值而不是传递参数。您仍然可以通过传递参数来覆盖默认值。

"KeyPair": {
    "Description": "The name of the keypair to use for SSH access",
    "Type": "AWS::EC2::KeyPair::KeyName",
    "Default": "launch"
}

答案 1 :(得分:0)

您的KeyPair参数应具有有效类型。根据{{​​3}}的Parameters部分,KeyPair参数的类型是AWS :: EC2 :: KeyPair :: KeyName。所以看起来应该是这样的:

"KeyPair": {
  "Description": "The name of the keypair to use for SSH access",
  "Type": "AWS::EC2::KeyPair::KeyName"
}

此外,如果在模板中将密钥对名称声明为参数,则在使用该模板创建堆栈时,必须将现有密钥对名称作为参数传递。