How to create EC2 Instance(AWS) using terraform on local-state

Abhishek KordeAbhishek Korde
2 min read

first install the terraform in your linux terminal using following command

  1. Update system packages:

    Image description

  2. Add HashiCorp’s official GPG key and repository:

Image description

3.Install Terraform:

Image description

after installing verify terraform is installed or not in your system use following command:
terraform -version
if it show version it means terraform is sucessfully.
there is three step in terraform are as follows:

  1. terraform init This command initializes a working directory containing Terraform configuration files. This is the first command you should run after writing a new Terraform configuration or cloning an existing configuration from version control. It is safe to run this command multiple times.

  2. terraform plan This command creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure.

  3. terraform apply This command executes the actions proposed in a Terraform plan.

now we can see practically one by one command:
first write main.tf file using following command:

Image description

This file contains all information about our EC2 instance example region,service provider, instance name, etc

Image description

then run first command as we discuss in above section:
ie Terraform Init

Image description

if you got above responce then go for the Terraform Plan
It's basically dry run things it's show process will be after apply.

Image description

after using Terraform Apply command, It ask for Enter the value:

Image description

type "yes" for creating EC2 instance.
Now we sucessfully created EC2 instance in our aws cloud server
Now open dash board--->EC2 instance---->select region="us-west-2"
you can seen your EC2 Instance:

Image description

I use https://developer.hashicorp.com documentation for references
github link:https://github.com/abhikorde31/first-terraform-project

0
Subscribe to my newsletter

Read articles from Abhishek Korde directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhishek Korde
Abhishek Korde