This scenario shows:
- how to create, manage workspaces using EC2 and variables.
Code: https://github.com/omerbsezer/Fast-Terraform/tree/main/labs/workspace
- You should have a look following lab:
-
With workspaces,
- a parallel, distinct copy of your infrastructure which you can test and verify in the development, test, and staging
- like git, you are working on different workspaces (like branch)
- single code but different workspaces
- it creates multiple state files
-
Workspace commands:
terraform workspace help # help for workspace commands
terraform workspace new [WorkspaceName] # create new workspace
terraform workspace select [WorkspaceName] # change/select another workspace
terraform workspace show # show current workspace
terraform workspace list # list all workspaces
terraform workspace delete [WorkspaceName] # delete existed workspace
- We have basic main.tf file with EC2, variables:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = var.location
}
locals {
tag = "${terraform.workspace} EC2"
}
resource "aws_instance" "instance" {
ami = var.ami
instance_type = var.instance_type
tags = {
Name = local.tag
}
}
Code: https://github.com/omerbsezer/Fast-Terraform/blob/main/labs/workspace/main.tf
- Variables.tf file:
variable "instance_type" {
type = string
description = "EC2 Instance Type"
}
variable "location" {
type = string
description = "The project region"
default = "eu-central-1"
}
variable "ami" {
type = string
description = "The project region"
}
Code: https://github.com/omerbsezer/Fast-Terraform/blob/main/labs/workspace/variables.tf
- For development workspace, terraform-dev.tfvars:
instance_type = "t2.nano"
location = "eu-central-1"
ami = "ami-0e067cc8a2b58de59" # Ubuntu 20.04 eu-central-1 Frankfurt
Code: https://github.com/omerbsezer/Fast-Terraform/blob/main/labs/workspace/terraform-dev.tfvars
- For product workspace, terraform-prod.tfvars:
instance_type = "t2.micro"
location = "eu-central-1"
ami = "ami-0d1ddd83282187d18" # Ubuntu 22.04 eu-central-1 Frankfurt
Code: https://github.com/omerbsezer/Fast-Terraform/blob/main/labs/workspace/terraform-prod.tfvars
- To list current workspaces on the local:
terraform workspace list
- 2 workspaces are created (DEV, PROD):
terraform workspace new dev
terraform workspace new prod
terraform workspace list
- Switch to DEV workspace, show current workspace and initialize terraform:
terraform workspace select dev
terraform workspace show
terraform init
- Create infrastructure in DEV workspace:
terraform plan -var-file="terraform-dev.tfvars" # for test, dry-run
terraform apply -var-file="terraform-dev.tfvars"
- On AWS, it creates dev EC2:
- Switch to PROD workspace, create infrastructure in PROD workspace:
terraform workspace select prod
terraform plan -var-file="terraform-prod.tfvars" # for test, dry-run
terraform apply -var-file="terraform-prod.tfvars"
- On AWS, it creates prod EC2:
- With workspace, 2 state files are created for each workspace:
- Switch to DEV workspace, destroy infrastructure only in DEV workspace:
terraform workspace select dev
terraform destroy -var-file="terraform-dev.tfvars"
- On AWS, ONLY dev EC2 is terminated:
- Switch to PROD workspace, destroy infrastructure only in PROD workspace:
terraform workspace select prod
terraform destroy -var-file="terraform-prod.tfvars"
- On AWS, ONLY prod EC2 is terminated: