top of page

What is Terraform and step-by-step Quick way to Try it in AWS

Updated: May 3, 2023


Terraform is an open-source infrastructure as code software tool created by HashiCorp. It allows users to define and provision a variety of cloud and on-premises infrastructure resources, such as virtual machines, networks, and storage, through a declarative configuration language. This article will provide a step-by-step example of how to use Terraform to provision an AWS EC2 instance, while also incorporating search engine optimization techniques to ensure that the content is optimized for search engines.

Here is a step-by-step example of how to use Terraform to create a virtual machine on AWS:


Step 1: Set up your AWS Account


Before we can get started with Terraform, we need to set up an AWS account. If you already have an AWS account, you can skip this step. If you don't have an account, go to the AWS website and sign up for a free account. Once you have an account, you'll need to create an access key and secret key for your account.


Step 2: Install Terraform


The next step is to install Terraform on your local machine. You can download Terraform from the HashiCorp website. Once you have downloaded Terraform, extract the zip file to a directory on your computer, and add the Terraform binary to your system's PATH.


Step 3: Write Terraform Configuration Files

Now that Terraform is installed, we can start writing the configuration files that will define our infrastructure. For this example, we will create an EC2 instance on AWS. Create a new file named "main.tf" and add the following code:

provider "aws" {
  access_key = "<AWS_ACCESS_KEY>"
  secret_key = "<AWS_SECRET_KEY>"
  region     = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

In this configuration, we are specifying that we want to use the AWS provider, and we are providing our AWS access key and secret key. We are also specifying that we want to create an EC2 instance with the "ami-0c55b159cbfafe1f0" Amazon Machine Image (AMI) and the "t2.micro" instance type.


Step 4: Initialize Terraform


Now that we have our Terraform configuration file, we need to initialize Terraform in the directory where our configuration file is located. Open a command prompt or terminal window, navigate to the directory where you saved the main.tf file, and enter the following command:

terraform init

This will initialize Terraform and download any necessary providers and modules.


Step 5: Plan Infrastructure Changes


Once Terraform is initialized, we can now plan our infrastructure changes. Enter the following command:

terraform plan

This will generate a plan of the infrastructure changes that Terraform will make. In this case, Terraform will create a new EC2 instance on AWS.


Step 6: Apply Infrastructure Changes


Now that we have generated a plan, we can apply the infrastructure changes by entering the following command:

terraform apply

Step 7: Destroy Infrastructure Changes


Now that we have created the resource, we can destroy after verifying in console the infrastructure by entering the following command:

terraform destroy

60 views0 comments

Recent Posts

See All

Difference between DEVOPS and SRE with Example?

DevOps (Development and Operations) and SRE (Site Reliability Engineering) are two approaches or philosophies that aim to improve software development and operations, but they have different focuses a

Linux for DevOps Interview Questions

Question: Write a Linux command to list all files and directories in the current directory. Answer: ls Question: How would you create a new directory named "my_project" in the home directory of the cu

bottom of page