如何将ssh密钥对与Amazon CloudFormation中由堆栈创build的实例相关联?

以前我已经创build了一个基于图像的EC2实例。 在创build过程中,亚马逊向导创build了一个密钥对,然后提供给我,以便我可以连接。

现在我正在尝试使用CloudFormation,以便我的新服务器安装了一些基本软件(LAMP堆栈)。 但是EC2实例最终没有find可用的ssh密钥对。

有没有办法在CloudFormation上创build堆栈时关联密钥对?

我读过,有办法通过停止实例,创build一个克隆,以及其他一些步骤来将键添加到实例的卷。 但是我在这个领域的pipe理技能并不是很强,所以我希望对我来说更直接一些。

希望这是有道理的 – 任何帮助非常感谢!

有没有办法在CloudFormation上创build堆栈时关联密钥对?

当然,通过在这个过程中关联你现有的一对密钥, AWS CloudFormation示例模板包含相应的片段,例如Simple EC2实例示例包含您正在查找的片段:

"Parameters" : { "KeyName" : { "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance", "Type" : "String" } }, [...] "Resources" : { "Ec2Instance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "KeyName" : { "Ref" : "KeyName" }, "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]}, "UserData" : { "Fn::Base64" : "80" } } } }, 

这个片段可以将所需的密钥对名称作为parameter passing,也可以直接embedded名称或简单地提供默认名称。

祝你好运!

AWS CloudFormation参数还可以为您提供您的账户和地区所有可用密钥的列表。 只需将参数的“types”更改为所需的AWStypes即可。 这将是“AWS :: EC2 :: KeyPair :: KeyName”。

使用“CloudFormation参数types”,上面的例子是:

 "Parameters" : { "KeyName" : { "Description" : "EC2 KeyPair to enable SSH access to the instance", "Type" : "AWS::EC2::KeyPair::KeyName" }, }, [...] "Resources" : { "Ec2Instance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "KeyName" : { "Ref" : "KeyName" }, "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]}, "UserData" : { "Fn::Base64" : "80" } } } }, 

我希望这有帮助。

另见: https : //blogs.aws.amazon.com/application-management/post/Tx3DV2UYG9SC38G/Using-the-New-CloudFormation-Parameter-Types