Home > Blockchain >  Terraform, How to output multiple private ips
Terraform, How to output multiple private ips

Time:08-28

I use "terraform-aws-modules/ec2-instance/aws" to provision two ec2 instances. How to output these two private IPs?

module "ec2_jekins_agent" {
    source = "terraform-aws-modules/ec2-instance/aws"
    version  = "4.1.4"
    for_each = toset(["one", "two"])
    name = "jenkins-agent-${each.key}"
    vpc_security_group_ids = [aws_security_group.sg_jenkins_agent.id]
    subnet_id = data.terraform_remote_state.vpc.outputs.private_subnets[0]
}

The code below does not work.

output "jenkins_agent_private_ips" {
  value = [module.ec2_jekins_agent.private_ip]
}

Error: Unsupported attribute module.ec2_jekins_agent is object with 2 attributes This object does not have an attribute named "private_ip".

CodePudding user response:

Since you've used for_each, you can use values:

output "jenkins_agent_private_ips" {
  value = values(module.ec2_jekins_agent)[*].private_ip
}
  • Related