Infrastructure as Code[IaC] With Terraform

Infrastructure as Code[IaC] With Terraform

In this article, we'll take a look at what Infrastructure as code [IaC] is and how Terraform is an Infrastructure as code tool for building infrastructure in the cloud.

What is Infrastructure as Code?

Infrastructure as Code allows you to define and manage infrastructure using code, instead of clicking through a console of cloud providers like "AWS, GCP, DIGITAL OCEAN" or the command line. This implies that one can manage an entire infrastructure in the same way one manages application code.

There are two approaches to Ia: "Imperative" and "Declarative".

The Imperative approach defines our configuration as a string of commands that is executed in a certain order.

Example: Running a "Bash script" to provision resources using AWS CLI.

While the Declarative approach defines the desired state of our infrastructure using the resources needed and properties the resources should have.

Examples: Terraform, Ansible, Cloud Formation.

In this article, we will be talking about one of the declarative approaches, Terraform.

What is Terraform?

HashiCorp Terraform is an infrastructure as a code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. You can then use a consistent workflow to provision and manage all of your infrastructure throughout its lifecycle. Terraform can manage low-level components like computing, storage, and networking resources, as well as high-level components like DNS entries and SaaS features.

Need of IaC

  • More efficient development

  • Speed and consistency

  • Decreased costs

  • Minimized risk

  • Single source of truth.

Creating a Terraform file

Step 1: To specify a specific version of the provider, use the required_providers block under terraform.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "4.67.0"
    }
  }
}

Step 2: Creating resource block inside main.tf file for creating an ec2 instance

resource "aws_instance" "my_ec2_instance" {
  ami           = "ami-08c40ec9ead489470"
  instance_type = "t2.micro"

  tags = {
    Name = "FirstEC2Instnace"
  }
}

Typical Terraform workflow involves 3 steps

  1. Write

  2. Plan

  3. Apply

Step 3: This command will check the configuration file and initialize the working directory containing the .tf file and install the required plugins for the provider.

terraform init

Step 4: This command will show the actions carried out by Terraform to validate whether the syntax used is correct or not.

terraform validate

Step 5: This command will show the actions carried out by Terraform to create the resource.

terraform plan

Step 6: This command executes the actions proposed in a terraform plan. It is used to deploy your infrastructure and will ask the user to type yes.

terraform apply --auto-approve

Step 5: This command will show the actions carried out by Terraform to delete all the created resources.

terraform destroy

What is Terraform state file?

When we create infrastructure after executing the "terraform apply" command. Terraform creates a state file called terraform.tfstate this state file contains all the information about the resources created using Terraform. This state file keeps track of resources created by your configuration and maps them to real-world resources. The state file is a sensitive file as it contains information about the infrastructure that we have created. You should never push this file to any version control system like GitHub. Store terraform.tfstate file in the backend to keep it safe.

The backend supported by Terraform:

  • Amazon S3

  • Azure Storage

  • Google Cloud Storage

  • HashiCorp’s Terraform Cloud and Terraform Enterprise.

Variables in Terraform

variables can be defined in variables.tf file and can be used in configuration files as var.variable_name

Types of variables
  • string ("file")

  • bool (true/false)

  • number (7)

  • any (Default value)

Output Variables in Terraform

Output variables are used to store the value of the expression in Terraform

output "public_ip_addr" {
   value = aws_instance.my_ec2_instance.public_ip
   description = "print public ipv4 of ec2 instance"

Conclusion

HashiCorp Terraform is a powerful tool that can be used to manage infrastructure across a wide range of cloud providers and on-premises systems. It is a popular choice for managing infrastructure for both small and large organizations. If you are looking for a way to improve the reliability, efficiency, and security of your infrastructure, then HashiCorp Terraform is a great option.