【问题标题】:getting output of multiple ec2 instances (tag name and Private ip) in Terraform在 Terraform 中获取多个 ec2 实例(标签名称和私有 ip)的输出
【发布时间】:2020-09-19 03:17:45
【问题描述】:

在我的 main.tf 中,我正在创建多个带有 diff 标签的 ec2 实例

 `resource "aws_instance" "AMZ-TRN01-V-APP" {`
  `ami                     = var.linux_ami[var.region]`
  `instance_type           = var.app_instance_type`
  `key_name                = var.linux_key_name`
  `vpc_security_group_ids  = [var.vpc_security_group_ids[var.region]]`
  `subnet_id               = var.subnetid`
  `count                   = var.app_count` 
  `tags = {`
  `Name        = "AMZ-TRN01-V-APP-${format("%02d", count.index + 1)}"`
  `Environment = var.env_tag`
  `}`
  `} `
 `resource "aws_instance" "AMZ-TRN01-V-DB" {`
 `ami                     = var.linux_ami[var.region]`
 `instance_type           = var.db.instance_type`
 `key_name                = var.linux_key_name`
 `vpc_security_group_ids  = [var.vpc_security_group_ids[var.region]]`
 `subnet_id               = var.subnetid`
 `count                   = var.db_count `
 `tags = {`
 `Name        = "AMZ-TRN01-V-DB-${format("%02d", count.index + 1)}"`
 `Environment = var.env_tag`
 ` }`
 `}`

在我的 output.tf 我有

`output "tags" {`
`description = "List of tags of instances"`
`value       = aws_instance.AMZ-TRN01-V-APP.*.tags.Name`
`}`
`output "private_ip" {`
`description = "List of private IP addresses assigned to the instances"`
`value       = aws_instance.AMZ-TRN01-V-APP.*.private_ip`
`}`

如何在同一输出中获取 AMZ-TRN01-V-DB 服务器的标签和私有 IP?

谢谢

【问题讨论】:

  • 为什么要将它们放在同一个输出中?

标签: amazon-web-services amazon-ec2 terraform terraform-provider-aws


【解决方案1】:

您可以将AMZ-TRN01-V-APPAMZ-TRN01-V-DB 的输出组合成一个列表,如下所示:

output "private_ips" {
description = "List of private IP addresses assigned to the DB and APP instances"
value       = [aws_instance.AMZ-TRN01-V-APP.*.private_ip,
               aws_instance.AMZ-TRN01-V-DB.*.private_ip]
}

标签也是如此:

output "tags" {
description = "List of tags of the APP and DB instances"
value       = [aws_instance.AMZ-TRN01-V-APP.*.tags.Name,
               aws_instance.AMZ-TRN01-V-DB.*.tags.Name]
}

【讨论】:

  • 感谢您的回复萨利姆。这有帮助
猜你喜欢
  • 2021-03-06
  • 2020-01-23
  • 2019-01-01
  • 1970-01-01
  • 2020-05-03
  • 2022-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多