Getting started with Terraform

Krishan SharmaKrishan Sharma
4 min read

Day 1 of #TerraWeek Challenge

Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to define and provision infrastructure resources such as virtual machines, databases, networks, and more using code. Terraform uses a declarative configuration language to define the desired state of your infrastructure, and it automatically manages the provisioning and configuration of those resources to match the defined state. This approach makes infrastructure provisioning and management more efficient, predictable, and scalable.

Why do we need Terraform?

Traditional infrastructure provisioning methods often involve manual and error-prone processes, making it difficult to replicate and maintain infrastructure across different environments. Terraform addresses these challenges and simplifies infrastructure provisioning in several ways:

  1. Infrastructure as Code (IaC): Terraform allows you to define your infrastructure as code, which means your infrastructure's configuration is stored in version-controlled files. This enables better collaboration, versioning, and automation.

  2. Declarative Configuration: Terraform uses a declarative syntax, where you specify the desired end state of your infrastructure. Terraform then figures out how to make the actual infrastructure match this desired state.

  3. Multi-Cloud and Multi-Provider Support: Terraform supports multiple cloud providers (e.g., AWS, Azure, GCP) and other infrastructure platforms (e.g., VMware, Docker). This allows you to manage resources across different providers with a consistent workflow.

  4. Resource Dependency Management: Terraform automatically manages resource dependencies, ensuring that resources are created or updated in the correct order to avoid configuration errors.

  5. Change Management: Terraform provides a way to plan and preview changes before applying them, reducing the risk of unintended modifications to your infrastructure.

Terraform Workflow

Terraform lifecycle consists of โ€” init, validate, plan, apply, and destroy

  • Terraform init: initializes the working directory (local) which consists of all the configuration files. (.terraform)

  • Terraform validate: checks the code for syntax errors and ensures that it is internally consistent. It only looks at the configuration files (*.tf) in the current working directory

  • Terraform plan: compares the Terraform state with the as-is state in the cloud, builds and displays an execution plan. This does not change the deployment (read-only).

  • Terraform apply: executes the plan. This potentially changes the deployment.

  • Terraform destroy: deletes all resources that are governed by this specific Terraform environment.

Setting up Terraform Environment

To get started with Terraform, you need to:

  1. Install Terraform:

    Download the Terraform binary suitable for your operating system from the official Terraform website (https://www.terraform.io/downloads.html).

    Ensure that your system is up to date and you have installed the gnupg, software-properties-common, and curl packages installed. You will use these packages to verify HashiCorp's GPG signature and install HashiCorp's Debian package repository.

     sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
    
     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
    

    Verify the installation using terraform --version.

  2. Configure Providers: Terraform requires provider configurations for your target cloud or infrastructure platform. You can set up configurations for providers like AWS, Azure, or GCP by providing API keys, authentication tokens, and other required credentials.

Write providers.tf file which defines the region we want the infrastructure to be created

  1. Create Terraform Configuration Files: Write Terraform configuration files (usually with a .tf extension) that defines the infrastructure resources and their configurations.

  2. For example- if we want to configure a AWS cloud provider we create a file called terraform.tf and write the source and version. You can use the link to know the latest version -https://registry.terraform.io/providers/hashicorp/aws/latest/docs

  3. Initialize the Working Directory: Run terraform init in your project directory to initialize the working directory and download any necessary plugins or modules.

  4. Terraform plan :- preview the changes that Terraform will make to your infrastructure based on your configuration. It helps you understand what Terraform intends to do before you apply those changes.

image

  • Review the output and ensure it matches your intentions.

  • If everything looks good, execute the following command to apply the changes and create the infrastructure:

  •     Terraform apply
    

    image

    This shows the instance is created , up and running

  1. Destroy Infrastructure:
   terraform destroy

With one single command, you can destroy your whole infrastructure.๐Ÿค–

image

If you recheck the EC2 dashboard, you will see the instance got terminated.

Conclusion of Terraform and Its Significance in Tech Terms:

Terraform is a vital Infrastructure as Code (IaC) tool that streamlines the management of tech infrastructure. It empowers automation, supports multi-cloud architecture, employs declarative syntax, offers change preview, ensuring efficient, scalable, and consistent infrastructure provisioning. Terraform serves as a cornerstone for modern DevOps practices and facilitates seamless cloud adoption.

Happy Learning peeps :-)

1
Subscribe to my newsletter

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

Written by

Krishan Sharma
Krishan Sharma