Sunday, March 15, 2020

Terraform for Absolute Beginners - Create your first EC2 instance

Objective : Today, I will help you to create your EC2 instance using terraform.

Terraform

Terraform is an Infrastructure as code (IaC) tool made by Hashicorp for building, changing and versioning of infrastructures. Now a days the demand of Terraform is getting higher. Terraform can be used with the popular cloud providers like AWS, Azure, Google, Oracle etc.

Pre Request

You need a user in AWS, having necessary permissions to create an EC2 instance.
(Please check our blogs/videos, how to add permission for an AWS user)

Steps to Provision

  1. Download the terraform binary file from https://www.terraform.io/downloads.html
  2. Extract the terraform zip file
  3. The result is a terraform binary file
  4. cp terraform /usr/local/bin/terraform
  5. Validate terraform by using the command terraform -v

Minimal configuration for starting EC2 instance

mkdir terraform-workshop
cd terraform-workshop
vi ec2.tf


provider "aws" {
access_key = "YOUR_ACCESS_KEY"
secret_key = "YOUR_SECRET_KEY"
region = "us-east-1"
}

resource "aws_instance" "example_ec2" {
ami = "ami-2757f631"
instance_type = "t2.micro"
}

Save the file. 
You will get your AWS access_key and secret_key as a pre-request.

terraform init
terraform apply

Login to your AWS console and check the EC2 instance under N. Virginia region.

terraform destroy

Will make sure to remove all the AWS resources which we created just above.