Day 61 - Terraform🔥

ANSAR SHAIKANSAR SHAIK
5 min read

Welcome back to the 61st day of our DevOps journey! Today, we dive into the world of Terraform, a powerful infrastructure as code tool. Terraform has been a game-changer in the DevOps landscape, providing a simple yet effective way to manage and provision infrastructure across various cloud providers and on-premises environments.

What is Terraform?

Terraform is an open-source infrastructure as code software tool created by HashiCorp. It allows operators to define infrastructure in a high-level configuration language known as HashiCorp Configuration Language (HCL) or optionally JSON. With Terraform, you can manage and provision infrastructure resources such as virtual machines, networks, storage, and more, across various cloud providers like AWS, Azure, Google Cloud, as well as on-premises environments.

Now, let's delve into some basic Terraform commands that you'll find yourself using frequently:

Task-01: Basic Terraform Commands and Their Purpose

  1. terraform init: The terraform init command initializes a working directory and sets up the backend for storing infrastructure's state. It also downloads and installs any required providers and plugins.

    You should run the terraform init command after writing a new Terraform configuration or cloning an existing one from version control. It's safe to run this command multiple times.

    Here's what the terraform init command does:

    • Initializes a working directory

    • Downloads the necessary provider plugins and modules

    • Sets up the backend for storing your infrastructure's state

    • Automatically downloads and installs any required Terraform providers and plugins used within the Terraform code

    • Sets up the backend configured in the Terraform code for storing the Terraform state ( .tfstate ) file

Terraform is an open-source provisioning tool written in the Go language and created by HashiCorp. DevOps teams primarily use it to automate various infrastructure tasks, such as provisioning cloud resources.

  1. terraform init -upgrade: In Terraform, terraform init is a command used to initialize a Terraform configuration. It performs several initialization steps, such as downloading providers and modules defined in the configuration, initializing the backend, and more.

  2. terraform plan: Terraform Plan is a command in Terraform's CLI that creates an execution plan. This plan previews changes that Terraform will make to your infrastructure.

    Here's how Terraform Plan works:

    1. Compares your current state (the existing resources) with your desired state (the configuration file)

    2. Generates a plan of action to achieve your desired state

    3. Displays an execution plan that details which actions will be taken on which resources

  3. terraform apply: The terraform apply command executes the actions proposed in a Terraform plan. It uses the relevant infrastructure provider's API to carry out the planned changes to each resource.

    Here's how terraform apply works:

    1. It performs a plan

    2. It asks for confirmation from the user before making any changes

    3. It executes the actions proposed in a Terraform plan

    4. It creates, updates, or deletes infrastructure resources to match the new state outlined in your IaC

    5. It uses the providers and modules installed during initialization to execute the steps stored in an execution plan

  4. terraform validate: The terraform validate command in Terraform verifies the correctness of configuration files. It checks the syntax of the files, ensures the correct usage of attributes and values, and validates the configuration based on the core syntax of Terraform. It also checks all the providers in the code.

    The terraform validate command is run from the command line in the directory containing your Terraform files. It does so without deploying any infrastructure resources, but by just checking your configuration files for mistakes.

    The terraform validate command does not check formatting (e.g. tabs vs spaces, newlines, comments etc.). It will display an error if any of the files doesn't validate.

  5. terraform fmt: The terraform fmt command in Terraform rewrites configuration files to a canonical format and style. It uses a subset of the Terraform language style conventions and other minor adjustments for readability.

    The terraformfmt command checks and rewrites Terraform files to the required canonical format and style. It checks for several features, including:

    • Indentation

      Ensures that the Terraform code has correct indentation, making the code easier to understand and read

    • Code irregularities

      The slightest code irregularities, such as misaligned brackets or excessive indentation, can lead to frustration and hinder the maintainability of Terraform projects

The terraformfmt command lists out the files that have been updated once run.

  1. terraform destroy: When you no longer need the infrastructure provisioned by Terraform, you can use this command to destroy all the resources defined in your configuration files. It's essential to use this command carefully, as it will permanently delete the infrastructure.

Task-02: Terraform Competitors and Alternatives

While Terraform is a popular choice for infrastructure provisioning, it's worth mentioning some of its main competitors and alternative tools:

  1. Ansible: Ansible is an open-source automation platform that focuses on configuration management, application deployment, and task automation. While Terraform is primarily used for infrastructure provisioning, Ansible can handle a broader range of automation tasks, including provisioning, configuration, and orchestration.

  2. Packer: Packer is another tool from HashiCorp that focuses on creating identical machine images for multiple platforms from a single source configuration. While Terraform provisions infrastructure resources, Packer builds custom images for virtual machines or containers, which can then be used with Terraform for provisioning.

  3. Cloud Foundry: Cloud Foundry is an open-source platform as a service (PaaS) that provides a platform for deploying, running, and managing applications. While Terraform focuses on infrastructure provisioning, Cloud Foundry abstracts away the underlying infrastructure, allowing developers to focus solely on deploying and managing applications.

  4. Kubernetes: Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. While Terraform can provision infrastructure resources for Kubernetes clusters, Kubernetes itself is more focused on application deployment and management rather than infrastructure provisioning.

Each of these tools has its strengths and use cases, and the choice between them depends on your specific requirements and preferences.

That's it for today's exploration of Terraform! Tomorrow, we'll continue our journey with more exciting topics in the world of DevOps. Stay tuned! 🔧🚀

0
Subscribe to my newsletter

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

Written by

ANSAR SHAIK
ANSAR SHAIK

AWS DevOps Engineer