Terraform: Your Infrastructure Copilot (Part 1: Getting Started)

Shruti Kashyap
4 min readAug 24, 2024

--

To understand Terraform, let’s explore its core purpose:

Let’s understand by its own use cases:

Multi-Cloud Deployment — Suppose the infrastructure you want to create or operate is hybrid, consisting of on-premises, multi-cloud… It’s impossible for a DevOps person to understand CloudFormation (CFT) for AWS, ARM for Azure, and Ansible for on-premises. Terraform allows us to use the same workflow to manage multiple providers and handle cross-cloud dependencies.

Efficient deploy, release, scale, and monitor of application- Terraform help in these features through hosting a static website, rolling out new releases with the help of load balancers, and monitoring applications with Datadog.

Self-Service Clusters — HCP Terraform can be used to build a self-service infrastructure model for a large organization that may get many repetitive infrastructure requests. For example, the Terraform ServiceNow Service Catalog integration enables your end-users to provision self-service infrastructure via ticketing systems like ServiceNow.

Policy Compliance and Management — HPC Terraform uses Sentinel(a policy as code framework )to automatically enforce policy on Terraform configurations, states, and plans.

Installation/Setup of Terraform

Refer to above site for installation or you can also opt for Codespaces(a development environment present in the cloud, by default it runs in a Linux environment) in the GitHub

Comes with 15 GB and 60 hours per month with free tier’s usage

Installation steps for Terraform & AWS on Codespaces -:

  1. Create Codespaces for your repository

2. In the search bar , select option -:

For Terraform Installation
For AWS Installation

3. Rebuild the container

Search for key “rebuild”

4. Check for installations

Verify AWS and Terraform installation

5. Configure AWS on Codespaces to interact directly with AWS resources -:

We can do the configure through root user or IAM user 
Root user (ex- Admins) - full control over the infrastructure
IAM user(ex- Devops Engineer ,Developers) - individual accounts within AWS account to do a specific kind of task

Create access key (Long term credentials for an IAM user or root user) on AWS

Creation of Access Key

Run the command “aws configure” on codespaces cli and provide the details

Verify the configuration by running the AWS command on Codespaces CLI to check that it is able to interact with AWS resources

Let’s dive deeper into understanding the creation and workflow of Terraform.

  • We need main.tf file— Starting point where you implement the logic of infrastructure as a code
Layout of main.tf file

In the above image , we are creating EC2 instance through terraform .We have to specify following parameters to create instances on AWS -:

1.Provider -plugin to interact with cloud providers

2. Resource -a crucial element to specify what kind of resource we are creating with unique identifier for instance name within terraform configuration

Apart from that other keys are feature of AWS to create the EC2 instance for ex- AMI (Amazon machine name ) ID to use for the instance, subnet (range of IP address in your VPC(Virtual Private Network) to define network range, gateway and security rule of instance) and key pair to access the instance

After writing the main.tf file ,we have to execute it according to the workflow of terraform -:

  1. terraform init # Initialize Terraform
  2. terraform plan #review the plan
Plan command describes about what the main.tf will do in background in details

3. terraform apply #will execute the main.tf file

4. Check in your AWS platform ,new EC2 instances will get created with the same AMI id that was mentioned in main.tf file (just to clarify we have to take the value from AWS to put in the main.tf)

EC2 Instance Created

5. “terraform destroy” #destroy all remote objects managed by a particular Terraform configuration

Note — You can also use “terraform validate” command to check whether a configuration is syntactically valid and internally consistent.

After executing main.tf file ,you will notice one state file has been created in your repository with this kind of content

Content of “terraform.tfstate” file

This state file is by default gets created under the name “terraform.fstate” that stores information that Terraform has created .

Stay Tune and Happy to receive any kind of feedback

Abhishek Veeramalla & Bibin Wilson…Thanks for the motivation !!

--

--

Shruti Kashyap
Shruti Kashyap

No responses yet