简体   繁体   English

如何使用 Terraform 获取新创建的实例 ID

[英]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_templateaws_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.

相关问题 如何获取新创建的 aws_cloudformation_stack 的 arn terraform - How to get the arn of a newly created aws_cloudformation_stack terraform 使用 Terraform 将文件传递给新创建的 ec2 实例,而不共享“连接”部分中的私钥 - using Terraform to pass a file to newly created ec2 instance without sharing the private key in "connection" section 如何使用 Python 使用“Cloud SQL Admin API”获取新创建的备份的 ID? - How to get the ID of newly created backup using "Cloud SQL Admin API" with Python? 如何在 Firestore 中的交易期间获取新创建文档的 ID? - How to get the ID of a newly created document during a Transaction in Firestore? 如何动态获取terraform中创建的rds实例的server IP - how to dynamically get the server IP of rds instance created in terraform 无法访问使用 terraform 创建的 EC2 实例 - unable to access EC2 instance created using terraform 如何使用 CDKTF 创建的 terraform 部署 Lambda? - How to deploy Lambda using terraform created by CDKTF? 如何在 Terraform 中获取 AWS RDS 编写器实例 - How I can get the AWS RDS writer instance in Terraform 如何使用 Terraform 获取 AWS 账户名? - How to get the AWS Account Name using Terraform? 如何使用 terraform 将 ssh 密钥添加到 GCP 实例? - How to add an ssh key to an GCP instance using terraform?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM