Elastic Block Storage (EBS)

The resource type “aws_ebs_volume” is used to manage EBS volume. We can attach the created volume to an instance in the same availability zone.

Configuration :

resource "aws_ebs_volume" "mydisk" {
  availability_zone = "us-east-1a"
  size = 30
}

The availability_zone attribute is mandatory. Other optional attributes are :

  • size
  • type
  • iops
  • encrypted
  • tags
  • snapshot_id
  • kms_key_id

The type of EBS volume. Can be “standard”, “gp2”, “io1”, “sc1” or “st1” (Default: “standard”).

Example :

resource "aws_ebs_volume" "mydisk" {
  availability_zone = "us-east-1a"
  type = "gp2"
  size = 30
  encrypted = true
}

Attaching EBS volume to EC2

We can attach the ebs volume to an EC2 instance with “aws_volume_attachment".

resource "aws_volume_attachment" "ebs_att" {
  device_name = "/dev/xvdh"
  volume_id   = "${aws_ebs_volume.example.id}"
  instance_id = "${aws_instance.web.id}"
}

Configuration :

resource "aws_instance" "myserver" {
  ami               = "ami-02e680c4540db351e" 
  availability_zone = "us-west-2a" 
  instance_type = "t1.micro" 
}

resource "aws_ebs_volume" "mydisk" {
  availability_zone = "us-west-2a"
  size              = 10
}
resource "aws_volume_attachment" "ebs_attachment" {
  device_name = "/dev/xvdh"
  volume_id   = "${aws_ebs_volume.mydisk.id}"
  instance_id = "${aws_instance.myserver.id}"
}

 

Advertisements

Learn Technology

%d bloggers like this: