DEV Community

Aisalkyn Aidarova
Aisalkyn Aidarova

Posted on

LAB: Terraform Alias + Workspace + Import (Production Style)

πŸ“ Project Structure (Skeleton)

terraform-alias-workspace-import-lab/
β”‚
β”œβ”€β”€ providers.tf
β”œβ”€β”€ variables.tf
β”œβ”€β”€ main.tf
β”œβ”€β”€ outputs.tf
β”œβ”€β”€ terraform.tfvars.example
β”‚
β”œβ”€β”€ backend.tf
β”‚
└── scripts/
    └── import.sh
Enter fullscreen mode Exit fullscreen mode

1️⃣ providers.tf

terraform {
  required_version = ">= 1.5.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

# Default provider (primary region)
provider "aws" {
  region = var.primary_region
}

# Alias provider (secondary region)
provider "aws" {
  alias  = "secondary"
  region = var.secondary_region
}
Enter fullscreen mode Exit fullscreen mode

2️⃣ variables.tf (NO hardcoding)

variable "project_name" {
  type = string
}

variable "environment" {
  type = string
}

variable "primary_region" {
  type = string
}

variable "secondary_region" {
  type = string
}

variable "bucket_name" {
  type = string
}

variable "common_tags" {
  type = map(string)
  default = {}
}
Enter fullscreen mode Exit fullscreen mode

3️⃣ main.tf

πŸ”Ή Uses:

  • workspace
  • alias provider
  • dynamic naming
locals {
  env = terraform.workspace

  name_prefix = "${var.project_name}-${local.env}"

  tags = merge(var.common_tags, {
    Project     = var.project_name
    Environment = local.env
    ManagedBy   = "Terraform"
  })
}

# Primary region bucket
resource "aws_s3_bucket" "primary" {
  bucket = "${local.name_prefix}-primary"

  tags = local.tags
}

# Secondary region bucket (alias usage)
resource "aws_s3_bucket" "secondary" {
  provider = aws.secondary

  bucket = "${local.name_prefix}-secondary"

  tags = local.tags
}
Enter fullscreen mode Exit fullscreen mode

4️⃣ outputs.tf

output "primary_bucket" {
  value = aws_s3_bucket.primary.bucket
}

output "secondary_bucket" {
  value = aws_s3_bucket.secondary.bucket
}

output "workspace" {
  value = terraform.workspace
}
Enter fullscreen mode Exit fullscreen mode

5️⃣ terraform.tfvars.example

project_name     = "jumptotech"
environment      = "dev"
primary_region   = "us-east-2"
secondary_region = "us-west-1"

common_tags = {
  Owner = "DevOpsTeam"
}
Enter fullscreen mode Exit fullscreen mode

6️⃣ backend.tf (Optional but Production Style)

terraform {
  backend "s3" {}
}
Enter fullscreen mode Exit fullscreen mode
terraform init \
  -backend-config="bucket=YOUR-TF-STATE-BUCKET" \
  -backend-config="key=alias-workspace/terraform.tfstate" \
  -backend-config="region=us-east-2" \
  -backend-config="dynamodb_table=terraform-locks"
Enter fullscreen mode Exit fullscreen mode

7️⃣ scripts/import.sh

#!/bin/bash

# Usage:
# ./import.sh <bucket_name>

BUCKET_NAME=$1

if [ -z "$BUCKET_NAME" ]; then
  echo "Usage: ./import.sh <bucket_name>"
  exit 1
fi

echo "Importing existing S3 bucket..."

terraform import aws_s3_bucket.primary $BUCKET_NAME
Enter fullscreen mode Exit fullscreen mode

πŸš€ STEP-BY-STEP EXECUTION


Step 1: Initialize

terraform init
Enter fullscreen mode Exit fullscreen mode

Step 2: Create Workspaces

terraform workspace new dev
terraform workspace new stage
terraform workspace new prod
Enter fullscreen mode Exit fullscreen mode

Switch:

terraform workspace select dev
Enter fullscreen mode Exit fullscreen mode

Step 3: Plan & Apply

terraform plan -var-file="terraform.tfvars"
terraform apply -var-file="terraform.tfvars"
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify Workspace Behavior

Switch workspace:

terraform workspace select stage
terraform apply -var-file="terraform.tfvars"
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Creates different buckets automatically


Step 5: IMPORT (IMPORTANT)

πŸ”₯ Scenario:

Bucket already exists (created manually)

aws s3 mb s3://jumptotech-dev-primary
Enter fullscreen mode Exit fullscreen mode

Now import:

./scripts/import.sh jumptotech-dev-primary
Enter fullscreen mode Exit fullscreen mode

Step 6: Verify Import

terraform plan
Enter fullscreen mode Exit fullscreen mode

Expected:

No changes. Infrastructure is up-to-date.
Enter fullscreen mode Exit fullscreen mode

🧠 KEY CONCEPTS (Explain to Students)


πŸ”Ή 1. Alias Provider

provider = aws.secondary
Enter fullscreen mode Exit fullscreen mode

➑️ Allows:

  • Multi-region
  • Multi-account

πŸ”Ή 2. Workspace

terraform.workspace
Enter fullscreen mode Exit fullscreen mode

➑️ Automatically separates:

  • dev
  • stage
  • prod

πŸ”Ή 3. Import

terraform import RESOURCE_NAME RESOURCE_ID
Enter fullscreen mode Exit fullscreen mode

➑️ Example:

terraform import aws_s3_bucket.primary my-bucket
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 4. Why Import is Critical

Without import:

  • Terraform will try to recreate resource
  • Can cause data loss / conflicts

πŸ”Ή 5. No Hardcoding

Everything comes from:

  • variables.tf
  • tfvars
  • workspace

🎯 REAL INTERVIEW QUESTIONS (FROM THIS LAB)

  1. What is provider alias and when do you use it?
  2. Difference between workspace and separate state files?
  3. What happens if you don’t import existing resources?
  4. Can you import into module?
  5. How do you manage multi-region deployments?
  6. What is terraform.workspace used for?

Top comments (0)