[英]How to get newly created instance id using Terraform
I am creating AWS ec2 instance(s) using auto scaling group and launch template.我正在使用自动缩放组和启动模板创建 AWS ec2 实例。 I would like to get instance ids of the newly launched instances.我想获取新启动实例的实例 ID。 Is this possible?这可能吗? For brevity purpose I have removed some code为了简洁起见,我删除了一些代码
resource "aws_launch_template" "service_launch_template" {
name_prefix = "${var.name_prefix}-lt"
image_id = var.ami_image_id
iam_instance_profile {
name = var.instance_profile
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_lb_target_group" "service_target_group" {
name = "${var.name_prefix}-tg"
target_type = "instance"
vpc_id = var.vpc_id
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "service_autoscaling_group" {
name = "${var.name_prefix}-asg"
max_size = var.max_instances
min_size = var.min_instances
desired_capacity = var.desired_instances
target_group_arns = [aws_lb_target_group.service_target_group.arn]
health_check_type = "ELB"
launch_template {
id = aws_launch_template.service_launch_template.id
version = aws_launch_template.service_launch_template.latest_version
}
depends_on = [aws_alb_listener.service_frontend_https]
lifecycle {
create_before_destroy = true
}
}
resource "aws_alb" "service_frontend" {
name = "${var.name_prefix}-alb"
load_balancer_type = "application"
lifecycle {
create_before_destroy = true
}
}
resource "aws_alb_listener" "service_frontend_https" {
load_balancer_arn = aws_alb.service_frontend.arn
protocol = "HTTPS"
port = "443"
}
This is working.这是工作。 But I would like to output the instance ids of the newly launched instances.但我想 output 新启动实例的实例 ID。 From terraform documentation looks like the aws_launch_template
or aws_autoscaling_group
does not export the instance ids.从 terraform 文档看来, aws_launch_template
或aws_autoscaling_group
不会导出实例 ID。 What are my options here?我在这里有什么选择?
Terraform is probably completing, and exiting, before the auto-scaling group has even triggered a scale-up event and created the instances. Terraform 可能在自动扩展组触发扩展事件并创建实例之前完成并退出。 There's no way for Terraform to know about the individual instances, since Terraform isn't managing those instances, the auto-scaling group is managing them. Terraform 无法了解各个实例,因为 Terraform 不管理这些实例,自动缩放组正在管理它们。 You would need to use another tool, like the AWS CLI, to get the instance IDs.您需要使用其他工具(例如 AWS CLI)来获取实例 ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.