Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to define and manage infrastructure using configuration files. In this blog, we’ll explore how to use Terraform to provision an Amazon EC2 instance, one of the most common use cases for AWS.
Why Use Terraform for AWS EC2 Instances?
Automation: Define your EC2 instance configuration once and reuse it multiple times.
Consistency: Avoid manual errors by maintaining the same infrastructure state across different environments.
Scalability: Easily scale your infrastructure by tweaking configuration files.
Prerequisites
Before we start, ensure the following:
AWS Account: You’ll need access to AWS with programmatic access (AWS Access Key and Secret Key).
Terraform Installed: Install Terraform on your system.
IAM Role: Ensure the role has necessary permissions like EC2FullAccess, VPCFullAccess
Step 1: Set Up Your Terraform Configuration
File Structure
Create a folder for your project and include these files:
provider.tf
: Define the list of provider detailsmain.tf
: Main configuration file.variables.tf
: Define variables.outputs.tf
: Capture useful outputs like the public IP of the EC2 instance.
Provider file:
provider "aws" {
region = "us-east-1"
}
Main.tf
resource "aws_instance" "example" {
tags = {
Name = "MyTerraform-Instance"
}
ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI
instance_type = "t2.micro"
key_name = "your-key-pair"
availability_zone = "us-east-1a"
vpc_security_group_ids = "sg-0abcd1234efgh5678"
root_block_device {
volume_size = 10
}
count = 1
}
Outputs.tf
output "instance_public_ip" {
value = aws_instance.example.public_ip
}
Step 2: Initialize Terraform
Run the following command to initialize Terraform in your project folder:
terraform init
This downloads the necessary providers and sets up your workspace.
Step 3: Plan Your Infrastructure
To preview what Terraform will create, execute:
terraform plan
Review the output to ensure the configurations are as expected.
Step 4: Apply the Configuration
Run the apply
command to provision the EC2 instance:
terraform apply
Terraform will prompt for confirmation. Type yes
to proceed. Once complete, Terraform will output the public IP address of your EC2 instance.
Step 5: Clean Up Resources
When you're done, you can destroy the resources to avoid unnecessary charges:
terraform destroy
Terraform will terminate the EC2 instance and delete associated resources.
Real-Time Use Case
Imagine a scenario where you need to provision multiple EC2 instances for a development environment. Instead of manually launching instances through the AWS Console, you can use Terraform to automate and replicate the infrastructure setup within minutes.
Conclusion
Terraform simplifies cloud infrastructure management, making it a preferred choice for automating EC2 provisioning. By following this guide, you’ve learned how to launch an EC2 instance, customize its configuration, and manage it efficiently.
If you love stories that inspire learning, growth, and productivity, consider subscribing for more! If this article added value to your journey, your support would mean the world to me — only if it’s within your means. Let’s stay connected on LinkedIn too. Thank you for reading!