Open In App

How To Create EBS Volume In AWS Using Terraform

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

EBS Stands for Elastic Block Storage is a block-level storage service provided by Amazon web services to use with Amazon’s Elastic Compute Cloud (EC2) instances.It provides persistent, high-performance storage volumes that can be attached to Amazon EC2 instances. it acts as an attached external hard drive to your instances. These EBS Volumes can be dynamically scaled to accommodate changing storage requirements. users can increase the size of a volume without detaching it from the instance, enabling them to scale storage capacity on-demand as they need.

What Is Terraform?

Terraform is an open-source Infrastructure as Code (IaC) tool created by HashiCorp. It allows DevOps teams to define and provision cloud infrastructure resources across multiple cloud providers and on-premises environments. Terraform is widely used IaC tool because of its key advantage it can be easily integrated with multiple cloud providers like AWS, Azure and GCP. The key idea for developing Terraform is to make it easy to define, provision, and manage cloud infrastructure using code, in a consistent way across different cloud environments.

Features Of Terraform

The following are the features of Terraform:

  • Infrastructure as Code: Terraform allows to define your infrastructure (like virtual machines, networks, databases, etc.) using code instead of clicking around in a web console. This code can be version-controlled, shared, and reused.
  • Multi-Cloud Integration: Terraform can easily integrated with multiple cloud providers (AWS, Azure, Google Cloud, etc.) as well as on-premises infrastructure. Organizations can use the same Terraform code to manage resources across different clouds.
  • Automatic Dependencies Configuration: Terraform automatically understands dependencies between resources and creates or updates them in the correct order, without having to manually manage those dependencies.
  • Execution Plan: Before making any changes, Terraform generates an execution plan that shows what it will do (create, update, or delete resources). we can review this plan and approve it before applying the changes.
  • Tracking Of Resources: Terraform keeps track of the resources it manages in a state file. This state file acts as a source of truth, so Terraform knows what already exists and what needs to be changed.
  • Consistent Workflow: Terraform has a consistent workflow (init, plan, apply) that you use to manage your infrastructure, regardless of the cloud provider we are using or resources involved.

Important Terraform Commands

Here are the essential Terraform commands which commonly used:

  • terraform init: This command initializes the current directory for use with Terraform.
  • terraform plan: This command creates an execution plan based on the current configuration files. It shows you what actions Terraform will take (create, update, or delete resources) when you apply the changes.
  • terraform apply: This command applies the changes described in the execution plan.
  • terraform show: This command shows the current state of resources managed by Terraform.
  • terraform output: This command displays the output values from the configuration files.
  • terraform validate: This command validates the syntax of the Terraform configuration files.

Step by Step Guide To Create EBS Volume In AWS Using Terraform

Step 1: Open the Amazon EC2 console and log in with your credentials.

Step 2: Go to EC2 Console and launch one Linux EC2 instance to perform this practical

Step 3: Login to the EC2 instance and Firstly Configure your AWS Credentials using AWS Configure command.

Note: Make sure you installed AWS CLI to perform this step

/usr/local/bin/aws configure

Enter necessary fields like access key, secret key etc. of AWS account after entering the command

Step 4: After Configuring credentials now we need to install Terraform follow article Setup Terraform On Linux and Windows Machine

Step 5: After completion of installing terraform create one terraform file using vi command as below:

vi main.tf
  • write following code into that file which contains provider, EBS Volume details for the creation

AWS Credentials in terraform provider

The created file named main.tf copy the below code to it. This code helps in defining the configuration resource for creating EBS Volume in AWS account.

# Configure the AWS provider
provider "aws" {
region = "ap-south-1" # Replace with your desired AWS region
}

# Create an EBS volume
resource "aws_ebs_volume" "Terraform_Volume" {
availability_zone = "ap-south-1a" # Replace with your desired Availability Zone
size = 10 # Specify the size of the volume in GiB

tags = {
Name = "Terraform_Volume" # Removed leading space for the volume name
}
}

Step 6: Save the file by pressing ESC button and entering :wq! command

Step 7: Now we need to run this terraform file to do so, we have to firstly intialize the terraform, enter following command

terraform init

terraform-init

Step 8: Now Run Terraform plan command to review the changes in our terraform file

terraform plan

terraform-plan

Step 9: After reviewing what is going to happen, we need to run terraform apply command to finally create EBS Volume

terraform apply

terraform-apply

  • The following screenshot indicates that as per defined configuration terraform is creating the EBS Volume in provided AWS Account.

Terraform creating EBS Volume

Step 10: In the final Step, go to the volumes menu in EC2 Console, you will see that EBS Volume created using terraform

Note: The volume which has empty name is the root volume of our EC2 instance

final-output of AWS EBS

Advantages of Terraform

The following are the advantages of terraform:

  • Open Source Tool: Terraform is an open-source tool, which means it’s free to use and has an active community contributing to its development and support.
  • Multi-Cloud Support: Terraform supports multi cloud environment, meaning it can manage infrastructure across various cloud providers like AWS, Azure, Google Cloud Platform, and more.
  • Designed in Modules: Terraform is designed in modular structure which Promotes code reuse, improves maintainability, and simplifies managing complex deployments.
  • Automated Dependency Management: Terraform automatically understands the dependencies between resources and creates or updates them in the correct order, without having to manually manage those dependencies.
  • Rollback And Taint: If something goes wrong during an apply, Terraform can automatically roll back to the previous state. we can also mark resources as “tainted” to force them to be recreated on the next apply.

Disadvantages Of Terraform

The following are the disadvantages of terraform:

  • Performance: Terraform’s performance can be decreased for complex and large infrastructures, especially during the planning and applying phases, as it needs to process and manage a large number of resources.
  • Limited Testing and Validation: While Terraform supports basic syntax validation, it lacks IN advanced testing and validation capabilities for configurations.
  • Debugging Errors: Debugging errors in Terraform, especially in large deployments, can be challenging. Limited rollback capabilities can add to the complexity of troubleshooting.
  • Limited Visibility and Monitoring: Terraform does not provide built-in visibility or monitoring capabilities for the resources it manages. need to rely on external tools or cloud provider services for monitoring and visibility

Amazon EBS And Terrafrom – FAQs

What Is The Minimum Size For An EBS Volume?

The minimum size for an EBS volume is 1 GB.

Can I Create An Ebs Volume From A Snapshot?

Yes, you can create an EBS volume from an existing snapshot using the snapshot_id argument in the aws_ebs_volume resource.

Can I Create An EBS Volume With Provisioned Iops?

Yes, you can create an EBS volume with provisioned IOPS (for the io1 volume type) by using the iops argument in the aws_ebs_volume resource.

How Do I Delete An EBS Volume Created Using Terraform?

To delete an EBS volume created using Terraform, you need to remove the corresponding aws_ebs_volume resource from your Terraform configuration and run terraform apply. Terraform will detect the resource needs to be deleted and remove it.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads