0
0
Terraformcloud~15 mins

Variable declaration syntax in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Variable declaration syntax in Terraform
📖 Scenario: You are setting up a Terraform configuration to manage cloud infrastructure. Variables help you customize your setup without changing the main code.
🎯 Goal: Learn how to declare variables in Terraform with default values and descriptions.
📋 What You'll Learn
Declare a variable named region with default value us-west-2
Declare a variable named instance_type with default value t2.micro
Add descriptions to both variables
💡 Why This Matters
🌍 Real World
Variables in Terraform let you customize infrastructure deployments without changing the main code, making it easy to reuse and share configurations.
💼 Career
Understanding variable declaration is essential for cloud engineers and DevOps professionals to write flexible and maintainable infrastructure as code.
Progress0 / 4 steps
1
Declare the region variable
Write a Terraform variable declaration for a variable called region with a default value of "us-west-2" and a description "AWS region to deploy resources".
Terraform
Need a hint?

Use the variable block with description and default inside.

2
Declare the instance_type variable
Add a Terraform variable declaration for a variable called instance_type with a default value of "t2.micro" and a description "EC2 instance type" below the region variable.
Terraform
Need a hint?

Follow the same pattern as the region variable.

3
Use the region variable in a provider block
Add a Terraform provider block for AWS that uses the variable region with the syntax region = var.region.
Terraform
Need a hint?

Use provider "aws" { region = var.region } to connect the variable.

4
Use the instance_type variable in an EC2 resource
Add a Terraform resource block for an AWS EC2 instance named example that uses the variable instance_type with the syntax instance_type = var.instance_type and the ami set to "ami-0c55b159cbfafe1f0".
Terraform
Need a hint?

Use resource "aws_instance" "example" { ami = "ami-..." instance_type = var.instance_type }.