DEV Community

Cover image for 4.Deploy Multiple EC2 Instances with Terraform
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

4.Deploy Multiple EC2 Instances with Terraform

Lab Information

The Nautilus DevOps team wants to provision multiple EC2 instances in AWS using Terraform. Each instance should follow a consistent naming convention and be deployed using a modular and scalable setup.

Use Terraform to:

Create 3 EC2 instances using the count parameter.

Name each EC2 instance with the prefix devops-instance (e.g., devops-instance-1).

Instances should be t2.micro.

The key named should be devops-key.

Create main.tf file (do not create a separate .tf file) to provision these instances.

Use variables.tf file with the following:
    KKE_INSTANCE_COUNT: number of instances.
    KKE_INSTANCE_TYPE: type of the instance.
    KKE_KEY_NAME: name of key used.
    KKE_INSTANCE_PREFIX: name of the instnace.

Use the locals.tf file to define a local variable named AMI_ID that retrieves the latest Amazon Linux 2 AMI using a data source.

Use terraform.tfvars to assign values to the variables.

Use outputs.tf file to output the following:
    kke_instance_names: names of the instances created.
Enter fullscreen mode Exit fullscreen mode

Lab Solutions

1️⃣ main.tf

ONLY resource definitions here

resource "aws_instance" "devops_ec2" {
  count         = var.KKE_INSTANCE_COUNT
  ami           = local.AMI_ID
  instance_type = var.KKE_INSTANCE_TYPE
  key_name      = var.KKE_KEY_NAME

  tags = {
    Name = "${var.KKE_INSTANCE_PREFIX}-${count.index + 1}"
  }
}
Enter fullscreen mode Exit fullscreen mode

2️⃣ variables.tf

Use exact variable names

variable "KKE_INSTANCE_COUNT" {
  type = number
}

variable "KKE_INSTANCE_TYPE" {
  type = string
}

variable "KKE_KEY_NAME" {
  type = string
}

variable "KKE_INSTANCE_PREFIX" {
  type = string
}
Enter fullscreen mode Exit fullscreen mode

3️⃣ locals.tf

Retrieve latest Amazon Linux 2 AMI

data "aws_ami" "amazon_linux_2" {
  most_recent = true

  owners = ["amazon"]

  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }
}

locals {
  AMI_ID = data.aws_ami.amazon_linux_2.id
}
Enter fullscreen mode Exit fullscreen mode

📌 This satisfies:

✅ Data source usage

✅ Latest Amazon Linux 2

✅ Local variable named AMI_ID

4️⃣ terraform.tfvars

Assign values here

KKE_INSTANCE_COUNT  = 3
KKE_INSTANCE_TYPE   = "t2.micro"
KKE_KEY_NAME        = "devops-key"
KKE_INSTANCE_PREFIX = "devops-instance"
Enter fullscreen mode Exit fullscreen mode

5️⃣ outputs.tf

Output instance names

output "kke_instance_names" {
  value = [
    for instance in aws_instance.devops_ec2 :
    instance.tags["Name"]
  ]
}
Enter fullscreen mode Exit fullscreen mode

6️⃣ Terraform Commands (REQUIRED FOR KODEKLOUD)

Run in this exact order:

terraform init

terraform validate

terraform apply 


# Type:

yes
Enter fullscreen mode Exit fullscreen mode


✨ (simplified explanation)

🔹 Why use count?

Instead of writing EC2 3 times, count tells Terraform:
“Create this same resource multiple times.”

Terraform creates:

devops-instance-1
devops-instance-2
devops-instance-3

🔹 Why count.index + 1?

count.index starts at 0

Humans start counting at 1

So we add +1 for clean naming

🔹 Why locals.tf for AMI?

AMI IDs change often

Data source finds the latest Amazon Linux 2

locals stores it once and reuses it

Cleaner + safer than hardcoding

🔹 What happens on terraform apply?

Terraform checks AWS

Finds latest AMI

Creates 3 EC2s

Tags them

Saves everything in state

Outputs instance names


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
📖 More Deep Dives: Whispering Cloud Insights - Read other technical articles
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)