Managing Infra with Terraform

Jigar R
3 min readJan 20, 2025

--

Every IT company needs a place to run its website, internal projects, client-facing websites, database, and many other things.
To support this, networks need to be created, machines need to be started up with certain memory, CPU, and software, and so on. These individual components lay the foundation for an IT company’s software infrastructure.

In the case of the Cloud, all of these resources can be specified in a text file (Checkout my earlier post intro-to-infrastructure-as-code). This text file is processed by tools like Terraform to create various infrastructure components.

Let’s take an example: you want to create a bucket to store data on different cloud platforms like AWS, GCP, and, Azure. Assuming, Terraform has been installed locally, We can do this with Terraform in 3 steps:

  1. Download the Terraform plugin for AWS, GCP, and, Azure
  2. Add text file to create a bucket on AWS, GCP, and, Azure
  3. Run “terraform apply” to create buckets

The plugins are also called Terraform provider. A full list of various providers at Terraform Regsitry. Using “AWS” terraform providers, one can create various resources like S3, RDS, EC2 and so on.




## From https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket
resource "aws_s3_bucket" "example" {
bucket = "my-tf-test-bucket"

tags = {
Name = "My bucket"
Environment = "Dev"
}
}
A Terraform provider consists of several resources

Step 1: Download plugins

Create a blank folder for our little project. Specify plugins/providers that we want to use in provider.tf file. To get started, we can specify credentials to use with AWS, GCP and Azure as environment variables and configure provider.tf file appropriately.

## provider.tf
## From https://registry.terraform.io/providers/hashicorp/aws/latest/docs
provider "aws" {
region = "us-west-2"
access_key = "my-access-key"
secret_key = "my-secret-key"
}

In the following screenshot, all the providers used by Project A are listed in provider.tf. Upon running the “terraform init” command, Terraform would download providers in the “.terraform” folder. Terraform can download providers from Registry or it can get it from local-file-system.

State of ProjectA after running “Terraform init”

Step 2: Specify resources to create

For a small project like this, we can provide the names of the data bucket for AWS, GCP, and Azure in the “main.tf”. It can also be in separate files like “aws.tf”, “gcp.tf” and so on.

Step 3: run “terraform apply” command

To create resources, run “terraform apply”. Terraform keeps track of resources in a state file named “terraform.tfstate”. It’s a good idea to store the “terraform.tfstate” file on the cloud service like Terraform Cloud or AWS S3.

The upcoming medium blog will provide more information on the state file.

Reference

--

--

Jigar R
Jigar R

Written by Jigar R

DevOps Engineer | feel free to reach out to me | LinkedIn — https://www.linkedin.com/in/jigarrathod/

No responses yet