0
0
Terraformcloud~30 mins

Count for multiple instances in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Count for multiple instances
📖 Scenario: You are setting up a simple cloud infrastructure using Terraform. You want to create multiple identical virtual machines (instances) to handle web traffic.
🎯 Goal: Build a Terraform configuration that creates exactly three virtual machine instances using the count meta-argument.
📋 What You'll Learn
Create a resource block for an AWS EC2 instance named aws_instance.web.
Use the count meta-argument to create exactly 3 instances.
Set the ami attribute to "ami-0c55b159cbfafe1f0".
Set the instance_type attribute to "t2.micro".
💡 Why This Matters
🌍 Real World
Creating multiple virtual machines is common when you want to scale your application to handle more users.
💼 Career
Cloud engineers often use Terraform's count feature to efficiently manage multiple resources without repeating code.
Progress0 / 4 steps
1
DATA SETUP: Define the AWS provider
Write a provider block for AWS with the region set to "us-east-1".
Terraform
Need a hint?

The provider block tells Terraform which cloud provider to use and where.

2
CONFIGURATION: Add the count variable
Create a variable called instance_count with a default value of 3.
Terraform
Need a hint?

Variables help you change values easily without editing resource blocks.

3
CORE LOGIC: Create multiple instances using count
Create a resource block named aws_instance.web that uses count = var.instance_count. Set ami to "ami-0c55b159cbfafe1f0" and instance_type to "t2.micro".
Terraform
Need a hint?

The count argument creates multiple copies of the resource.

4
COMPLETION: Add tags to instances
Add a tags block inside the aws_instance.web resource with Name = "WebServer-${count.index + 1}".
Terraform
Need a hint?

Tags help identify your instances in the cloud console.