prscrew.com

# Essential Terraform Commands for Every DevOps Engineer

Written on

Chapter 1: Understanding Terraform

If you're a DevOps engineer, you've likely encountered Terraform. You may even have some familiarity with it. This powerful tool transforms our code into tangible infrastructure, exemplifying the concept of Infrastructure as Code (IaC). Essentially, IaC empowers us to construct and manage our infrastructure using code, which is precisely what Terraform accomplishes.

Terraform in Action

Infrastructure as Code — Terraform

To set up a Virtual Machine (instance) on AWS, the code is straightforward:

provider "aws" {

region = "us-west-2"

}

resource "aws_instance" "example" {

ami = "ami-0c55b159cbfafe1f0"

instance_type = "t2.micro"

}

After writing this, we simply execute terraform init, wait a moment, and then run terraform plan followed by terraform apply to create the resources. This assumes that the AWS access and secret keys are already configured as Environment Variables. If not, you can set them as follows:

export AWS_ACCESS_KEY_ID="your_access_key"

export AWS_SECRET_ACCESS_KEY="your_secret_key"

Simple, isn’t it? While this process seems straightforward, there's a lot happening behind the scenes.

Initiating Terraform

When we execute terraform init, Terraform begins preparing our project. If Terraform were a vehicle, this would be akin to starting the engine. It inspects the code in main.tf to determine the desired infrastructure, which in our example is an EC2 instance on AWS.

Terraform recognizes that AWS is specified as the provider through the line provider "aws". This allows it to communicate effectively with AWS regarding our configurations. If we had chosen Azure or Google Cloud, Terraform would adapt accordingly.

Following this, Terraform downloads the AWS provider plugin, enabling it to interact with AWS services efficiently. A hidden directory called .terraform is created, acting as a toolbox where Terraform stores all the necessary tools for setting up the infrastructure, including the AWS plugin.

After completing these steps, Terraform is prepared and will display a message indicating readiness or any errors encountered. It essentially informs you whether it is ready for further instructions or if it needs assistance to resolve issues.

Initializing Terraform

Using terraform init

Creating a Blueprint with terraform plan

At this point, Terraform is equipped with the required tools and understands what we need. However, architects typically create a plan or blueprint before construction.

By running the command terraform plan, Terraform generates a blueprint based on our code. It revisits the main.tf file and identifies our intention to create an EC2 instance in AWS. It also assesses the current state of the infrastructure to determine if any components already exist, adjusting the blueprint accordingly.

After evaluating the state file, Terraform compares our existing infrastructure with the code and identifies any discrepancies, preparing to create the necessary resources. Since we have no existing infrastructure, it will proceed to create the EC2 instance. Once the blueprint is complete, Terraform provides a detailed overview of the proposed changes.

Terraform Planning

The Blueprint

Executing Changes with terraform apply

With the blueprint ready and all tools at hand, Terraform awaits further instructions. By issuing the command terraform apply, we prompt Terraform to commence construction based on the blueprint.

Upon execution, Terraform displays the planned changes and requests confirmation to proceed. If we agree to the changes, Terraform will construct the infrastructure as dictated by our code. In this case, it will initiate the creation of the EC2 instance.

Throughout the process, Terraform communicates with AWS using the previously downloaded AWS plugin. This is akin to Terraform acting as a project manager, directing AWS on how to build the instance. You will receive notifications throughout the build, detailing progress and successes or failures.

Once terraform apply completes, Terraform summarizes the actions taken, indicating which resources were created, modified, or deleted.

Successful Terraform Application

Successful Execution of Terraform

After completing these steps, you should see a newly created EC2 instance in the AWS console. Understanding the underlying processes of terraform init, terraform plan, and terraform apply is crucial. With this knowledge, learning the syntax and logic of Terraform code becomes significantly easier. I hope this guide proves as beneficial to you as it did for me.

This video, titled "All Terraform Commands - A Practical Approach," provides a practical overview of all essential Terraform commands, ensuring you grasp their applications effectively.

The "Common Terraform Commands" video offers insights into frequently used Terraform commands, helping you become more proficient in using this powerful tool.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Dune Part Two Delayed: Everything We Know About the Release

Dune Part Two's release has been postponed to March 2024. Here's what we know about the reasons behind this decision and the film's future.

Exploring the Mind's Limits: The 11-Day No Sleep Challenge

Discover the incredible journey of Randy Gardner's 11-day wakefulness experiment and its shocking effects on mental health.

Essential Insights for Crafting a UX Portfolio Website

Key considerations for fresh graduates to create an effective UX portfolio website that stands out to recruiters.