How to attach an EBS volume to EC2 instance using terraform

0 votes

Hi Guys,

I am new in Terraform. I want to attach an EBS volume to EC2 instance using Terraform. Can anyone help me with this?

Jun 12, 2020 in Terraform by akhtar
• 38,180 points
4,123 views

1 answer to this question.

0 votes

Hi@akhtar,

You can use aws_volume_attachment resource to attach an ebs volume. I have attached one example below for your reference.

variable "device_name" {default=["/dev/sdd"]}
variable "ec2_ebs_volume_count" {default=1}
resource "aws_ebs_volume" "ebs_volume" {
  availability_zone = aws_instance.myin2.availability_zone
  size = 2
  tags = {
    Name = "myebs1"
  }
}
resource "aws_volume_attachment" "ebs_att" {
  device_name = "/dev/sdd"
  volume_id = aws_ebs_volume.ebs_volume.id
  instance_id = aws_instance.myin2.id
  force_detach = true
} 

Hope this will help.

answered Jun 12, 2020 by MD
• 95,220 points
If you were to use this, when the machine booted, the device will be present to the instance, but will not be mounted at any mount point, nor be in the fstab.  This I think needs to be done via user_data.tpl.
You can try that.

Related Questions In Terraform

0 votes
1 answer

How to read the volume ID of the EC2 instance using Terraform?

Hi@akhtar, You won't be able to extract EBS ...READ MORE

answered Dec 4, 2020 in Terraform by MD
• 95,220 points
234 views
0 votes
1 answer

How to do ssh in EC2 instance using Terraform?

Hi@akhtar, To ssh in your EC2 instance, you ...READ MORE

answered Jun 15, 2020 in Terraform by MD
• 95,220 points
3,733 views
0 votes
1 answer

How to launch EC2 windows instance with powershell script using terraform?

Hi, In your Terraform code, you have written ...READ MORE

answered Jul 21, 2020 in Terraform by MD
• 95,220 points
1,490 views
0 votes
1 answer

How to launch one EC2 instance for WordPress using Terraform?

Hi@akhtar, You can use the aws_instance resource in Terraform. ...READ MORE

answered Sep 9, 2020 in Terraform by MD
• 95,220 points
166 views
0 votes
1 answer

Create Snapshot of EBS and attach to EC2 using Terraform

You can also create a snapshot using ...READ MORE

answered Oct 29, 2018 in AWS by Priyaj
• 58,120 points
1,033 views
0 votes
1 answer
0 votes
1 answer
+2 votes
1 answer

How to attach an elastic load balancer with ec2 instance using Terraform?

Hi@akhtar, You can use aws_elb_attachment resource in Terraform. ...READ MORE

answered Aug 17, 2020 in Terraform by MD
• 95,220 points
1,263 views
0 votes
1 answer

How to attach Elastic IP in EC2 instance using Terraform code?

Hi@akhtar, You can use aws_eip resource in your ...READ MORE

answered Jul 22, 2020 in Terraform by MD
• 95,220 points
3,927 views