运行terraform时键的错误

时间:2017-09-07 14:47:05

标签: terraform

运行terraform apply时,我的键出错了。我只是在学习terraform。我似乎无法在线找到解决此问题的解决方案:

   machine:terraform$ terraform apply
   aws_instance.nginix: Creating...
     ami:                          "" => "ami-d1180894"
     associate_public_ip_address:  "" => "<computed>"
     availability_zone:            "" => "<computed>"
     ebs_block_device.#:           "" => "<computed>"
     ephemeral_block_device.#:     "" => "<computed>"
     instance_state:               "" => "<computed>"
     instance_type:                "" => "t2.nano"
     ipv6_address_count:           "" => "<computed>"
     ipv6_addresses.#:             "" => "<computed>"
     key_name:                     "" => "terraform-keys2"
     network_interface.#:          "" => "<computed>"
     network_interface_id:         "" => "<computed>"
     placement_group:              "" => "<computed>"
     primary_network_interface_id: "" => "<computed>"
     private_dns:                  "" => "<computed>"
     private_ip:                   "" => "<computed>"
     public_dns:                   "" => "<computed>"
     public_ip:                    "" => "<computed>"
     root_block_device.#:          "" => "<computed>"
     security_groups.#:            "" => "<computed>"
     source_dest_check:            "" => "true"
     subnet_id:                    "" => "<computed>"
     tenancy:                      "" => "<computed>"
     volume_tags.%:                "" => "<computed>"
     vpc_security_group_ids.#:     "" => "<computed>"
   Error applying plan:

   1 error(s) occurred:

   * aws_instance.nginix: 1 error(s) occurred:

   * aws_instance.nginix: Error launching source instance: InvalidKeyPair.NotFound: The key pair 'terraform-keys2' does not exist
    status code: 400, request id: 993cc401-4c7a-4e4b-9630-71bc4b5729b0

   Terraform does not automatically rollback in the face of errors.
   Instead, your Terraform state file has been partially updated with
   any resources that successfully completed. Please address the error
   above and apply again to incrementally change your infrastructure.

我尝试使用AWS控制台并创建密钥对,但这似乎没有帮助

4 个答案:

答案 0 :(得分:3)

如果出现此错误,请检查您是否在您正在处理的区域中创建了密钥对terraform-keys2

  

aws_instance.nginix:启动源实例时出错:InvalidKeyPair.NotFound:密钥对terraform-keys2不存在   状态代码:400,请求ID:993cc401-4c7a-4e4b-9630-71bc4b5729b0

答案 1 :(得分:1)

在您的计算机上,使用以下ssh-keygen -f terraform-keys2

命令创建密钥对

然后你的配置应该在aws

中有如下所示的资源aws_key_pair
resource "aws_key_pair" "terraform-keys2" {
  key_name = "terraform-keys2"
  public_key = "terraform-keys2.pub"
}

答案 2 :(得分:1)

解决方案:

请运行以下命令,您应该已经安装了aws-cli。

aws ec2 import-key-pair --public-key-material file://~/.ssh/id_rsa.pub --key-name my-key

答案 3 :(得分:0)

如果您在引号中引用 aws_key_pair 资源,也可能会发生此错误,如下所示:

resource "aws_key_pair" "mykey" {
  key_name    = "mykey"
  public_key  = file(var.PATH_TO_PUBLIC_KEY)
}

resource "aws_instance" "example" {
  ami           = var.AMIS[var.AWS_REGION]
  instance_type = "t4g.micro"
  key_name      = "aws_key_pair.mykey.key_name"
  ...
}

我在一个冲动的事故中添加了引号,我花了一段时间才注意到我的 sysntax 错误。所以它应该是这样的:

...
key_name      = aws_key_pair.mykey.key_name
...