CloudFormation 模板不选择密钥对

Jag*_*886 0 amazon-ec2 amazon-web-services aws-cloudformation aws-cloudformation-custom-resource

我使用以下方法成功创建了以下四个资源Cloud Formation Template (CFT)

  1. 专有网络
  2. 子网
  3. 互联网网关
  4. 连接网关

现在,我正在尝试使用 EC2 实例创建一个安全组,这是代码。

  Type: AWS::EC2::SecurityGroup
  Properties:
      GroupDescription: Allow http and ssh to client host
      VpcId:
         Ref: InsuranceVPC
      SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: 80
        ToPort: 80
        CidrIp: 0.0.0.0/0
      - IpProtocol: tcp
        FromPort: 22
        ToPort: 22
        CidrIp: 0.0.0.0/0
      SecurityGroupEgress:
      - IpProtocol: tcp
        FromPort: 80
        ToPort: 80
        CidrIp: 0.0.0.0/0

Ec2Instance: 
  Type: AWS::EC2::Instance
  Properties: 
    ImageId: "ami-08706cb5f68222d09"
    KeyName: 
      Ref: "DevOpsAutomation"
    NetworkInterfaces: 
      - AssociatePublicIpAddress: "true"
        DeviceIndex: "0"
        GroupSet: 
          - Ref: "InsuranceSecurityGroup"
        SubnetId: 
          Ref: "InsuranceSubnet"
Run Code Online (Sandbox Code Playgroud)

但是,当我在(CFT,如上所示,代码)中使用 Key 参数(这是我在资源的同一区域中存在的密钥)时,我的 CFT 堆栈失败并出现以下错误:

Template format error: Unresolved resource dependencies [DevOpsAutomation] in the Resources block of the template note: DevOpsAutomation is my keyname

我验证的步骤:

  1. CFT模板资源与key在同一个区域
  2. 删除并新创建的密钥对
  3. 尝试使用不同的密钥对
  4. 我在任何地方都找不到将密钥与 CFT 堆栈一起导入的选项,以便我的 EC2 实例可以使用它。
  5. 即使在创建堆栈时,密钥也不会出现在堆栈部分中(在部分中可见keypair) 。parameter

我的问题是,我应该如何使用我的 AWS 账户中存在的密钥对创建 EC2 实例(作为 CFT 的一部分)?

Den*_*aub 5

删除Ref键名称前面的 。Ref用于引用已定义为 CloudFormation 模板一部分的其他资源。如果密钥对已经存在,则只需使用密钥名称即可。

KeyName: "DevOpsAutomation"
Run Code Online (Sandbox Code Playgroud)