Installing and Configuring Terraform for Your Environment

Amish KohliAmish Kohli
3 min read

As you embark on your journey into Infrastructure as Code (IaC) with Terraform, the first crucial step is setting up Terraform on your local machine. In this guide, we'll walk through the installation process and initial configuration, ensuring you're ready to start defining and managing your infrastructure as code.

"terraform" installation done just by dropping and playing the terraform binary or executable

Step 1: Downloading Terraform

Terraform is distributed as a standalone binary, making installation straightforward. Follow these steps to download Terraform:

  1. Visit the official Terraform website: Terraform Downloads.

  2. Identify the appropriate version for your operating system (Windows, macOS, or Linux).

  3. Download the binary and save it to a directory on your machine.

Step 2: Installing Terraform

Windows:

  1. Extract the downloaded zip file.

  2. Move theterraform.exe binary to a directory included in your system's PATH.

  3. Open a command prompt and verify the installation by typing:

terraform -version

macOS:

  1. Extract the downloaded archive.

  2. Move theterraform binary to a directory in your PATH (e.g., /usr/local/bin/).

  3. Open a terminal and verify the installation.

terraform -version

AWS Linux:

  1. Extract the downloaded archive.

  2. Move theterraform binary to a directory in your PATH (e.g., /usr/bin/terraform).

  3. Open a terminal and verify the installation.

sudo yum install -y yum-utils shadow-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform

terraform -version

If the installation was successful, you should see Terraform's version information, indicating that it's ready for use.

Step 3: Configuring Terraform

Before you start using Terraform, it's essential to configure it with your preferred settings and credentials. Follow these steps to configure Terraform:

  1. Create a working directory.

    Organize your Terraform projects by creating a dedicated directory for each. Navigate to your desired workspace.

     mkdir my_terraform_project
     cd my_terraform_project
    

  2. Initialize Terraform:

    Run the following command to initialize Terraform in your project directory:

     terraform init
    

    This command downloads the necessary provider plugins and sets up your Terraform environment.

  3. Configure provider credentials:

    If you're working with a cloud provider (e.g., AWS, Azure), you'll need to configure your provider credentials. Use the command "aws configure" for configuring AWS credentials:

     aws_access_key = "your_access_key"
     aws_secret_key = "your_secret_key"
    

    Make sure to replace "your_access_key" and "your_secret_key" with your actual credentials.

  4. (Optional) Configure additional settings:

    Explore advanced configurations in the terraform block in your main.tf file. For example, set a preferred backend for storing Terraform state remotely.

     main.tf
    
     terraform {
       backend "s3" {
         bucket = "my-terraform-state-bucket"
         key    = "terraform.tfstate"
         region = "us-west-2"
       }
     }
    

    Modify these settings according to your project requirements.

Running Multiple versions of terraform on same machine

By default, we run a single Terraform version on our machine. But we can make use of multiple versions of Terraform on the same machine. By copying various terraform versions inside, /usr/bin/terraform* it will look like this:

/usr/bin/terraform_122 #rename terraform 1.2.2 binary

/usr/bin/terraform_157 #rename terraform 1.5.7 binary

Configure default version by creating symbolic link as:

/usr/bin/terraform -> /usr/bin/terraform_122

If I want to use version 1.5.7 for my work, I can explicitly use the corresponding version as follows:

terraform_157 --version

terraform_157 init

terraform_157 plan

If I want to use version 1.2.2 for my work, I can explicitly use the corresponding version as follows:

terraform_122 --version

As we can see, Terraform is symlinked to/usr/bin/terraform_122, the default version is set to 1.2.2 on your system, and it can be used as usual, i.e.

terraform --version

Conclusion:

Congratulations! You've successfully installed and configured Terraform for your environment. With Terraform initialized and your credentials set up, you're ready to start defining infrastructure as code. In the next blog post, we'll explore the basics of creating your first Terraform configuration and provisioning resources. Stay tuned for an exciting journey into the world of infrastructure as code with Terraform!

0
Subscribe to my newsletter

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

Written by

Amish Kohli
Amish Kohli