Introduction.
Creating and managing infrastructure in the cloud has become a crucial aspect of modern DevOps and system administration. Amazon Web Services (AWS) provides a vast range of cloud services, including EC2 instances (virtual servers) and EBS volumes (elastic block storage). When setting up an EC2 instance, it’s often necessary to attach additional storage volumes (EBS) to handle data persistently. Managing this infrastructure manually can be time-consuming and prone to human error, which is where Terraform comes in.
Terraform is an open-source tool that allows you to define and manage infrastructure as code. By writing configuration files, you can automate the provisioning of resources such as AWS EC2 instances and EBS volumes, ensuring consistency, repeatability, and version control. This method reduces the potential for mistakes and helps teams maintain their infrastructure more efficiently.
In this step-by-step guide, we’ll walk you through the process of using Terraform to create an AWS EC2 instance and attach an EBS volume to it. The process includes writing the necessary Terraform configuration files, initializing the project, and running the commands to provision your resources on AWS. We’ll also cover the best practices for setting up Terraform, configuring AWS credentials, and ensuring the configuration is correct.
By the end of this tutorial, you will have learned how to automate the creation of EC2 instances and the attachment of EBS volumes in a secure, scalable, and efficient way. Whether you’re a beginner to Terraform or AWS, this guide will give you a solid understanding of how to work with both services using infrastructure-as-code practices.
In addition to understanding the Terraform syntax and configuration for AWS, this tutorial will also highlight important concepts such as resource dependencies, variable usage, and output management, which are essential for managing infrastructure at scale. Let’s dive in and get started with provisioning AWS EC2 and EBS using Terraform!
STEP 1: Open the folder and create provider.tf file.
- Enter the command and save the file.
provider "aws" {
region = "us-east-1"
}

STEP 2: Create variables.tf file.
variable "instance_type" {
description = "This describes the instance type"
type = string
default = "t2.micro"
}
variable "ami_id" {
description = "This describes the ami image"
type = string
default = "ami-01c647eace872fc02"
}
variable "server_port" {
description = "Server use this port for http requests"
type = number
default = 80
}
variable "ssh_port" {
description = "Describes the ssh port"
type = number
default = 22
}
variable "availability_zone" {
default = "us-east-1a"
}

STEP 3: Create the file and enter the following command.
resource "aws_security_group" "instance" {
name = "terraform-SG"
ingress {
from_port = var.server_port
to_port = var.server_port
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = var.ssh_port
to_port = var.ssh_port
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

STEP 4: Create main.tf file.
resource "aws_instance" "server" {
ami = var.ami_id
instance_type = var.instance_type
vpc_security_group_ids = [ aws_security_group.instance.id ]
availability_zone = var.availability_zone
tags = {
Name = "EC2-Server"
}
user_data = <<-EOF
#!/bin/bash
sudo yum update -y
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
echo "Terraform is easy!!!" > /var/www/html/index.html
EOF
user_data_replace_on_change = true
}

STEP 5: Create ebs_volume.tf file.
resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = aws_ebs_volume.ebs_vol.id
instance_id = aws_instance.server.id
}
resource "aws_ebs_volume" "ebs_vol" {
availability_zone = var.availability_zone
size = 1
}

STEP 6: Create output.tf file.
output "public_ip" {
description = "The public IP address of the web server"
value = aws_instance.server.public_ip
}

STEP 7: Go terminal Enter the aws configure and enter the Access key and secret key.

STEP 8: Enter the following command.
terraform init
terraform plan
terraform apply



Conclusion.
In conclusion, using Terraform to create an AWS EC2 instance and attach an EBS volume offers a streamlined, automated approach to managing cloud infrastructure. By defining your infrastructure as code, you gain consistency, scalability, and repeatability, while reducing the risk of human error. The ability to easily create and configure EC2 instances and EBS volumes ensures that your infrastructure setup is efficient and fully automated, allowing you to focus on other important aspects of your application and environment.
This step-by-step guide has shown how simple it is to write Terraform configuration files, initialize your project, and deploy resources to AWS. By leveraging Terraform’s powerful features like resource dependencies, variables, and outputs, you can further enhance the flexibility and maintainability of your infrastructure.
As you continue working with Terraform, you’ll realize its value in managing more complex cloud resources and environments in a more structured and controlled way. Whether you’re working on small-scale projects or large enterprise infrastructures, Terraform’s capabilities will help you achieve seamless, error-free cloud provisioning and management. By automating the creation of EC2 instances and EBS volumes, you can ensure that your infrastructure remains consistent, cost-effective, and easily replicable across multiple environments.