Effortless AWS Resource Provisioning with Terraform
Published: 2023-11-25 17:29:25

---
title: Effortless AWS Resource Provisioning with Terraform
published: true
date: 2023-09-05 17:53:31 UTC
tags: 
canonical_url: 
---

## Terraform

Terraform is a DevOps tool that allows the automation of cloud infrastructure, platforms and services that are running on the cloud.

### Key Features of Terraform

- Infrastructure as Code: We can create a blueprint of the cloud infrastructure using scripting language which can be shared and re-used just like we can do with any other code.

- Execution Plans: The Terraform planning step generates an execution plan that shows which resources will be added, deleted and modified after we do the Terraform apply. This is one of the important features that allows us to review or assess our infrastructure before deploying it to the cloud.

- Resource Graph: When creating resources using terraform, it parallelizes the creation and modification by creating a resource graph of non-dependent resources. This helps in creating infrastructure as efficiently as possible.

- Change Automation: It is easier to make any change to the infrastructure as we execution plan gives an idea of what change is to be made and the Resource Graph takes care of the dependency among resources.

## Prerequisites

Before we create cloud resources on the Terraform we want:

- Cloud Provider Account: We will be using AWS as our cloud provider. You can create an account with AWS.

- Terraform installed: We want to have Terraform CLI installed on the local machine to write and run scripts. Follow the instructions mentioned in [https://developer.hashicorp.com/terraform/downloads](https://developer.hashicorp.com/terraform/downloads) to install Terraform.

- Basic knowledge of AWS and how to navigate and create resources on AWS through a web interface.

# Terraform workflow

Terraform workflow defines the set of processes to follow while creating infrastructure on the cloud. This allows us to follow best practices of writing code for infrastructure and managing and reusing the configurations. Following are the major steps that we should be following when creating infrastructure using Terraform.

1. **Writing terraform scripts** : We write the configurations that we want to deploy to the cloud using the HCL (HashiCorp Configuration Language). HCL scripts are identified with **.tf** extension\*\*.\*\*

2. **Initialize:** This step is used to initialize the Terraform and download the providers that are mentioned in the **.tf** configuration file. **_terraform init_** is the command that is used to initialize the terraform scripts.

3. **Plan:** This step is to preview changes that we are going to deploy on the cloud platform. This is one of the most important steps which gives us the idea of resources that we are going to create, update or delete (destroy) on the cloud. **_terraform plan_** is the command that is used to see the preview of the changes.

4. **Apply:** In this step, we create the resources according to the Plan that was generated in the Plan step. **_terraform apply_** is the command that is used to apply changes\*\*.\*\*

5. **Destroy:** When resources are no longer needed we want to delete them from the cloud. This step is irreversible so we have to be careful when deleting the resources.

# AWS infrastructure with Terraform

Open up your favorite editor and create a new file name as [**main.tf**](http://main.tf) **,** copy the below code and paste it into the [main.tf](http://main.tf).

```
terraform {
    required_providers {
        aws = {
            source = "hashicorp/aws"
            version = "~> 4.0"
        }
    }
}

# Configure AWS Provider
provider "aws" {
  access_key = # IMA User access key
  secret_key = # IMA User secret key
    region = "ap-south-1"
}

```

Let's go through the code and see what exactly it is doing.

All the configurations related to the AWS or other cloud providers are done in terraform block.

**Terraform Providers:** Terraform providers are packages that connect to cloud platforms and provide the necessary API calls for Terraform to manage resources on those platforms. In our case, we are using the AWS provider.

Details of different providers can be found at [**https://registry.terraform.io/browse/providers**](https://registry.terraform.io/browse/providers) . We are using the AWS provider ([**https://registry.terraform.io/providers/hashicorp/aws/latest**](https://registry.terraform.io/providers/hashicorp/aws/latest) ), which is provided by Hashicorp.

We have a **required\_providers** block that specifies the source and version of the provider we are using. In our case, we are using the AWS provider provided by Hashicorp, with a version greater than 4.0.

**access\_key:** This is where we specify the IMA User access key.

**secret\_key:** This is where we specify the IMA User secret key.

Let's test the code that we have just written. Run the following command in the same directory where you have saved [main.tf](http://main.tf) file.

```
> terraform init

```

**terraform init:** This command is used to initialize the working directory. It is responsible for installing the correct version on plugins from the configuration files. In our case this command will install the latest version of the AWS terraform provider.

## Resources - EC2

AWS provider comes with a set of resources that represent the actual cloud resource that we wish to create using Terraform

```
resource "aws_instance" "demo" {
 ami = ami-00831fc7c1e3ddc60
 instance_type = t2.micro

 tags = {
   name = "Demo System"
 }
}

```

In the above code, resources are specified using the **resource** keyword followed by the type of resource we want to create on the cloud. In the above code, we are creating a resource of type aws\_instance with the name demo

For each resource, we create we provide default configuration for that resource. In the above code, we have mentioned

- Amazon Machine Image (AMI) ID: This is a unique identifier assigned to the specific virtual machine image in the AWS.

- Instance Type: This specifies the type of instance we want to create on the AWS. Different instances come with different sets of configurations such as CPU, Memory, Storage, Networking, etc.

- Tags: This is the identifier we have given to the instance so that we can recognize this specific instance on AWS.

Run the following commands.

```
> terraform plan

```

This will show the preview of the resources in our case AWS instance to be created.

Run the below command to apply changes to the AWS.

```
> terraform apply

```

# **Closing Thoughts**

Well, this was just a basic introduction to the Terraform its workflow and how we can create resources on the cloud platforms using scripts that can be reused. We can create a whole cloud infrastructure using Terraform. One of the major advantages that Terraform provides is that we can manage the scripts through a version control system like GIT that gives us the flexibility to track the changes in the infrastructure which ensures collaboration.

# Further Reading

- [https://www.varonis.com/blog/what-is-terraform](https://www.varonis.com/blog/what-is-terraform)

- [https://www.hashicorp.com/resources](https://www.hashicorp.com/resources)

- [https://registry.terraform.io/providers/hashicorp/aws/latest/docs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)