Terraform(IAAC)- Cross-referencing attributes

n00πŸ”‘
2 min readDec 2, 2023

Example- creating a security group allowing ingress traffic from a specific elastic IP.

Using the output attribute of one resource as input for other resources!

provider "aws" {
}

resource "aws_eip" "lb" {
domain = "vpc"
}

resource "aws_eip" "lb2" {
domain = "vpc"
}


resource "aws_security_group" "allow_tls" {
name = "allow_tls"
description = "Allow TLS inbound traffic"


ingress {
description = "TLS from VPC"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["${aws_eip.lb.public_ip}/32"]
}

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

tags = {
Name = "allow_tls"
}
}

The line cidr_blocks = ["${aws_eip.lb.public_ip}/32"] is an example of cross-referencing attributes in Terraform. Here, ${aws_eip.lb.public_ip} is used to reference the public IP address of the EIP lb. The /32 at the end is a CIDR notation that represents a subnet mask of 255.255.255.255, indicating that only the specific IP address can access the resource.

Cross-referencing attributes is a powerful feature in Terraform that allows you to use the output of one resource as the input for another. This enables you to create complex, interconnected infrastructures.

--

--