如何将密钥从我的桌面导入到 cloudformation 模板?

How can I import a key from my desktop to the cloudformation template?

对使用 cloudformation 非常陌生,我无法弄清楚将密钥导入实例创建的语法。抱歉,如果此 post 的格式已关闭。假设我有一个名为 MyKey.pem 的密钥保存在我的桌面上。我将如何导入密钥并将其应用到我在模板中创建的实例?此外,如果有人也可以告诉我在模板中实际生成新密钥并应用它的过程,我也将不胜感激。这是我用来创建实例的代码。

Ec2Instance1:

Type: AWS::EC2::Instance

Properties:

  InstanceType: t2.micro

  ImageId: ami-07dcc3822b6f2bdbe

  KeyName: MyKey.pem

遗憾的是,在 CloudFormation (CFN) 中没有直接的方法 可以做到这一点。最简单的方法是使用 AWS 控制台或 CLI 导入密钥,然后在模板中按名称引用它。

但是如果你真的想通过 (CFN) 完成它,你将不得不使用 custom resource. The resource would be in the form of a lambda function which would use AWS SDK to create the key. The private key could come from SSM parameter store for instance where you would upload your key or AWS::SecretsManager::Secret

更新 例如,如果您的密钥对称为 MyKeyPair,那么在 CFN 中您将使用:

  MyEC2Instance: 
    Type: AWS::EC2::Instance
    Properties: 
      ImageId: "ami-79fd7eee"
      KeyName: MyKeyPair