【发布时间】:2018-12-14 10:13:44
【问题描述】:
我已将文件配置器添加到 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 中获取有关该问题的更多信息的最佳方法是什么?有什么我不知道的问题吗?
非常感谢!
【问题讨论】:
-
你想在这里做什么?因为您的代码正在尝试复制文件,但您尚未使用连接块定义将其复制到的位置。通常,您会连接到 Terraform 正在启动的实例,但这不适用于自动缩放组(API 不会直接为您提供有关实例的任何详细信息,即使这样做也只会针对那些初始实例) 并且启动配置是 ASG 将用于创建实例的不可变模板,您不能像那样将内容复制到它。相反,您应该使用 Packer 之类的东西烘焙 AMI。
-
我已经使用打包程序创建了一个 AMI,但我需要将 RDS (aws_db_instance) 资源的计算主机和端口嵌入到 .pg_service.conf 文件中,所以我想动态配置它从这些计算值启动文件。同一个 AMI 用于多个环境,每个环境都有自己的数据库主机地址。所以不可能使用带有launch_configuration的文件配置器?
-
@ydaetskcoR 在进一步研究之后,确实看起来我的方法被误导了。将使用
user_databash 脚本并从中创建文件。谢谢! -
是的,如果需要在实例的生命周期内进行更改,我通常建议使用用户数据之类的东西来配置实例的创建时间配置,或者使用 Consul-Template 之类的东西来配置运行时配置。
-
我在下面添加了一个答案,它使用
userdata以 base64 编码引导文件。这样,您可以按照其他人的建议使用用户数据块,但您可以根据需要将脚本/文件/配置“复制”到服务器。
标签: terraform terraform-provider-aws