9

我使用的是AWS 两层示例,我直接复制粘贴了整个内容。terraform apply一直工作到它尝试通过 SSH 连接到创建的 EC2 实例的位置。在最终失败之前,它会循环多次给出此输出。

aws_instance.web (remote-exec): Connecting to remote host via SSH...
aws_instance.web (remote-exec):   Host: 54.174.8.144
aws_instance.web (remote-exec):   User: ubuntu
aws_instance.web (remote-exec):   Password: false
aws_instance.web (remote-exec):   Private key: false
aws_instance.web (remote-exec):   SSH Agent: true

最终,它失败了/:

Error applying plan:

1 error(s) occurred:

* ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

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.

我四处搜索,看到一些较旧的帖子/问题说翻转agent=false,我也尝试过,没有任何变化或成功。我怀疑这个例子是开箱即用的,但我没有做任何可能破坏它的剪裁或修改。我在 OS X 10.10.5 上使用通过 homebrew 安装的 terraform 0.6.11。

附加细节:

resource "aws_instance" "web" {
  # The connection block tells our provisioner how to
  # communicate with the resource (instance)
  connection {
    # The default username for our AMI
    user = "ubuntu"

    # The connection will use the local SSH agent for authentication.
    agent = false
  }

  instance_type = "t1.micro"

  # Lookup the correct AMI based on the region
  # we specified
  ami = "${lookup(var.aws_amis, var.aws_region)}"

  # The name of our SSH keypair we created above.
  key_name = "${aws_key_pair.auth.id}"

  # Our Security group to allow HTTP and SSH access
  vpc_security_group_ids = ["${aws_security_group.default.id}"]

  # We're going to launch into the same subnet as our ELB. In a production
  # environment it's more common to have a separate private subnet for
  # backend instances.
  subnet_id = "${aws_subnet.default.id}"

  # We run a remote provisioner on the instance after creating it.
  # In this case, we just install nginx and start it. By default,
  # this should be on port 80
  provisioner "remote-exec" {
    inline = [
      "sudo apt-get -y update",
      "sudo apt-get -y install nginx",
      "sudo service nginx start"
    ]
  }
}

并从变量 tf 文件中:

variable "key_name" {
  description = "Desired name of AWS key pair"
  default = "test-keypair"
}

variable "key_path" {
  description = "key location"
  default = "/Users/n8/dev/play/.ssh/terraform.pub"
}

但我可以用这个命令 ssh:

ssh -i ../.ssh/terraform ubuntu@w.x.y.z
4

4 回答 4

23

你有两种可能:

  1. 将您的密钥添加到您的ssh-agent

    ssh-add ../.ssh/terraform
    

    agent = true在您的配置中使用。这个案子应该适合你

  2. 修改您的配置以直接使用密钥

    secret_key = "../.ssh/terraform"
    

    或者。请查阅文档以获取更具体的语法。

于 2016-02-13T17:22:07.237 回答
3

我有同样的问题,我做了以下配置

connection {
    type = "ssh"
    user = "ec2-user"
    private_key = "${file("*.pem")}"
    timeout = "2m"
    agent = false
}
于 2016-06-02T21:17:57.017 回答
3

以下是带有 SSH 连接的完整且独立的resource "null_resource"配置程序,remote-exec包括 ssh 连接类型支持的必要参数:

  • private_key - 用于连接的 SSH 密钥的内容。这些可以使用 file 函数从磁盘上的文件中加载。如果提供,这将优先于密码。

  • type - 应该使用的连接类型。有效类型为 ssh 和 winrm 默认为 ssh。

  • user - 我们应该用于连接的用户。使用 ssh 类型时默认为 root,使用 winrm 类型时默认为管理员。

  • host - 要连接的资源的地址。这通常由提供者指定。

  • port - 要连接的端口。使用 ssh 类型时默认为 22,使用 winrm 类型时默认为 5985。

  • timeout - 等待连接可用的超时时间。这默认为 5 分钟。应以字符串形式提供,例如 30s 或 5m。

  • agent - 设置为 false 以禁用使用 ssh-agent 进行身份验证。在 Windows 上,唯一受支持的 SSH 身份验证代理是 Pageant。

null_resource带有remote-exec以下示例代码的资源:

resource "null_resource" "ec2-ssh-connection" {
  provisioner "remote-exec" {
    inline = [
      "sudo apt-get update",
      "sudo apt-get install -y python2.7 python-dev python-pip python-setuptools python-virtualenv libssl-dev vim zip"
    ]

    connection {
      host        = "100.20.30.5"  
      type        = "ssh"
      port        = 22
      user        = "ubuntu"
      private_key = "${file(/path/to/your/id_rsa_private_key)}"
      timeout     = "1m"
      agent       = false
    }
  }
}
于 2019-03-21T20:28:32.523 回答
0
  1. 检查基本映像中存在的用户名。例如,它可以ubuntu用于 Ubuntu 操作系统,也可以ec2-user用于 AWS 映像。或者,大多数云提供商允许 Terraform 在cloud-init
    配置 的帮助下在第一个实例上创建新用户(检查您的提供商文档):

    metadata = {
        user-data = "${file("./user-meta-data.txt")}"
    }
    

    用户元数据.txt:

    #cloud-config
    users:
      - name: <NEW-USER-NAME>
        groups: sudo
        shell: /bin/bash
        sudo: ['ALL=(ALL) NOPASSWD:ALL']
        ssh-authorized-keys:
        - ssh-rsa <SSH-PUBLIC-KEY>
    
  2. 增加连接超时设置,有时使用 ssh 启动实例云网络需要 1-2 分钟。

    connection {
       type = "ssh"
       user = "<USER_NAME>"
       private_key = "${file("pathto/id_rsa")}"
       timeout = "3m"
    }
    

如果它不起作用,请尝试通过 ssh 手动连接-vforverbose

ssh -v -i <path_to_private_key/id_rsa> <USER_NAME>@<INSTANCE_IP>
于 2021-04-03T17:15:27.100 回答