Just to start with a little bit of theory! What exactly terraform is or the hyped IaC (infrastructure as code)?

IaC does sounds like a magical concept. If you are new to infrastructure as code as a concept, it is the process of managing infrastructure in a file or files rather than manually configuring resources in a user interface.

Terraform is the infrastructure as code offering from HashiCorp. It is a tool for building, changing and managing infrastructure in a safe, repeatable way. Operators and Infrastructure teams can use Terraform to manage environments with a configuration language called the HashiCorp Configuration Language (HCL) for human-readable, automated deployments.

In this article we focus on creating and managing different environments through terraform. Every organization maintains a different set of resources for different stages of software development. Let’s say these stages are dev, test, and prod.

While using terraform we can maintain the same set of files and create parallel infrastructure in no time. This is made possible through workspaces.

Enough of theory!! Let’s dive into implementation directly.

I am using my AWS account and Amazon Linux2 EC2 instance to execute all the coding stuff for this article. I have already installed a terraform on my machine. Link to installation guide:-
https://learn.hashicorp.com/terraform/getting-started/install.html

A security best practice to start with! I have attached an IAM role to my EC2 instance which gives it admin access instead of user credentials.

Attach Replace IAM Role Terraform Instances

Attach Replace IAM Role Terraform Instances 2

I created a file named provider.tf and initialized my current directory with terraform init.

provider "aws" {
	region = "us-east-1"
}

Let’s see what all actions we can do with terraform workspace command.

Terraform Workspace Command

Initially terraform creates a workspace for us by default named “default”.

In this article we create 2 workspaces named dev and prod.

To create a new workspace execute terraform workspace new

Execute Terraform Workspace

To switch between different workspaces execute terraform workspace select . The little star/asterisk in the list of workspaces indicates that we dev workspace is selected by default.

Switch Between Terraform Workspace

Now to keep it simple and give you an idea of how we can create 2 different sets of resources using a single set of files in terraform I will create a file named instance.tf and this will hold our code to create resources with for dev and prod deployment environment. I have also created a variables.tf file which holds out variables and it’s a good thing to work with variables.

So the plan is to create more and bigger instances in our prod environment which makes sense as well.

variables.tf

variable "image_id" {
	type = string
	default = "ami-09d95fab7fff3776c"
}
variable "types" {
	type = map
	default = {
		prod = "t2.large"
		dev = "t2.medium"
		default = "t2.medium"
	}
}
variable "count_of_instances" {
	type = map
	default = {
		prod = 5
		dev = 2
		default = 2
	}
}

instance.tf

resource "aws_instance" "instances" {
	count = lookup(var.count_of_instances, terraform.workspace)
	instance_type = lookup(var.types, terraform.workspace)
	ami = var.image_id
	tags = {
		Name = "web - ${terraform.workspace}"
	}
}

Now as you remember from before I have selected dev environment as I don’t want to get billed by AWS for creating so many instances large instances.

First I do terraform plan and then do terraform apply.

Terraform Workspace Plan and Commands

Terraform Workspace Plan and Apply

As you can see in the instances for our dev environment are up and running. There can be a variety of use cases that we can cover with workspaces. The idea of this blog was to show how terraform can be a very handy tool and reduce the operational overhead of how we do stuff. Go ahead and do a variety of stuff using this concept like creating a dev RDS database with no read replica and a prod RDS database with read replica. There is an ocean of opportunities!

Sajal Tyagi

Sajal Tyagi

Technical Consultant · DevOps Engineer · Quality Engineer · Software Engineer

Sajal Tyagi is a seasoned Cloud and DevOps Professional, pursuing MSc Management at Trinity College Dublin. He is Terraform, Vault, AWS Certified Solution Architect, Big Data and Oracle Autonomous Database Cloud specialist.