How to Launch Ec2 instance and attach EBS Volume by using Terraform

Anu Chundawat
3 min readMay 21, 2021

What is terraform??

Terraform is a automation tool which is used to provision the infrastructure o cloyud . It is used with hybrid and multi-cloud gcp, azure,aws and etc. It support almost all main clouds.

Terraform is an infrastructure as code tool, it uses HCL (Hashicorp configuration language) to write a code. Its code involves blocks,arguments and expressions.

Why we need terraform ??

All cloud have facility of IAC(Infrastructure As Code) but it harder to memorize how to write code for different cloud. But by using terraform, we can use the code written by using terraform tool in different cloud to provision our resources. It codify our application infrastructure.

First we have to install Terraform in our system:

Downlaod the terraform from the given link :

Download Terraform — Terraform by HashiCorp

After download, install it and give its path to path environment variable.

Now to windows CMD

Create default profile using AWS configure command. our default profile will contains aws credentials(access key and secret access key).

For getting credential go to your AWS account and Go to IAM service in aws portal in your account and click add user as shown below:

Give the programmatic access to these user and then click next and attach a policy which gives this user to access AWS services.

Now click next and create this user. we will use the credential of this user in cmd.

Go to CMD and create default profile

aws configure

Now let write the code

create a new folder and in this folder create a file with .tf extension:

> mkdir aws_ws
>cd aws_ws
> notepad ec2_ebs.tf

Now write the code in ec2_ebs.tf file

Give the provider and default profile in the code

provider aws {profile = defaultregion=ap-south-1}

Now to launch ec2 instance:

resource "aws_instance" "os1" {ami= "ami-010aff33ed5991201"instance_type = "t2.micro"tags = {Name = "AWS_Os_from_terraform"}
}

To create the EBS volume:

resource "aws_ebs_volume" "my_vol" {availability_zone = aws_instance.os1.availability_zonesize              = 10}

Attach the EBS volume to created EC2 instance:

resource "aws_volume_attachment" "ebs_att" {device_name = "/dev/sdh"volume_id   = aws_ebs_volume.my_vol.idinstance_id = aws_instance.os1.id}

Now We have created our code now we need to run below given command which will connect us to AWS by installing required plugins

> terraform init

Now we can see the plan:

> terraform plan

it will shown what action willl performed by terraform on aws cloud. so we can make s ure that we are doing the right things.

Now run the code

> terraform apply

It will ask for yes when prompted so write yes then the will launch our requirements on aws

it will launch an EC2 instance and attach EBS volume to it.

Now, we can see our EC2 instance is launched in aws cloud:

And EBS is also created and attached to above EC2 instance:

Thank you for reading.

--

--