0
0
Terraformcloud~30 mins

Why variables make configurations reusable in Terraform - See It in Action

Choose your learning style9 modes available
Why variables make configurations reusable
📖 Scenario: You are setting up a cloud server configuration using Terraform. You want to make your setup easy to change for different projects without rewriting the whole code.
🎯 Goal: Build a Terraform configuration that uses variables to make the server setup reusable for different environments.
📋 What You'll Learn
Create a variable for the server name
Create a variable for the server instance type
Use the variables in the resource configuration
Set default values for the variables
💡 Why This Matters
🌍 Real World
Cloud engineers use variables in Terraform to create flexible infrastructure code that works for many projects.
💼 Career
Knowing how to use variables is essential for writing reusable and maintainable infrastructure as code.
Progress0 / 4 steps
1
Create variables for server name and instance type
Create two variables in Terraform: server_name with default value my-server and instance_type with default value t2.micro.
Terraform
Need a hint?

Use variable blocks with default values for both variables.

2
Add a resource using the variables
Add an aws_instance resource named example that uses the variables server_name for tags.Name and instance_type for instance_type.
Terraform
Need a hint?

Use var.instance_type and var.server_name inside the resource block.

3
Add a variable for the AMI ID
Create a variable called ami_id with default value ami-0c55b159cbfafe1f0 and update the aws_instance resource to use var.ami_id for the ami attribute.
Terraform
Need a hint?

Define the ami_id variable and replace the hardcoded AMI in the resource with var.ami_id.

4
Add an output for the server ID
Add an output called server_id that outputs the id attribute of the aws_instance.example resource.
Terraform
Need a hint?

Use an output block with the value set to aws_instance.example.id.