DEV Community

Cover image for Getting Started with AWS and Terraform: Multi-Attaching EBS Volumes with Terraform
Chinmay Tonape
Chinmay Tonape

Posted on • Edited on

Getting Started with AWS and Terraform: Multi-Attaching EBS Volumes with Terraform

In a previous post, we tried to optimize scalability and availability for a web application by setting up autoscaling EC2 instances.

In this post, we will explore the concept of multi-attaching Elastic Block Store (EBS) volumes to EC2 instances using Terraform. This approach enhances storage resilience and performance, especially in scenarios where a web application demands data access from multiple instances simultaneously.

Architecture Overview

Before we dive into the technical details, let's quickly review the architecture we'll be working with:

Multi Attach EBS Architecture

Step 1: Creating the VPC and Network Components

Create VPC with IGW, 4 public subnets in single AZs and route table with association. Please refer to my github repo in resources section below.

Step 2: Creating Linux EC2 instances in single AZ

Create 4 EC2 instances in single AZ as multi-attach EBS is supported in single AZ only. Please refer to my github repo in resources section below.

Step 3: Creating an EBS volume using io2 family and multi-attach to EC2 instances

Multi Attach EBS is supported by Provisioned IOPS SSD (io1 or io2) type only and can be attached to only specific instance types.

####################################################
# Create EBS Multi attach Volume and attach to all EC2 instances
####################################################

resource "aws_ebs_volume" "volume" {
  availability_zone    = var.aws_azs
  size                 = 4
  type                 = "io2"
  iops                 = 200
  multi_attach_enabled = true
}

resource "aws_volume_attachment" "ebsAttach" {

  device_name = "/dev/sdh"
  volume_id   = aws_ebs_volume.volume.id
  count       = length(module.web.instance_ids)
  instance_id = element(module.web.instance_ids, count.index)
}
Enter fullscreen mode Exit fullscreen mode

Steps to run Terraform

terraform init
terraform plan 
terraform apply -auto-approve
Enter fullscreen mode Exit fullscreen mode

Upon successful completion, Terraform will provide relevant outputs.

Apply complete! Resources: 15 added, 0 changed, 0 destroyed.

Outputs:

aws_ebs_volume_id = "vol-0c435b4ee98bf0a88"
ec2_instance_ids = [
  "i-0fc8e0a22098fc4e3",
  "i-00c3d9e854fe98a83",
  "i-05ad62000bb15d168",
  "i-00de4618581eea346",
]
Enter fullscreen mode Exit fullscreen mode

Testing the outcome

You can see that EBS is attached to multiple instances.

Multi Attach EBS

and running instances are:

EC2 instances

Cleanup:

Remember to stop AWS components to avoid large bills.

terraform destroy -auto-approve
Enter fullscreen mode Exit fullscreen mode

By incorporating multi-attach EBS volumes into your infrastructure, you're taking a significant step towards improving storage performance and resilience. Stay tuned for the next module as we explore Elastic File System and its integration with multiple EC2 instances. Happy coding!

Resources:

Github Link: https://github.com/chinmayto/terraform-aws-linux-webserver-ec2-alb-multiattach-ebs
EBS Volume multi-attach: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-volumes-multi.html

Top comments (0)