๐ Unlocking Terraform: ๐ ๏ธ Understanding Variables ๐ and Maximizing Local Values ๐
What are Terraform Variables?
Variables are basic building blocks in the realm of programming languages. It works as a container for transient values that are essential for creating dynamic programs and similar to those in conventional programming languages. They serve as adaptable receptacles that can store different values when a Terraform plan is being carried out.
In Terraform, a variable acts as a container with the capacity to hold a value. Its crucial role lies in representing different values at various stages during the execution of a Terraform plan. Proficiency in harnessing variables within Terraform is essential for crafting infrastructure as code that is dynamic and can be reused effectively.
Before delving into the different variable types, it is beneficial to conceptualize the entire Terraform configuration as a unified function. The parallels the roles of input variables, output variables, and local variables within the Terraform framework.
1. Input Variables :
In Terraform, input variables are parameters that you can use to customize your infrastructure configurations. These variables allow you to parameterize your Terraform code so that you can reuse the same configuration with different values. Here's an example of a simple Terraform configuration with a single input variable:
# main.tf
variable "example_variable" {
description = "An example input variable"
type = string
default = "default_value"
}
resource "example_resource" "example" {
name = var.example_variable
}
In this example:
variable "example_variable"
declares an input variable namedexample_variable
. It has a description, a type (in this case, it's a string), and a default value.resource "example_resource" "example"
is a simple resource block that uses the input variableexample_variable
as the value for thename
attribute.The default value of the input variable is set to "default_value". If you don't provide a value for this variable when running Terraform, it will use the default.
You can then use this Terraform module by creating a separate file (e.g., variables.tfvars
) where you define the actual values for your input variables:
# variables.tfvars
example_variable = "custom_value"
And then, when you run terraform apply
, you can provide this variable file to customize the value of the input variable:
terraform apply -var-file=variables.tfvars
This is a simple example, and in real-world scenarios, you might have more complex configurations with multiple input variables and resources.
2.Output Variables
Output variables allow you to expose specific values from your infrastructure after successfully applying a Terraform configuration. Output variables are useful for capturing and displaying information that might be needed by other parts of your infrastructure or external systems.
To declare an output variable in Terraform, you use the output
block. Here's a simple example:
hclCopy codeoutput "example_output" {
value = "This is an example output variable."
}
In this example:
example_output
is the name of the output variable.The
value
attribute specifies the value associated with this output variable.
You can also reference resources or data sources within the value
attribute to expose specific attributes or information:
hclCopy codeoutput "instance_id" {
value = aws_instance.example_instance.id
}
In this case, the instance_id
output variable will expose the ID of the AWS EC2 instance created using the aws_instance
resource.
Once an output variable is declared, you can use the terraform output
command to view its value after applying the Terraform configuration.
Output variables are particularly useful when you need to share information between different Terraform configurations, modules, or external systems. They provide a way to communicate important details about your infrastructure to other components or users.
What are the Local Values?
Local variables in Terraform are a way to declare and use temporary, named values within a configuration. These local variables are defined using the locals
block, and their values can be referenced throughout the configuration. Local variables are scoped to the module or configuration where they are declared, providing a convenient way to reuse or compute values without repeating expressions.
Here's an example of using local variables in a Terraform configuration:
locals {
ami = "ami-0d26eb3972b7f8c96"
type = "t2.micro"
tags = {
Name = "instances"
Env = "Dev"
}
subnet = "subnet-76a8163a"
nic = aws_network_interface.my_subnet.id
}
resource "aws_instance" "ecinstance" {
ami = local.ami
instance_type = local.type
tags = local.tags
network_interface {
network_interface_id = local.networkid
device_index = 0
}
}
resource "aws_network_interface" "my_networkid" {
description = "My networkid"
subnet_id = local.subnet
tags = {
Name = "networkid"
}
}
In this example, the locals
block defines several local variables, such as ami
, type
, tags
, subnet
, and nic
. These local variables are then used within the aws_instance
and aws_network_interface
resource blocks, providing a cleaner and more maintainable way to manage values.
Local variables in Terraform are especially useful for avoiding redundancy, improving readability, and making it easier to update values consistently across the configuration. They can also be used to compute values or reference other resources, enhancing the flexibility of Terraform configurations.
Subscribe to my newsletter
Read articles from Mamoona Arshad directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Mamoona Arshad
Mamoona Arshad
Hello, I'm Mamoona Arshad, a highly motivated and skilled professional with a Master's degree in Information Technology, with research areas of Machine Learning, Computer Networking, Computer Vision, and deep learning. My research areas have provided me with a solid foundation in cutting-edge technologies and a deep understanding of their applications. My academic journey has sparked a passion for working at the intersection of cloud engineering and DevOps. I am deeply fascinated by the seamless integration of cloud technologies and the efficient deployment of software through DevOps practices. I have been actively learning and gaining hands-on experience in both areas to enhance my skills and contribute to organizations' digital transformation. I possess excellent problem-solving skills, a keen eye for detail, and a strong ability to adapt to new technologies and methodologies. My dedication to continuous learning and my passion for DevOps make me a valuable asset to any organization seeking to streamline their software development processes and improve collaboration between development and operations teams. If you are looking for a dedicated and motivated professional who can contribute to your cloud engineering and DevOps initiatives, I would love to discuss opportunities further. Let's connect and explore how my skills and experience align with your organization's goals. Feel free to reach me at out.