Terraform-Workspace..!!

2 min read

What Is Workspace In terraform..?
we can create perticular thinks in perticular space
A workspace in Terraform allows you to create and manage multiple independent state files within the same Terraform configuration. This is useful for managing different environments like dev, staging, and prod without duplicating code
We Can Create multiple workspaces
How To See Our Workspaces
terraform workspace list
This * Simball Show Your current Worspace
How To Create New WorkSpace
terraform workspace new dev
How to Change Workspace
terraform worspace select default
Let’s Create different instances in different workspace..!
that Related create main.tf file & variable.tf file
nano main.tf
Code For Main.tf File
terraform {
required_version = "~> 1.1"
required_providers {
aws = {
version = "~>3.1"
}
}
}
provider "aws" {
region = var.my_region
}
resource "aws_instance" "web" {
ami = var.my_ami
instance_type = lookup (var.inst_type,terraform.workspace)
tags = {
Name = "myserver"
}
}
Create variable.tf file
nano variable.tf
Code For Variable.tf File
variable "my_region" {
type = string
default = "ap-south-1"
}
variable "my_ami" {
type = string
default = "ami-00bb6a80f01f03502"
}
variable "inst_type" {
type = map
default = {
default = "t2.micro"
dev = "t2.nano"
test = "t2.small"
prod = "t2.medium"
}
}
After Adding This Code And Creating File First Initialized Your Directory Following Command
terraform init
Create A Instance t2.micro for default workspace
terraform workspace select default
terraform plan
Create A Instance t2.medium for prod workspace
terraform workspace select prod
terraform plan
0
Subscribe to my newsletter
Read articles from prasad kale directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
