0
0
Terraformcloud~30 mins

Module inputs (variables) in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Terraform Module Inputs with Variables
📖 Scenario: You are creating a reusable Terraform module to deploy a simple AWS EC2 instance. To make the module flexible, you will use variables as inputs.
🎯 Goal: Build a Terraform module that accepts input variables for instance_type and ami_id, and uses them to configure an AWS EC2 instance resource.
📋 What You'll Learn
Create variables for instance_type and ami_id with default values
Use the variables in the aws_instance resource configuration
Output the instance ID after deployment
💡 Why This Matters
🌍 Real World
Terraform modules often use variables to make infrastructure reusable and configurable for different environments or projects.
💼 Career
Understanding how to use module inputs is essential for cloud engineers and DevOps professionals to write flexible and maintainable infrastructure as code.
Progress0 / 4 steps
1
Create variables for instance type and AMI ID
Create two variables in Terraform: instance_type with default value t2.micro, and ami_id with default value ami-12345678. Use the variable block syntax.
Terraform
Need a hint?

Use variable "name" {} blocks with default values.

2
Add provider configuration
Add the AWS provider configuration block with region set to us-east-1. Use the provider "aws" block.
Terraform
Need a hint?

Use provider "aws" { region = "us-east-1" } to set the AWS region.

3
Create the AWS EC2 instance resource using variables
Create an aws_instance resource named example that uses the variables instance_type and ami_id for its instance_type and ami attributes respectively.
Terraform
Need a hint?

Use var.instance_type and var.ami_id to reference variables inside the resource.

4
Add output for the instance ID
Add an output named instance_id that outputs the id attribute of the aws_instance.example resource.
Terraform
Need a hint?

Use output "name" { value = resource.attribute } syntax.