Getting Started with Terraform

ยท

3 min read

Getting Started with Terraform

What is Terraform?

  • It is a tool which automates and manages your infrastructure.

  • your platform and services that run on that platform

  • It may not be totally open source due to the licence recently published by HashiCorp.

  • and it follows the Declarative approach means it defines WHAT should be end result.

Terraform vs Ansible

Similarities :

  • Both tools are used as infrastructure as code like provisioning infrastructure and configuring them.

Difference:

TerraformAnsible
Mainly infrastructure provisioning toolMainly a configuration tool
Relatively new and much more advance in orchestrationMore mature

So, DevOps Engineer uses both tools in combination.

Terraform Archiecture and How does terraform work?

There are two main components:

  • Core:

Core takes the terraform file and compares what has to be done. It compares with the current state and makes a plan for how to achieve the desired state.

  • Providers:

It is another component of a specific technology. It could be like AWS, Digital Ocean etc. Terraform also support some advanced-level providers like Kubernetes. These providers give access to Terraform to make some stuff in their platform.

Installation

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

Run terraform --version check Is it good to go?

Writing Your First Infrastructure Code

Let's walk through writing a simple Terraform configuration that provisions an AWS EC2 instance.

provider "aws" { # provider is AWS
  region = "us-west-1"
}

resource "aws_instance" "example_instance" {# creating the Ec2 instance on AWS
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

Terraform command for different stage

In Terraform, you can use different commands to manage your infrastructure across various stages of its lifecycle. Here are some common Terraform commands and how they relate to different stages:

  1. Initialization:

    • terraform init: This command initializes your Terraform working directory by downloading provider plugins and setting up the backend configuration.
  2. Configuration:

    • terraform plan: This command creates an execution plan, showing what changes Terraform will make to your infrastructure to reach the desired state.

    • terraform validate: This command checks your configuration files for syntax errors and potential issues.

  3. Provisioning:

    • terraform apply: This command applies the changes specified in your configuration, creating or updating resources to match the desired state. It's used to create or modify infrastructure.
  4. Managing State:

    • terraform state: This set of subcommands allows you to inspect and manage the Terraform state file.
  5. Destroying:

    • terraform destroy: This command destroys the resources defined in your configuration, effectively tearing down your infrastructure.
ย