Automating AWS EC2 Instance Deployment with Terraform
Introduction
In the world of cloud computing, manual deployment of resources can be time-consuming and prone to errors. Terraform, an Infrastructure as Code (IaC) tool developed by HashiCorp, addresses this issue by enabling users to define, deploy, and manage their infrastructure using simple configuration files. This blog walks you through deploying an AWS EC2 instance using Terraform while explaining the key concepts involved.
What is Terraform?
Terraform is an open-source IaC tool that allows you to manage cloud infrastructure across multiple providers (like AWS, Azure, and Google Cloud) using declarative code.
Declarative Code: You specify what resources you need, and Terraform determines how to provision them.
Benefits:
Automation: Removes manual setup of cloud resources.
Version Control: Infrastructure can be stored and versioned in Git.
Multi-Provider Support: Manage resources across different cloud providers simultaneously.
Step 1: Understanding the Terraform Basics
1. Providers
Providers are plugins that allow Terraform to interact with different cloud platforms. In this example, we are using the AWS provider. Here’s the configuration:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.79.0"
}
}
}
provider "aws" {
region = "ap-south-1" # Mumbai Region
}
Code Breakdown:
source
: Specifies the provider's source, in this case,hashicorp/aws
.version
: Ensures consistency by locking the provider to version5.79.0
.region
: Defines the AWS region where resources will be created.
Step 2: Creating AWS Resources with Terraform
Goal: Create an AWS EC2 instance with a secure SSH connection, default VPC, and custom security rules.
Below is the ec2.tf
file that defines the EC2 instance and related resources.
2. Key Pair Configuration
A key pair ensures secure access to your EC2 instance using SSH. Terraform generates or references an existing key pair.
resource "aws_key_pair" "my_key" {
key_name = "junoon-key"
public_key = file("junoon-key.pub")
}
key_name: The name assigned to the key pair.
public_key: Refers to the public key file stored locally (
junoon-key.pub
).
3. Security Group
A security group acts as a virtual firewall for your EC2 instance, controlling inbound and outbound traffic.
resource "aws_security_group" "my_sg" {
name = "my-security"
description = "This is SG created by TF"
vpc_id = aws_default_vpc.default.id
ingress {
description = "allow access from SSH port 22"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "allow access from HTTP port 80"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
description = "allow all outgoing traffic"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
Ingress Rules: Allow incoming traffic on ports:
22: SSH access for remote login.
80: HTTP access for web traffic.
Egress Rule: Allows all outbound traffic for communication from the instance.
cidr_blocks:
0.0.0.0/0
means the rule applies to all IP addresses (not recommended for production).
4. EC2 Instance
An EC2 instance is a virtual server in AWS. Here’s how Terraform provisions it:
resource "aws_instance" "my_instance" {
ami = "ami-053b12135b2cc8c71"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.my_sg.id]
key_name = aws_key_pair.my_key.key_name
root_block_device {
volume_size = 8
volume_type = "gp3"
}
}
ami: The Amazon Machine Image used to launch the instance. In this example, the ID is for an Ubuntu image in the Mumbai region.
instance_type: Specifies the size of the instance (
t2.micro
is free-tier eligible).root_block_device: Configures storage with an 8GB gp3 volume.
Step 3: Deploying Resources with Terraform
Follow these steps to deploy the resources:
1. Initialize Terraform
terraform init
This downloads the AWS provider plugin and sets up Terraform for your project.
2. Plan Deployment
terraform plan
Terraform generates a plan, showing the resources it will create without applying changes.
3. Apply the Plan
terraform apply
After confirming the prompt, Terraform provisions the EC2 instance and associated resources.
Step 4: Verifying the Deployment
Go to the AWS Management Console and navigate to the EC2 Dashboard.
Verify that the instance is running.
Instance Type:
t2.micro
Region:
ap-south-1
State: Running
Key Terraform Concepts
State Files:
Terraform keeps track of your infrastructure in a state file (terraform.tfstate
). This file must be carefully managed to avoid inconsistencies.Idempotency:
Running the same configuration multiple times will not recreate resources, ensuring efficiency and reliability.Modules:
Reusable configurations in Terraform can be created as modules for scalability. For instance, you can package the EC2 configuration for use across multiple projects.
Benefits of Using Terraform
Simplicity: No need to navigate complex AWS interfaces manually.
Reproducibility: Consistently recreate infrastructure across environments.
Collaboration: Store Terraform code in Git for team collaboration.
Multi-Cloud Support: Manage resources from multiple providers in one configuration.
Conclusion
Terraform transforms cloud infrastructure management by automating resource creation and configuration. In this blog, we demonstrated how to deploy an AWS EC2 instance with secure access and proper traffic rules. By leveraging Terraform, you can standardize your deployments and reduce manual errors, making it an essential tool for modern DevOps practices