You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform 1.2.8中如何从对象输出提取指定实例公网IP?

Terraform提取模块输出中指定实例的public_ip(v1.2.8)

你的模块输出结构如下:

ec2_multiple_instances = {
  "one" = {
    "private_dns" = "aaaaa.bbbb.cccc"
    "private_ip" = "xx.xx.xx.xx"
    "public_dns" = "ec2-xx-xx-xx-xx.compute.amazonaws.com"
    "public_ip" = "xx.xx.xx.xx"
    "spot_bid_status" = ""
    "spot_instance_id" = ""
    "spot_request_state" = ""
  }
  "two" = {
    "private_dns" = "xxxxx.yyyyy.zzzzz"
    "private_ip" = "yy.yy.yy.yy"
    "public_dns" = "ec2-yy-yy-yy-yy.compute.amazonaws.com"
    "public_ip" = "yy.yy.yy.yy"
    "spot_bid_status" = ""
    "spot_instance_id" = ""
    "spot_request_state" = ""
  }
}

要提取指定实例的public_ip并定义为单独的输出,直接在Terraform配置中添加output块即可,Terraform 1.2.8支持直接通过嵌套对象的属性路径访问值:

output "ec2_one_public_ip" {
  description = "Public IP of instance 'one'"
  value       = module.ec2_multiple_instances.one.public_ip
}

output "ec2_two_public_ip" {
  description = "Public IP of instance 'two'"
  value       = module.ec2_multiple_instances.two.public_ip
}

添加完成后执行terraform apply应用配置,之后可以通过terraform output ec2_one_public_ip或terraform output ec2_two_public_ip单独查看对应实例的公网IP,也可以直接执行terraform output查看所有输出结果。

内容的提问来源于stack exchange,提问作者Nightt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 11:45:38