This repository demonstrates how to cross-reference resource attributes in Terraform. Cross-referencing attributes between resources is a common practice in Terraform to create dependencies and use values from one resource to configure another. This example includes a basic setup using AWS resources, but the principles can be applied to other providers as well.
In Terraform, it's often necessary to reference the attributes of one resource in another resource. This is useful for ensuring that resources are created in a specific order and for passing data between resources. This example will show how to:
- Create an AWS VPC.
- Create a subnet within that VPC.
- Launch an EC2 instance in the subnet and associate a security group with it.
- Terraform installed.
- AWS CLI installed and configured with appropriate permissions.
- An AWS account.
-
Clone the repository:
git clone https://github.com/yourusername/terraform-cross-reference-example.git cd terraform-cross-reference-example
-
Initialize the Terraform configuration:
terraform init
-
Plan the infrastructure changes:
terraform plan
-
Apply the infrastructure changes:
terraform apply
-
Destroy the infrastructure (when no longer needed):
terraform destroy
The example configuration includes the following resources:
-
VPC:
- Creates a new VPC.
- Outputs the VPC ID for use in other resources.
-
Subnet:
- Creates a subnet within the VPC.
- References the VPC ID from the VPC resource.
-
Security Group:
- Creates a security group within the VPC.
- Outputs the Security Group ID for use in the EC2 instance.
-
EC2 Instance:
- Launches an EC2 instance in the subnet.
- References the Subnet ID and Security Group ID.
Here is a simplified example of the Terraform code used in this repository:
# VPC
resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
}
# Subnet
resource "aws_subnet" "example" {
vpc_id = aws_vpc.example.id
cidr_block = "10.0.1.0/24"
}
# Security Group
resource "aws_security_group" "example" {
vpc_id = aws_vpc.example.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
# EC2 Instance
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Example AMI ID
instance_type = "t2.micro"
subnet_id = aws_subnet.example.id
security_groups = [aws_security_group.example.name]
}
This repository contains different branches for various Terraform topics. Each branch focuses on a specific topic or feature.
main
branch: Contains the primary example of cross-referencing resource attributes in Terraform.outpu_value
branch: Demonstrates how to use output values in Terraformterraform_variables
branch:
- Instead of using repeated static values, this branch uses variables declared in the
variables.tf
file. - For variable assignment, there are several approaches (environment variables, command line flags, file formats, and variable defaults). Here, the
terraform.tfvars
file is used, which is the best and most common way to manage variables in Terraform. - Adds type constraints (string, number, list, map, etc.) to variables, which is a best practice.
count_parameter
branch:
- Demonstrates the use of the
count
parameter andcount.index
attribute to create multiple resources dynamically. - Creates three different IAM users using the
count
parameter and variable list for user names.
conditional_exp
branch:
- Illustrates the use of conditional expressions in Terraform to dynamically control resource creation based on a variable.
- Creates either three
t2.micro
instances for a development environment or onet2.large
instance for a production environment based on the value of theis_test
variable.
terraform_functions
branch: Documentation for the functions
- Using functions in different categories:
- String Functions:
formatdate()
: Formats a given timestamp according to a specified layout.
- Collection Functions:
lookup()
: Looks up a single value in a map.length()
: Returns the number of elements in a list or map.element()
: Retrieves a single element from a list by index.
- Time Functions:
timestamp()
: Returns the current timestamp in UTC.
- String Functions:
-
data_sources
branch:- Demonstrates the use of data sources in Terraform to fetch information about existing resources.
- Fetches the most recent Ubuntu AMI and uses it to launch an EC2 instance.
-
dynamic_blocks
branch:- Demonstrates the use of dynamic blocks in Terraform to generate repeated configurations.
- Shows a comparison between hardcoded ingress rules and dynamically generated ingress rules for an AWS Security Group.
- Utilizes the
for_each
anditerator
features in dynamic blocks to iterate over a list of ports and create ingress and egress rules.