Day 1: Introduction to Terraform

Day 1: Introduction to Terraform

Introduction

Infrastructure as Code (IaC) is the process of managing and provisioning computing infrastructure and related services in an automated, programmatic way, using code. There are several tools available for implementing IaC, and one of the most popular ones is Terraform. Terraform is an open-source infrastructure as a code software tool created by HashiCorp. It allows you to define, provision, and manage a wide range of resources, such as virtual machines, storage accounts, network interfaces, and so on, across multiple cloud providers and on-premises environments. In this blog post, we will explore the basics of Terraform, its key features, and how it can be used to manage infrastructure.

What is Infrastructure as Code?

Infrastructure as Code (IaC) is the practice of managing and provisioning computing infrastructure and related services in an automated, programmatic way, using code. It involves treating infrastructure as if it were software, and applying software engineering principles and practices to the management of infrastructure. In IaC, infrastructure is defined and managed using a high-level description language, such as YAML, JSON, or HCL. This language describes the desired state of the infrastructure, including the resources and their configuration. A tool, such as Terraform or Ansible, is then used to apply this configuration to the infrastructure, creating and managing the resources as needed.

  • ARM Templates

ARM Templates and Azure Blueprints are infrastructure as code tools that are specific to the Azure cloud platform. ARM templates are used to define and deploy Azure resources, while Azure Blueprints are used to orchestrate the deployment of multiple ARM templates and to manage resource relationships.

  • CloudFormation

CloudFormation is an infrastructure as a code tool developed by AWS that allows developers to define and deploy AWS resources. With CloudFormation, you can create templates to automate the deployment of resources and manage their dependencies.

  • Cloud Deployment Manager

Cloud Deployment Manager is an infrastructure as code tool developed by Google Cloud Platform that allows developers to define and deploy Google Cloud resources. With Cloud Deployment Manager, you can create templates to automate the deployment of resources and manage their dependencies.

  • Terraform

Terraform is an infrastructure as a code tool that supports multiple cloud service providers, including AWS, Azure, Google Cloud Platform, and others. With Terraform, you can define infrastructure resources in a declarative way and manage them across different cloud platforms. Terraform is known for its flexibility, extensibility, and ease of use.

Terraform Basics

Terraform uses a declarative syntax to define the desired state of the infrastructure. You write Terraform code in a Domain-Specific Language (DSL) called HashiCorp Configuration Language (HCL). In HCL, you define resources, their properties, and their relationships. Terraform uses this information to create a plan for the infrastructure, which then applies to the cloud provider or on-premises environment.

The Terraform workflow consists of the following steps:

  1. Define: You write Terraform code in HCL to define the infrastructure resources and their properties.

  2. Initialize: You run the terraform init command to initialize Terraform and download the necessary provider plugins.

  3. Plan: You run the terraform plan command to generate an execution plan that shows what Terraform will do when you apply the configuration.

  4. Apply: You run the terraform apply command to apply the execution plan and create the infrastructure resources.

  5. Destroy: You run the terraform destroy command to destroy the infrastructure resources.

Key Features

Here are some of the key features of Terraform:

  • Declarative Syntax

Terraform uses a declarative syntax to define the desired state of the infrastructure. This means that you specify what you want the infrastructure to look like, and Terraform figures out how to make it happen.

  • Provider Plugins

Terraform uses provider plugins to interact with cloud providers and other infrastructure providers. Provider plugins are written in Go and communicate with the provider's API to create and manage resources.

  • Resource Graph

Terraform creates a resource graph based on the relationships between resources. This graph is used to determine the order in which resources are created and destroyed.

  • State Management

Terraform stores the state of the infrastructure in a file called the state file. This file contains information about the resources that Terraform manages, including their current state, metadata, and dependencies.

  • Plan Execution

Terraform generates an execution plan that shows what changes will be made to the infrastructure before it applies them. This allows you to review the changes and ensure that they meet your requirements before they are made.

Set up Terraform and create your first configuration

Step1: Run the following commands to install Terraform in Ubuntu/Debian:

wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform

Step2: Verify the installation using the following command:

terraform -v

Create a Terraform configuration file

  1. Create a new file in the working directory with a .tf extension. For example, main.tf. Open the file in a text editor and define your infrastructure configuration using the HashiCorp Configuration Language (HCL). Here's a simple example for AWS:

     provider "aws" {
       access_key = "YOUR_ACCESS_KEY"
       secret_key = "YOUR_SECRET_KEY"
       region     = "us-west-2"
     }
    
     resource "aws_instance" "example" {
       ami           = "ami-0c55b159cbfafe1f0"
       instance_type = "t2.micro"
     }
    
  2. Run the following command in the terminal to initialize a new Terraform configuration in the current directory:

terraform init
  1. Run the following command:
terraform plan

Terraform will analyze your configuration files and compare the current state of your infrastructure with the desired state defined in the configuration. The output of terraform plan will display a summary of the actions Terraform plans to take. It will show you which resources will be created, modified, or destroyed. It also provides information such as the execution order and any dependencies between resources.

  1. Apply the configuration

Run the following command to apply the Terraform configuration and create the infrastructure resources:

terraform apply

Terraform will prompt you to confirm the planned changes. Enter yes to proceed. It will then provide the resources defined in your configuration.

Conclusion

Infrastructure as Code (IaC) is an approach to managing infrastructure that involves treating infrastructure as if it were software, using code to define and manage infrastructure resources. IaC enables the creation of consistent, repeatable infrastructure configurations that can be applied across environments, reducing the risk of configuration drift, and enabling faster provisioning and scaling of resources. Popular IaC tools include Terraform, Ansible, Chef, Puppet, CloudFormation, Azure Resource Manager, and Google Cloud Deployment Manager. Choosing the right IaC tool will depend on your specific needs and preferences, but all of these tools share the goal of enabling the automated, programmatic management of infrastructure resources. Overall, IaC is a powerful approach to managing infrastructure that can help organizations achieve greater consistency, speed, collaboration, auditing, and agility.

Did you find this article valuable?

Support Ashutosh Mahajan by becoming a sponsor. Any amount is appreciated!