在 Terraform 中获取多个 EC2 实例(标签名称和私有 IP)的输出



在我的 main.tf 中,我正在创建多个带有差异标签的 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`
` }`
`}`

在我的 outputs.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?

谢谢

您可以将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]
}

相关内容

  • 没有找到相关文章

最新更新