In this section, we will delve into the concepts of variables and outputs in Terraform. These are fundamental components that help in making your Terraform configurations more dynamic and reusable.
What are Variables?
Variables in Terraform allow you to parameterize your configurations. This means you can define values that can be reused across your configuration files, making them more flexible and easier to manage.
Types of Variables
Terraform supports three types of variables:
- String: A single line of text.
- Number: A numeric value.
- Bool: A boolean value (true or false).
Defining Variables
Variables are defined using the variable
block. Here is an example of defining a string variable:
variable "region" { description = "The AWS region to deploy resources in" type = string default = "us-west-2" }
Using Variables
Once defined, you can use variables in your Terraform configuration by referencing them with the var
keyword:
Variable Files
Variables can also be defined in separate files, typically with a .tfvars
extension. This is useful for managing different sets of variables for different environments (e.g., development, staging, production).
Example variables.tf
:
Example terraform.tfvars
:
What are Outputs?
Outputs in Terraform are used to extract information from your Terraform state files and make it available for other configurations or for external use.
Defining Outputs
Outputs are defined using the output
block. Here is an example of defining an output:
output "instance_id" { description = "The ID of the EC2 instance" value = aws_instance.my_instance.id }
Using Outputs
Outputs can be accessed after running terraform apply
by using the terraform output
command:
Output Sensitivity
You can mark outputs as sensitive to prevent them from being displayed in the CLI output or logs:
output "db_password" { description = "The database password" value = aws_db_instance.my_db.password sensitive = true }
Practical Example
Let's create a simple Terraform configuration that uses variables and outputs.
Step 1: Define Variables
Create a file named variables.tf
:
variable "region" { description = "The AWS region to deploy resources in" type = string default = "us-west-2" } variable "instance_type" { description = "The type of EC2 instance" type = string default = "t2.micro" }
Step 2: Use Variables in Configuration
Create a file named main.tf
:
provider "aws" { region = var.region } resource "aws_instance" "my_instance" { ami = "ami-0c55b159cbfafe1f0" instance_type = var.instance_type }
Step 3: Define Outputs
Add the following to main.tf
:
output "instance_id" { description = "The ID of the EC2 instance" value = aws_instance.my_instance.id }
Step 4: Apply Configuration
Run the following commands to apply the configuration:
After the apply is complete, you can view the output:
Exercises
Exercise 1: Define and Use a Variable
- Define a variable for the EC2 instance's key name in
variables.tf
. - Use this variable in the
aws_instance
resource inmain.tf
.
Solution:
Add to variables.tf
:
variable "key_name" { description = "The key name for the EC2 instance" type = string default = "my-key" }
Modify main.tf
:
resource "aws_instance" "my_instance" { ami = "ami-0c55b159cbfafe1f0" instance_type = var.instance_type key_name = var.key_name }
Exercise 2: Define and Use an Output
- Define an output for the public IP of the EC2 instance in
main.tf
.
Solution:
Add to main.tf
:
output "instance_public_ip" { description = "The public IP of the EC2 instance" value = aws_instance.my_instance.public_ip }
Summary
In this section, we covered the basics of variables and outputs in Terraform. We learned how to define and use variables to make our configurations more dynamic and reusable. We also explored how to define outputs to extract useful information from our Terraform state. These concepts are essential for writing efficient and maintainable Terraform configurations.
Terraform Course
Module 1: Introduction to Terraform
Module 2: Terraform Configuration Language
Module 3: State Management
Module 4: Terraform Modules
Module 5: Provisioning Resources
- Provisioning Basics
- Provisioning AWS Resources
- Provisioning Azure Resources
- Provisioning GCP Resources
Module 6: Advanced Terraform Features
Module 7: Terraform Best Practices
Module 8: Terraform in CI/CD
- Integrating Terraform with CI/CD
- Automating Terraform with Jenkins
- Using Terraform with GitHub Actions
- Terraform Cloud and Enterprise