Skip to content

Latest commit

 

History

History
211 lines (139 loc) · 5.85 KB

LAB09-Workspaces-EC2.md

File metadata and controls

211 lines (139 loc) · 5.85 KB

LAB-09: Workspaces => Provision EC2 with Different tfvars Files

This scenario shows:

  • how to create, manage workspaces using EC2 and variables.

Code: https://github.com/omerbsezer/Fast-Terraform/tree/main/labs/workspace

Prerequisite

Steps

  • 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

image

  • 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

image

  • 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

image

  • 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

image

  • 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

image

  • To list current workspaces on the local:
terraform workspace list

image

  • 2 workspaces are created (DEV, PROD):
terraform workspace new dev
terraform workspace new prod
terraform workspace list

image

  • Switch to DEV workspace, show current workspace and initialize terraform:
terraform workspace select dev
terraform workspace show
terraform init

image

  • 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:

image

  • 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"

image

  • On AWS, it creates prod EC2:

image

  • With workspace, 2 state files are created for each workspace:

image

  • Switch to DEV workspace, destroy infrastructure only in DEV workspace:
terraform workspace select dev
terraform destroy -var-file="terraform-dev.tfvars"  

image

  • On AWS, ONLY dev EC2 is terminated:

image

  • Switch to PROD workspace, destroy infrastructure only in PROD workspace:
terraform workspace select prod
terraform destroy -var-file="terraform-prod.tfvars"  

image

  • On AWS, ONLY prod EC2 is terminated:

image

References