如何调试Terraform AWS提供程序文件供应商ssh超时问题?

时间:2018-07-05 20:44:10

标签: terraform terraform-provider-aws

我已将文件配置程序添加到aws_launch_configuration资源中,并且看到SSH超时。

1 error(s) occurred:

* module.dev-agooch.module.web.aws_launch_configuration.primary: timeout - last error: dial tcp :22: connect: connection refused

我确保所创建的安全组允许入站ssh通信。

这是我的相关配置:

data "template_file" "pg_service_conf" {
  template = "${file("${path.module}/pg_service_conf.tpl")}"

  vars {
    db_host = "${var.db_host}"
    db_port = "${var.db_port}"
  }
}

resource "aws_launch_configuration" "primary" {
  name_prefix             = "${var.cluster_name}"
  image_id                = "${var.ami}"
  instance_type           = "${var.instance_type}"
  security_groups         = ["${aws_security_group.backend.id}"]
  key_name                = "${var.key_name}"

  user_data = <<-EOF
              #!/bin/bash
              apt-get install nginx -y
              echo "Hello from primary `hostname`" > /var/www/html/index.html
              EOF

  lifecycle {
    create_before_destroy = true
  }

  provisioner "file" {
    content               = "${data.template_file.pg_service_conf.rendered}"
    destination           = "/home/admin/.pg_service.conf"
  }
}

resource "aws_autoscaling_group" "primary" {
  name                    = "${var.cluster_name}-primary"
  launch_configuration    = "${aws_launch_configuration.primary.id}"
  availability_zones      = ["${data.aws_availability_zones.all.names}"]
  target_group_arns       = ["${aws_alb_target_group.frontend.arn}"]
  desired_capacity        = 1
  min_size                = 1
  max_size                = 1
}

resource "aws_security_group" "backend" {
  name                    = "${var.cluster_name}-backend-sg"

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_security_group_rule" "backend_allow_http_inbound" {
  type                    = "ingress"
  security_group_id       = "${aws_security_group.backend.id}"
  from_port               = "${local.http_port}"
  to_port                 = "${local.http_port}"
  protocol                = "tcp"
  cidr_blocks             = ["0.0.0.0/0"]
}

resource "aws_security_group_rule" "backend_allow_ssh_inbound" {
  type                    = "ingress"
  security_group_id       = "${aws_security_group.backend.id}"
  from_port               = "${local.ssh_port}"
  to_port                 = "${local.ssh_port}"
  protocol                = "tcp"
  cidr_blocks             = ["0.0.0.0/0"]
}

resource "aws_security_group_rule" "backend_allow_all_outbound" {
  type                    = "egress"
  security_group_id       = "${aws_security_group.backend.id}"
  from_port               = 0
  to_port                 = 0
  protocol                = "-1"
  cidr_blocks             = ["0.0.0.0/0"]
}

我正在使用Terraform v0.11.7,provider.aws v1.25.0,provider.template v1.0.0版本。

从Terraform获取有关该问题的更多信息的最佳方法是什么?有一些我不知道的陷阱吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,并决定完全不使用用户文件配置程序,而是使用userdata脚本在启动脚本中引导base64编码的文件字符串,然后将它们解码回原始文件。虚拟机文件系统上的文本文件。

根据您的情况,您可以删除文件配置程序,并在userdata元素中添加单行代码:

echo ${base64encode(data.template_file.pg_service_conf.rendered)} | base64 --decode > /home/admin/.pg_service.conf

在您现有代码的上下文中:

# ...
  user_data = <<-EOF
              #!/bin/bash
              echo ${base64encode(data.template_file.pg_service_conf.rendered)} | base64 --decode > /home/admin/.pg_service.conf
              apt-get install nginx -y
              echo "Hello from primary `hostname`" > /var/www/html/index.html
              EOF
# ...

更多信息:Deploy local files to instances without using Terraform file provisioners