Mastering Terraform: A Comprehensive Guide to Understanding, Implementing, and Installing Terraform on Cloud9

Amudha Balamurugan
6 min readJun 26, 2024

--

Learning new things always enlightens your mind. We here at Level Up in Tech, learn by doing. This June we learned about Terraform and did three projects. Before starting projects, it is good to have a nice intro about the subject, and setting up the environment for the projects will make life easier and healthier. Let us get familiar with the most powerful, popular Infrastructure as a Code tool, Terraform. Being a cloud engineer, it is vital to have a friendship with this guy.

Terraform is an infrastructure as a code tool that lets you build, change, and version cloud and on-prem resources safely and efficiently. It is an open-source software designed by Hashicorp, now IBM has acquired Terraforn.

Key Learning Outcomes:

  1. What is Terraform?
  2. How does Terraform work?
  3. Why Terraform?
  4. Basic Installation of Terraform on your computer
  5. Terraform on AWS Cloud9

Excited? Let’s go!

What is Terraform?

HashiCorp Terraform is an infrastructure as a code tool that lets you define cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. You can then use a consistent workflow to provision and manage your infrastructure throughout its lifecycle. Terraform can manage low-level components like compute, storage, and networking resources, as well as high-level components like DNS entries and SaaS features.

How does Terraform work?

Terraform creates and manages resources on cloud platforms and other services through their application programming interfaces (APIs). Providers enable Terraform to work with virtually any platform or service with an accessible API.

HashiCorp and the Terraform community have written thousands of providers to manage various resources and services. You can get all publicly available providers at the Terraform Registry, including Amazon Web Services (AWS), Azure, Google Cloud Platform (GCP), Kubernetes, Helm, GitHub, Splunk, DataDog, and many more.

The core Terraform workflow consists of three stages:

  • Write: You define resources, which may be across multiple cloud providers and services. For example, you might create a configuration to deploy an application on virtual machines in a Virtual Private Cloud (VPC) network with security groups and a load balancer.
  • Plan: Terraform creates an execution plan describing the infrastructure it will create, update, or destroy based on the existing infrastructure and your configuration.
  • Apply: On approval, Terraform performs the proposed operations in the correct order, respecting any resource dependencies. For example, if you update the properties of a VPC and change the number of virtual machines in that VPC, Terraform will recreate the VPC before scaling the virtual machines.
Terraform Workflow

Why Terraform?

1. Manage any infrastructure:

Terraform Registry offers cloud providers for many of the platforms and services. We can create on our own too. Terraform takes an immutable approach to infrastructure, reducing the complexity of upgrading or modifying services and infrastructure.

2. Track your infrastructure

Terraform generates a plan and prompts you for your approval before modifying your infrastructure. It also keeps track of your real infrastructure in a state file, a source of truth for your environment. Terraform uses the state file to determine the changes to make to your infrastructure so that it will match your configuration.

3. Automate changes

Terraform configuration files are declarative, meaning they describe your infrastructure's end state. You do not need to write step-by-step instructions to create resources because Terraform handles the underlying logic. Terraform builds a resource graph to determine resource dependencies and creates or modifies non-dependent resources in parallel. This allows Terraform to provision resources efficiently.

4. Standardize configurations

Terraform supports reusable configuration components called modules that define configurable collections of infrastructure, saving time and encouraging best practices. You can use publicly available modules from the Terraform Registry, or write your own.

5. Collaborate

Since your configuration is written in a file, you can commit it to a Version Control System (VCS) and use HCP Terraform to manage Terraform workflows across teams. HCP Terraform runs Terraform in a consistent, reliable environment and provides secure access to shared state and secret data, role-based access controls, a private registry for sharing both modules and providers, and more.

Install Terraform:

To use Terraform you will need to install it. HashiCorp distributes Terraform as a binary package. You can also install Terraform using popular package managers.

Follow this video to install Terraform on your system.

Terraform Installation

Install Terraform | Terraform | HashiCorp Developer

Terraform on AWS Cloud9:

AWS Cloud9 is a cloud-based integrated development environment (IDE) that lets you write, run, and debug your code with a browser. It includes a code editor, debugger, and terminal. Cloud9 comes prepackaged with essential tools for popular programming languages, including JavaScript, Python, PHP, and more, hence you don’t need to install files or configure your development machine to start new projects.

I am using AWS Cloud9 as my IDE for all my terraform projects.

Let’s create an IDE in AWS Cloud9. Go to the AWS console and select Cloud9 service.

  1. Click “Create environment’ on Cloud9.
  2. Give a name for your IDE.
  3. Select ‘New EC2 Instance’ with t2.micro as instance type, Amazon Linux 2023 as platform, and with Amazon System Manager (SSM) from Network settings as shown in the pictures below.
Create Environment Part — 1
Create Environment Part — 2

Once the environment is ready, you open it to work on browser-based IDE with git, and Amazon CLI all pre-installed. Terraform is not installed by default. so follow these instructions to install Terraform.

environment — Terra
Cloud9 IDE

Check for the versions of AWS CLI, Git, Python and Terraform. You can see terraform is not pre-installed like Git or Python.

Installed Versions

Now install Terraform on your IDE. Type the following commands on your terminal one by one.

sudo yum update
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
Installation 1
Installation 2

Terraform is Installed Successfully. That shows our environment is ready to work on terraform.

If you found this article helpful, please hit the Follow 👉 and Clap 👏 buttons to help me write more articles like this.
Thank You 🖤

👉Follow me on Medium | LinkedIn | GitHub

References:

  1. https://developer.hashicorp.com/terraform/docs
  2. https://registry.terraform.io/
  3. https://docs.aws.amazon.com/cloud9/

--

--