Terraform Unveiled: A Beginner’s Journey

Satish Rundla
Cypik
Published in
4 min readApr 11, 2024

Terraform is a robust Infrastructure as Code (IaC) tool that allows you to define and provision infrastructure using a declarative configuration language. In this beginner-friendly guide, we’ll cover some essential Terraform basics to help you kickstart your journey.

Terraform Registry

The Terraform Registry serves as a central hub where you can discover providers and modules to enhance your Terraform projects.

  • Providers: These serve as interfaces to APIs, enabling the creation of resources in Terraform. A variety of providers can be found on the registry, including the Random Terraform Provider.
  • Modules: Modules help you organize your Terraform code, making it modular, portable, and easily shareable. They encapsulate reusable components of your infrastructure.

Terraform Terminal

To explore a list of all Terraform commands, simply type terraform in your terminal. This provides an overview of available commands along with their descriptions.

Terraform Init

When starting a new Terraform project, the initial step is to run terraform init. This command downloads the necessary binaries for the providers you'll be using in your project.

Terraform Plan

The terraform plan the command generates a changeset outlining proposed modifications to your infrastructure. It provides insights into what Terraform will modify or create. You can output this plan for review or pass it directly to an applicant.

Terraform Apply

To execute the planned changes, utilize terraform apply. This command prompts you to confirm the changes before applying them. For automated approval, use the --auto-approve flag, such as terraform apply --auto-approve.

Terraform Secure Files

The .terraform.lock.hcl file contains versioning information for the providers or modules used in your project. It's crucial to commit this file to your Version Control System (VCS), like GitHub.

Terraform State Files

The .terraform.tfstate file holds information about the current state of your infrastructure. Never commit this file to your VCS, as it may contain sensitive data. Losing this file means losing track of your infrastructure state. The .terraform.tfstate.backup file is a backup of the previous state.

Terraform Directory

The .terraform directory contains the binaries of Terraform providers.

Challenges with Terraform Cloud Access and Gitpod Environment

When attempting to run terraform login in Gitpod VSCode, there might be issues launching the interactive view. A workaround is to generate a token in Terraform Cloud manually:

  1. Visit Terraform Cloud Tokens.
  2. Create and copy your token.

Open a terminal and create the credentials file:

touch /home/gitpod/.terraform.d/credentials.tfrc.json
open /home/gitpod/.terraform.d/credentials.tfrc.json

Provide the following code, replacing “YOUR-TERRAFORM-CLOUD-TOKEN” with your actual token:

{
"credentials": {
"app.terraform.io": {
"token": "YOUR-TERRAFORM-CLOUD-TOKEN"
}
}
}

Semantic Versioning

This project follows semantic versioning, with versions formatted as MAJOR.MINOR.PATCH. Major versions indicate incompatible API changes, minor versions add backward-compatible functionality, and patch versions include backward-compatible bug fixes.

Install the Terraform CLI

Ensure you follow the latest Terraform CLI installation instructions via the official documentation. Considerations for Linux distribution are mentioned, so adapt accordingly.

Refactoring into Bash Scripts

To address Terraform CLI GPG keyring changes, a bash script (./bin/install_terraform_cli) has been created. This allows for a cleaner Gitpod Task File (./.gitpod.yml), easier debugging, manual execution, and better portability for installing Terraform CLI in other projects.

Shebang Considerations

The recommended shebang for bash scripts is #!/usr/bin/env bash for portability across different OS distributions.

Execution Considerations

Execute bash scripts using the ./ shorthand notation. If using a script, use source to interpret the script.

Linux Permissions Considerations

Make bash scripts executable with the following commands:

chmod u+x ./bin/install_terraform_cli

Alternatively:

chmod 744 ./bin/install_terraform_cli

GitHub Lifecycle (Before, Init, Command)

Exercise caution with terraform init as it won't rerun if you restart an existing workspace. Refer to Gitpod workspace tasks for more information.

GitHub Lifecycle (Before, Init, Command)

Exercise caution with terraform init as it won't rerun if you restart an existing workspace. Refer to Gitpod workspace tasks for more information.

Working Environment Variables

List environment variables using the env command. Filter specific variables with, e.g., env | grep AWS_.

Setting and Unsetting Environment Variables

Set variables using export VAR_NAME='value' and unset using unset VAR_NAME. For temporary setting during command execution:

#!/usr/bin/env bash
VAR_NAME='value' ./bin/print_message

Printing Variables

Print an environment variable using echo $VAR_NAME.

Scoping of Environment Variables

New bash terminals in VSCode won’t be aware of variables set in other windows. To persist across all terminals, set environment variables in your bash profile (.bash_profile).

Maintaining Environment Variables in Gitpod

Persist environment variables in Gitpod using:

gp env VAR_NAME='value'

This stores variables in Gitpod Secrets Storage for all future workspaces.

AWS CLI Installation

Install the AWS CLI using the bash script ./bin/install_aws_cli. Verify AWS credentials with aws sts get-caller-identity.

Enjoy it! That’s It; we are done…

For hassle-free Cloud Management with DevOps at the center of the process, contact us at info@cypik.com
Cypik

About the author:
I’m Satish Chand Rundla, an experienced DevOps engineer at Cypik. With a strong focus on automating development processes, my expertise lies in the AWS cloud and Terraform platforms. Currently, I’m part of the team at Cypik, I strive to optimize software delivery and infrastructure management, aiming to increase efficiency and scalability in all aspects of our work.

--

--

Satish Rundla
Cypik
Writer for

DevOps Engineer || DevOps || Aws || Jenkins || Networking || Terraform || Github || Mysql || Ansible || Linux || Docker