Output of one terraform script to be used as input into another

Hi…

I am creating resources via terraform script example like below :-1:

resource “aws_security_group” “tfsg1” {
name = “tfsg1”
description = “to allow ports for eks cluster”
vpc_id = aws_vpc.tfvpc.id

ingress {
description = “pod port”
from_port = 32004
to_port = 32004
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}

ingress {
description = “container port”
from_port = 8080
to_port = 8080
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}

egress {
from_port = 0
to_port = 0
protocol = “-1”
cidr_blocks = [“0.0.0.0/0”]
ipv6_cidr_blocks = [“::/0”]
}

tags = {
Name = “tfsg1”
}
}

Now created the output.tf to know the security group ID as below :-1:

output “tfsg1” {
value = “${aws_security_group.tfsg1.id}”

}

Now I want to pass this security ID into my variable.tf file

I am not using modules.

Kindly help

You can’t pass it in as a variable, at runtime Terraform compiles all the terraform code and you cannot dynamic modify attributes as you go. In the part of your Terraform where you want to use this output you refer to it as:
aws_security_group.tfsg1.id

but tfsg1 is the name which got hardcoded…what is the otherway round ?

So will not pass it in as variable so now can I use output as input to another resource example I want to use as

security_group_ids = example

security_group_ids = sg-1245690865

Sorry I don’t understand what you are saying.

I mean to say like in eks configuration we can attach additional security group so in eks configuration I want to pass additional security group

so instead of passing aws_security_group.tfsg1.id , I want to pass as security_group_ids = sg-1245690865

where sg-1245690865 come as output when tfsg1 got created

Are you running Terraform twice? That is the only way you can use an output as a variable in terraform.

No will run single time terraform only…but after output come I can sleep the terraform script for few minutes/seconds in meantime it can put inside variable.
If that’s possible need your guidance.

No Terraform does not work like that. Pass the security group as a variable, from the command line when you run Terraform.

1 Like