【问题标题】:Terraform fails with timeout creating aws resourcesTerraform 因创建 aws 资源超时而失败
【发布时间】:2017-10-30 10:27:34
【问题描述】:

我想使用一个 terraform 脚本来创建一个 ec2 实例。我还希望能够使用 SSH 和在 AWS 控制台中创建的密钥对连接到机器。

这是脚本的相关部分:

resource "aws_instance" "web" {
  instance_type = "t2.micro"
  ami = "${lookup(var.aws_amis, var.aws_region)}"

  key_name = "${var.key_name}" # this is my keypair
  connection {
    type = "ssh"
    user = "${var.ssh_user}"
  }
  vpc_security_group_ids = ["${aws_security_group.default.id}"]
  subnet_id = "${aws_subnet.default.id}"
  provisioner "remote-exec" {
    inline = [
      "sudo apt-get -y update"
    ]
  }
}

然后,5 分钟后得到一个 timeout,表明 terraform 无法连接到 ec2 实例

aws_instance.web: Still creating... (5m20s elapsed)
aws_instance.web (remote-exec): Connecting to remote host via SSH...
aws_instance.web (remote-exec):   Host: 34.250.66.198
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:

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

* timeout

但是,即使 terraform 文件超时

  • 所有资源实际上创建成功
  • 我可以在 AWS 控制台上列出它们
  • 我可以通过 SSH + pem 文件手动连接到实例而没有任何问题(甚至在超时错误之前):ssh -i "aws.pem" ubuntu@instance_ip

此信息可能相关: * terraform 版本:v0.10.7 * 地区:eu-west-1 * 操作系统:El Capitan 10.11.6

以防万一这就是我映射 amis 的方式

variable "aws_amis" {
  default = {
    eu-west-1 = "ami-674cbc1e"
    us-east-1 = "ami-1d4e7a66"
    us-west-1 = "ami-969ab1f6"
    us-west-2 = "ami-8803e0f0"
  }
}

非常感谢您的帮助

【问题讨论】:

    标签: amazon-ec2 terraform


    【解决方案1】:

    您需要在connection 中指定私钥,如下所示:

    provisioner "remote-exec" {
        inline = [
            "sudo apt-get -y update"
        ]
        connection {
            private_key = "${file("/location/of/the/pem/file")}"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多