Variables, Outputs, and Locals
# CHAPTER 6
Variables, Outputs, and Locals
1. Introduction
Hardcoding values—likeinstance_type = "t2.micro"—directly into your main.tf file is an anti-pattern. If you want to deploy the exact same architecture to a "Staging" environment (using cheap micro servers) and a "Production" environment (using massive enterprise servers), you would have to copy and paste the entire codebase and change every single line manually. This defeats the purpose of automation. In this chapter, we will learn how to make our Terraform code dynamic, flexible, and reusable by utilizing Input Variables to accept data, Output Values to return data, and Local Values to reduce repetition.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Define and use Input Variables (
variableblock) to parameterize configurations.
- Understand variable typing and validation.
-
Extract useful data from Terraform using Output Values (
outputblock).
-
Utilize Local Values (
localsblock) to calculate and reuse internal logic.
-
Pass variables securely via
.tfvarsfiles or command-line arguments.
3. Beginner-Friendly Explanation
Imagine a coffee machine.- The Input Variable: The buttons on the front. Do you want "Small" or "Large"? Do you want "Dark Roast" or "Light Roast"? The machine's internal mechanics don't change, but the *inputs* change the final result.
- The Output Value: The digital screen that says "Your coffee is ready at Spout 2." It gives you the information you need *after* the machine finishes its job.
- The Local Value: The machine's internal memory. It calculates "Small + Dark Roast = 30 seconds of brewing." You never see this calculation; it just helps the machine stay organized internally so it doesn't have to recalculate the math every time.
4. Input Variables (variable block)
Variables are usually defined in a file named variables.tf.
Now, in your main.tf, you replace the hardcoded string with the variable reference using the var. prefix:
5. Output Values (output block)
When Terraform builds a server, AWS assigns it a random Public IP address. How do you find out what that IP is without logging into the AWS Console? You use an Output! Outputs are defined in outputs.tf.
When you run terraform apply, the very last thing the terminal will print is:
Outputs: website_url = "192.168.1.50"
6. Mini Project: Build Reusable Infrastructure Template
Let's uselocals to create a standard naming convention for our servers, ensuring every server name includes the environment and project name.
Step-by-Step Architecture Concept:
7. How to Pass Variable Values
If a variable doesn't have adefault, Terraform will prompt you to type the value in the terminal when you run plan. This is annoying.
The Solution: Use a .tfvars file. Create a file named terraform.tfvars:
Terraform automatically loads any file named terraform.tfvars and uses those values to run the code.
8. Real-World Scenarios
A company had a strict security policy requiring all cloud resources to be tagged with the department name, the environment, and the cost-center code. Developers kept forgetting to add these tags, leading to auditing nightmares. The DevOps team refactored the Terraform code using alocals block. They defined a commontags map containing all the mandatory tags based on input variables. In the resource blocks, developers simply wrote tags = local.commontags. This eliminated the repetition and ensured 100% tagging compliance across the entire infrastructure.
9. Best Practices
-
Variable Validation: You can force Terraform to reject bad inputs. In your
variableblock, add avalidationblock to ensureserversizeis only allowed to be"t2.micro"or"t3.large". If a developer tries to deploy an expensive"p3.16xlarge"server, Terraform will reject the code and halt the deployment immediately.
10. Security Recommendations
-
Sensitive Variables: If an input variable is a database password, add
sensitive = trueto the variable block. When Terraform runs, it will mask the value as(sensitive value)in the terminal output, preventing passwords from leaking into the CI/CD logs.
11. Exercises
- 1. What is the syntax difference between referencing an Input Variable versus referencing a Local Value in an HCL resource block?
-
2.
Why is using a
terraform.tfvarsfile preferred over typing variables interactively into the terminal?
12. FAQs
Q: Can I use Environment Variables in my Linux terminal to pass data to Terraform? A: Yes! If you export an environment variable prefixed withTFVAR, Terraform will automatically pick it up. For example, export TFVARserversize="t2.micro" will inject that value into the server_size variable in your code.
13. Interview Questions
-
Q: Differentiate the operational use cases for Input Variables (
variable), Local Values (locals), and Output Values (output) within a Terraform module.
-
Q: You have a variable that accepts an API token. How do you configure the HCL to ensure this token is never printed in plain text to the console during a
terraform planexecution?