参数:[KeyPair] 必须有值

Parameters: [KeyPair] must have values

我正在尝试使用 cloudformation 镜像我的 EC2 实例。目前,我已经能够使用对流层创建以下 JSON,但我遇到了错误 'Parameters: [KeyPair] must have values'。我不确定这个 'values' 是什么?

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

{
    "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"
        }
    }
}

您的 KeyPair 参数应具有有效类型。根据 Parameters section of the CloudFormation User Guide,KeyPair 参数的类型是 AWS::EC2::KeyPair::KeyName。所以它应该是这样的:

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

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

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

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