Variables and Outputs
Learn to use input variables to make your configurations flexible and reusable, and output values to extract important information from your deployed infrastructure.
Variables and Outputs is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Flexible Configurations
Terraform allows you to define your infrastructure as code. But what if you want to reuse your code for different environments or settings?
This is where variables and outputs come in. They make your configurations flexible and easy to manage.
Input Variables: Flexibility
Input variables act like parameters for your Terraform configurations. They let you customize aspects of your infrastructure without changing the core code.
- Avoids hardcoding values directly in your configuration.
- Makes your modules and configurations reusable.
- Allows different values for different environments (e.g., dev vs. prod).
Declare Your Variables
You declare input variables in a .tf file, often named variables.tf. Each variable needs a unique name.
Here's a basic declaration:
variable "file_content" {
description = "Content to write to a local file."
type = string
}Types and Default Values
Variables can have different types like string, number, bool, list, or map. You can also provide a default value, making the variable optional.
variable "environment" {
description = "The deployment environment (e.g., dev, prod)."
type = string
default = "dev"
}
variable "instance_count" {
description = "Number of instances."
type = number
default = 1
}Using Variables in Code
Once declared, you can reference a variable's value using the var. prefix, followed by its name (e.g., var.environment).
This allows your resources to adapt based on the provided input.
resource "local_file" "example" {
content = "Hello from ${var.environment}!"
filename = "example-${var.environment}.txt"
}How to Provide Values
There are several ways to provide values for your variables:
- Command Line: Using
-var="key=value"withterraform planorapply. .tfvarsFiles: Create a file liketerraform.tfvarsordev.tfvars.- Environment Variables: Using
TF_VAR_name=value.
The terraform.tfvars file is common for setting values:
# terraform.tfvars
environment = "staging"
file_content = "This is some custom content."Output Values: Extract Info
Just as input variables allow you to pass data into your configuration, output values allow you to extract data out of it.
- Display important information about deployed resources (e.g., IP addresses, URLs).
- Pass data between Terraform configurations, especially when using modules.
- Makes it easy to see what Terraform has provisioned.
Declare Your Outputs
You declare output values in a .tf file, often named outputs.tf. Each output needs a unique name and a value argument.
output "file_name" {
description = "The name of the generated local file."
value = local_file.example.filename
}Viewing Output Values
After running terraform apply, you can view the output values using the terraform output command.
The value argument usually references an attribute of a resource, like resource_type.resource_name.attribute.
# After 'terraform apply', run:
terraform output file_nameVariables & Outputs in Action
Let's combine what we've learned. This example uses variables for content and environment, and outputs the filename and content. Create these three files in a directory:
# main.tf
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "~> 2.1"
}
}
}
resource "local_file" "message" {
content = var.greeting_message
filename = "hello_${var.env}.txt"
}
# variables.tf
variable "greeting_message" {
description = "The message content for the file."
type = string
default = "Hello CoddyKit!"
}
variable "env" {
description = "Environment name."
type = string
default = "dev"
}
# outputs.tf
output "generated_filename" {
description = "The full path of the generated file."
value = local_file.message.filename
}
output "file_content_used" {
description = "The content actually written to the file."
value = local_file.message.content
}Quick Check: Variables & Outputs
Which of the following statements about Terraform variables and outputs are TRUE?
Recap: Variables & Outputs
Great job! You've learned how to make your Terraform configurations dynamic:
- Input variables allow you to define parameters for your infrastructure.
- You can provide variable values through CLI,
.tfvarsfiles, or environment variables. - Output values help you extract and display important information about your deployed resources.
Next, you'll dive into how Terraform keeps track of your infrastructure with State Management!
Frequently asked questions
Is the “Variables and Outputs” lesson free?
Yes — the full text of “Variables and Outputs” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Variables and Outputs”?
Learn to use input variables to make your configurations flexible and reusable, and output values to extract important information from your deployed infrastructure. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Variables and Outputs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Providers and Resources
- Variables and Outputs
- State Management Fundamentals
- Modules and Code Reuse