为什么不能使用提供的示例将 SSH 改造成 EC2 实例?

Why can't terraform SSH in to EC2 Instance using supplied example?

我正在使用 AWS Two-tier example 并且我直接复制粘贴了整个内容。 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

最终,它失败了 w/:

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.

我四处搜索,看到一些较旧的 posts/issues 说 flip agent=false 我也尝试过,但没有任何变化或成功。我怀疑这个例子是开箱即用的,但我没有做任何可能破坏它的剪裁或修改。我正在使用通过 OS X 10.10.5.

上的自制软件安装的 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

你有两种可能:

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

    ssh-add ../.ssh/terraform
    

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

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

    secret_key = "../.ssh/terraform"
    

    左右。请查阅文档以了解更具体的语法。

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

connection {
    type = "ssh"
    user = "ec2-user"
    private_key = "${file("*.pem")}"
    timeout = "2m"
    agent = false
}

下面是一个完整的独立 resource "null_resource",带有 remote-exec 配置器和 SSH 连接,包括 ssh 连接类型支持的必要参数:

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

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

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

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

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

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

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

资源 null_resource w/ 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
    }
  }
}
  1. 检查基本映像中存在的用户名。例如,对于 Ubuntu OS,它可以是 ubuntu,对于 AWS 图像可以是 ec2-user
    或者,大多数云提供商允许 Terraform 在 cloud-init 配置的帮助下在第一个实例中创建新用户(检查您的提供商文档):

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

    用户元data.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 手动连接 -v for verbose

ssh -v -i <path_to_private_key/id_rsa> <USER_NAME>@<INSTANCE_IP>