Create AWS EC2 Instance using Terraform.

Shubham Rasal [SRE]
3 min readJun 11, 2020
Create AWS EC2 Instance using Terraform

What is Terraform ?

“Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.”

Now let’s discuss where it is used?

There are many use-cases but let’s consider one scenario.

Consider an organization that has a project and they have multiple teams working on that project. After a certain level, it becomes very challenging to manage a large and growing infrastructure for the centralized operations team. Manually handling infrastructure and shifting to another account without having to document what you have done is very hard.

Using Terraform, the knowledge of how to build can be codified in a configuration. Additionally, infrastructure can be shared and re-used. i.e Infrastructure as a Code.

and my favorite one is Terraform lets you build one-click infrastructure and one-click destroy infrastructure.

1.Download Terraform

You can download terraform according to your Operating System.

https://www.terraform.io/downloads.html

2. Extract and Set the environment path variable.

You can extract anywhere but I recommend you make a folder into C:/Program file and extract there. Then update the environment path variable.

3.Check terraform Version.

terraform -v

Configure AWS

1. Create an IAM user that has an EC2 creation policy.

I recommend you to create PowerAccessPolicy for Practice.

For more details you can refer this:How to create IAM user

2. You will get ACCESS KEY and SECRET KEY using that you can now configure to aws on cli.

aws configure — profile “name”

Build EC2 instance

For Terraform we have to use(HCL) HashiCorp Configuration Language.

As now we have already signed up into CLI.

we need to configure into Terraform.

Configure to terraform.

Open file and give any name. Save with .tf extension. Copy below code into that file.

provider "aws" {
region = "ap-south-1"
profile = "shubham"
}

Now to install aws plugin we need to run below command

$ terraform init

Create EC2 instance

resource "aws_instance" "myFirstInstance" {
ami = "ami-0447a12f28fddb066"
instance_type = "t2.micro"
tags = {
Name = "server"
}
}

add above code to file. Here we are using “aws_instance” resource and we named it as “MyFirstInstance”. We used ami of Amazon linux so it will lanuch Amazon Linux os with t2.micro type and name will be “server”.

Output

output "Server_instance"{
value=aws_instance.myFirstInstance
}

add above code to get the details of instance on terminal.

How to run code?

$ terraform validate myinstance.tf
$ terraform apply

first command will validate the syntax is correct or not. Even it is optional but using this command is good practice.

terraform apply will execute the code on aws. type “yes” when it prompted.

Output: We will see the details of above instance. You can also do recheck on https://ap-south-1.console.aws.amazon.com/ec2 .

How to delete all infrastructure?

To delete everything you created using terraform code.

$ terraform destroy

above command will destroy everything that is managed by terraform.

Conclusion

This is the just HelloWorld program of AWS using Terraform. Here we just created EC2 instance without key.

We will do many practicals. Don’t hesitate to ask queries. For further articles follow and stay connected.

--

--