Related Resources

Our Services

You May Also Like

Getting started with Terraform

Abhilash

Jan 20, 2023

6 min readLast Updated Jan 20, 2023

Welcome to our blog on Getting Started with Terraform! If you're new to Terraform, you're in the right place. Terraform is a powerful tool that allows you to define, preview, and deploy infrastructure as code. This means that you can use a single configuration file to manage resources across multiple providers, including public cloud providers like AWS, Azure, and Google Cloud, as well as on-premises and edge infrastructure.

One of the main benefits of using Terraform is that it enables you to version your infrastructure, just like you would with code. This makes it easy to track changes and roll back if needed. It also makes it easier to collaborate with your team, as you can share and review configuration files in the same way you would with code.

In this blog, we'll cover the basics of Terraform and walk you through the process of getting started. We'll also provide tips and best practices along the way to help you get the most out of Terraform.

So let's dive in and start using Terraform to manage your infrastructure!

1. Install Terraform

The first step in getting started with Terraform is to install it on your local machine. Terraform is a command-line tool, so you can download the appropriate binary for your operating system from the Terraform website. Once you have it downloaded, make sure it is in your system's PATH so that you can run it from the command line.

For macOS, you can use the below command to install terraform

brew tap hashicorp/tap
brew install hashicorp/tap/terraform
macOS

For Linux[Ubuntu/Debian], you can use the below command to install terraform

wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /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
Ubuntu/Debian

For Windows, you can directly download the binary from here

2. Initialize a Terraform project

Once you have Terraform installed, you can initialize a new Terraform project by running the terraform init command. This command will create a new folder called ".terraform" in your current working directory, where Terraform will store all of its state files.

Prerequisites

To follow this tutorial you will need the following:

  • The Terraform CLI is installed.
  • The AWS CLI is installed.
  • AWS account and associated credentials that allow you to create resources.
terraform init  //assuming AWS and Terraform CLI is installed and configured

3. Write your Terraform Configuration

Next, you'll need to write your Terraform configuration. The configuration is written in the HashiCorp Configuration Language (HCL), and it describes the resources you want to create. For example, if you want to create an AWS EC2 instance, you would write the following configuration:

resource "aws_s3_bucket" "gallery" {
  bucket = "mygalleryapptrt"

  tags = {
    Name        = "GalleryApp"
    Environment = "Dev"
  }
}
Creating s3 bucket

4. Validate your Terraform Configuration

You'll need to validate your Terraform configuration. This can be done using the "terraform plan" command. The "terraform plan" command is used to create an execution plan for your Terraform configuration. It allows you to preview the changes that Terraform will make before applying them to your infrastructure, making it easier to avoid costly mistakes.

terraform plan

When you run the "terraform plan" command, Terraform will check the current state of your infrastructure, compare it to the configuration file, and generate a plan of the changes that will be made. The plan will show you the resources that will be created, updated, or destroyed, along with the details of the changes that will be made. This allows you to review the changes and make sure that they are what you intended before applying them to your infrastructure.

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_s3_bucket.gallery will be created
  + resource "aws_s3_bucket" "gallery" {
      + acceleration_status         = (known after apply)
      + acl                         = (known after apply)
      + arn                         = (known after apply)
      + bucket                      = "mygalleryapp"
      + bucket_domain_name          = (known after apply)
      + bucket_regional_domain_name = (known after apply)
      + force_destroy               = false
      + hosted_zone_id              = (known after apply)
      + id                          = (known after apply)
      + object_lock_enabled         = (known after apply)
      + policy                      = (known after apply)
      + region                      = (known after apply)
      + request_payer               = (known after apply)
      + tags                        = {
          + "Environment" = "Dev"
          + "Name"        = "GalleryApp"
        }
      + tags_all                    = {
          + "Environment" = "Dev"
          + "Name"        = "GalleryApp"
        }
    ........... 
Plan: 1 to add, 0 to change, 0 to destroy.
Sample Terraform plan output for the above terraform configuration

5. Apply the Configuration

Once you are satisfied with the changes that Terraform will make, you can apply the configuration by running the "terraform apply" command. This will create or update all the resources defined in your configuration, and Terraform will keep track of all the resources it creates in its state files, allowing you to manage and update them in the future.

terraform apply

It's important to note that "terraform apply" command should be used with caution, as it will make real changes to your infrastructure. It is also important to keep in mind that any errors that occur during the execution of "terraform apply" command might cause the provisioning to be in an unknown state, so always validate the changes with "terraform plan" command before applying them.

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value:
Once Terraform apply command is entered 

you need to confirm the changes before typing yes and then the changes will be applied to your cloud.

aws_s3_bucket.gallery: Creating...
aws_s3_bucket.gallery: Still creating... [10s elapsed]
aws_s3_bucket.gallery: Creation complete after 10s [id=mygalleryapptrt]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Output for terraform apply

In conclusion, Terraform is a game-changer for managing your infrastructure, providing a unified and efficient way to manage resources across different cloud providers, while also allowing you to version, collaborate and roll back changes. The unique features of Terraform make it

Terraform is a powerful infrastructure as code (IAC) tool that allows users to provision and manage resources across multiple cloud providers and on-premises environments. Some of its key features include:

  • Cross-platform compatibility: Terraform supports a wide range of cloud providers and on-premises environments, including AWS, Azure, Google Cloud, and more. This allows users to manage their entire infrastructure using a single tool, regardless of where their resources are located.
  • State management: Terraform keeps track of the current state of your infrastructure, allowing you to easily see what resources are currently running, and making it easy to roll back changes if necessary.
  • Modularity: Terraform's use of modules makes it easy to organize and reuse infrastructure code. By breaking your infrastructure down into smaller, reusable modules, you can reduce duplication, and increase scalability and maintainability.
  • Versioning: Terraform's ability to version control your infrastructure code and state files, makes it easy to collaborate and track changes over time.
  • Previewing changes: Terraform's "plan" command allows you to preview the changes that will be made to your infrastructure before they're applied. This makes it easy to catch errors before they happen and ensure that your infrastructure stays in the desired state
  • Automation: With Terraform, you can automate the process of provisioning, updating, and deleting resources, which can save you a lot of time and effort.
  • Open source tool: Terraform is an open-source tool and is actively maintained by a community of developers and contributors around the world which makes it more reliable and efficient to use.

In summary, Terraform is an efficient and powerful tool for managing infrastructure across multiple cloud providers and environments. Its features enable users to easily provision and manage resources, preview changes, automate processes, version control and maintain their infrastructure in a modular way.

FAQs

1. What is IAC?

IAC stands for Infrastructure as Code. It's a way of managing and provisioning IT infrastructure using code, rather than manual configuration.

2. What is Terraform?

HashiCorp Terraform is a powerful, open-source tool that allows engineers to automate the process of creating and managing IT infrastructure. With Terraform, you can use code to define the resources your application needs and then use that code to provision and manage those resources in a consistent and repeatable way.

3. When do we need to use terraform?

Mainly terraform is used for provisioning the infrastructure in the cloud. apart from that it is also used to automate infrastructure deployments and manage multiple environments.

Projects Completed till now.

Discover how we can help your business grow.

"Third Rock Techkno's work integrates complex frameworks and features to offer everything researchers need. They are open-minded and worked smoothly with the academic subject matter."

- Dr Daniel T. Michaels, NINS