Terraform Workspaces

Always learning
4 min readJan 14, 2024

--

Terraform is an IAC tool, used primarily by DevOps teams to automate various infrastructure tasks. The provisioning of cloud resources, for instance, is one of the main use cases of Terraform. It’s a cloud-agnostic, open-source provisioning tool written in the Go language and created by HashiCorp.

buymeacoffee ☕ 👈 Click the link

Ec2-instance-creation

Create a main.tf file

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

variable "ami" {
description = "value"
}

variable "instance_type" {
description = "value"
}

module "Ec2_Instance" {
source = "./modules/Ec2_Instance"
ami = var.ami
instance_type = var.instance_type

}

Terraform.tfvars

ami = "ami-0c7217cdde317cfec"
instance_type = "t2.micro"

Inside modules → Ec2_Instance create a main.tf file

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

variable "ami" {
description = "This is AMI for the instance"
}

variable "instance_type" {
description = "This is the instance type, for example: t2.micro"
}

resource "aws_instance" "example" {
ami = var.ami
instance_type = var.instance_type
subnet_id = "subnet-09fd9e51af86f6313"

}

Initialize Terraform

Initializes the Terraform working directory, downloading any necessary provider plugins.

terraform init

Terraform plan command creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure.

When Terraform creates a plan it → Reads the current state of any already-existing remote objects to make sure that the Terraform state is up-to-date.

terraform plan

Apply the Configuration

Create the AWS resources defined in your Terraform configuration

terraform apply

Verify Resources

After Terraform completes the provisioning process, you can verify the resources created in the AWS Management Console.

Automatically create a tfstate file

terraform show

Create a staging file inside Ec2_Instance directory

ami = "ami-0c7217cdde317cfec"
instance_type = "t2.medium"

Goto the right directory apply the staging file

terraform plan

Finally apply the terraform cmd

terraform apply -var-file=stage.tfvars

Check the folder the tfstate files create or not?

Total tree structure

The apply to the total directory so create a workspace.

Workspaces allow users to manage different sets of infrastructure using the same configuration by isolating state files.

Now, imagine you want to deploy this server in three environments:

  1. Development
  2. Staging
  3. Production

To do this using workspaces, you first create a workspace called dev using the terraform command

terraform workspace new dev

Create a prod, stage environments

Switching between environments

Switch between environments, you need to switch between workspaces

terraform workspace list
terraform workspace select dev # Switch a another environment
terraform workspace show

Initialize the dev environment

terraform init
terraform plan
terraform apply

Finally check the directory the tfstate created totally (or) particular environment?

Every time mention this cmd

terraform apply -var-file=stage.tfvars

So, add to the yaml files in dev, stage, prod environment

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

variable "ami" {
description = "value"
}

variable "instance_type" {
description = "value"
type = map(string)

default = {
"dev" = "t2.micro" # Add
"stage" = "t2.medium" # Add
"prod" = "t2.xlarge" # Add
}
}

module "ec2_instance" {
source = "./modules/Ec2_Instance"
ami = var.ami
instance_type = lookup(var.instance_type, terraform.workspace, "t2.micro")
}

Switch a prod workspace

terraform workspace select prod
terraform apply

Finally destroy the resources

terraform destroy

Thank you 🙏 for taking the time to read our blog.

--

--

Always learning
Always learning

Written by Always learning

கற்றுக் கொள்ளும் மாணவன்...

No responses yet