【问题标题】:Why can't terraform SSH in to EC2 Instance using supplied example?为什么不能使用提供的示例将 SSH 改造成 EC2 实例?
【发布时间】:2016-05-24 16:51:16
【问题描述】:

我正在使用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

最终,它失败了:

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

【问题讨论】:

  • 你能用普通的ssh连接吗?您的代理中有密钥吗?
  • 我可以手动 ssh。我不确定我是否理解你的第二个问题,所以答案可能是“不”。你能解释一下吗?
  • 请用信息更新问题如何你可以从命令行ssh 以及你如何进行身份验证。
  • 添加了一些信息。谢谢!

标签: amazon-web-services ssh amazon-ec2 sdn terraform


【解决方案1】:
  1. 检查基本映像中存在的用户名。例如,对于 Ubuntu 操作系统,它可以是 ubuntu,对于 AWS 映像,它可以是 ec2-user
    或者,大多数云提供商允许 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 手动连接 -v for verbose

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

【讨论】:

    【解决方案2】:

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

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

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

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

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

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

    • 超时 - 等待连接可用的超时时间。这默认为 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
        }
      }
    }
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        你有两种可能:

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

          ssh-add ../.ssh/terraform
          

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

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

          secret_key = "../.ssh/terraform"
          

          左右。有关更具体的语法,请参阅文档。

        【讨论】:

        • @NathanHinchey 随意使用投票选出对您有用并帮助您的答案。
        • @Jakuje 我发表评论是因为这个答案已经超过 2 年了,所以我想确认这个解决方案仍然与当前/近期的读者相关
        • 如果你已经设置了 ssh-agent 你可能只需要ssh-add
        • @PaulOdeon ssh-add 仅在默认位置 (id_(rsa|dsa|ecdsa|ed25519)) 添加键,而 OP 显然不是。
        • @Jakuje 你是对的,虽然这不适用于我,谷歌把我带到了这里:)
        猜你喜欢
        • 2019-11-20
        • 1970-01-01
        • 2020-05-29
        • 2021-03-30
        • 2016-03-07
        • 1970-01-01
        • 1970-01-01
        • 2023-01-30
        • 2014-08-17
        相关资源
        最近更新 更多