🚀Terraform: Managing Multi-Environment Infrastructure

>>> Streamlined Multi-Environment Infrastructure Deployment Using Terraform Workspaces and Azure DevOps

Sai Manasa
5 min readSep 13, 2024
Managing Multi Environment Infra: Terraform by Sai Manasa

Hello World… In modern infrastructure management, handling multiple environments such as Development, Staging, and Production is a common challenge. Each environment requires careful isolation, configuration management, and automation to ensure consistency and reliability. Terraform, as an Infrastructure-as-Code (IaC) tool, offers several ways to efficiently manage multi-environment setups. Two of the most commonly used methods are Terraform Workspaces and the Terraform init, plan, applyworkflow via CI/CD pipelines, such as those in Azure DevOps (ADO). So, let’s understand this with a basic example…

Overview:

  • What is Terraform?
  • Terraform Workspaces
  • Terraform Workspaces Commands
  • Hands-On Guide: Using Workspaces
  • Hands-On Guide: ADO Pipelines
  • Source Code

What is Terraform?

It is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows users to define, provision, and manage infrastructure across multiple cloud providers and services using a simple, human-readable configuration language known as HCL (HashiCorp Configuration Language). Commonly used for Provisioning cloud infrastructure like virtual machines, networks, storage, and databases; Automating the scaling of cloud resources; and Managing multi-environment infrastructure (e.g., dev, staging, prod) with a consistent codebase.

Terraform Workspaces:

It enables us to manage multiple sets of deployments from the same set of the configuration file. Depending on the workspace being used, the value of a specific argument in the code can also change. Each workspace maintains a separate state file, which tracks the resources created for that environment. This is useful when you want to reuse the same code for different environments while keeping their infrastructure states isolated.

Terraform Workspaces by Sai Manasa

Terraform Workspaces Commands:

  • terraform workspace — Shows all the options of the workspace
  • terraform workspace list — Displays all the available workspaces in the current configuration.
  • terraform workspace new <workspace_name> — Creates a new workspace and switches to the newly created workspace.
  • terraform workspace select <workspace_name> — Switches to the specified workspace.
  • terraform workspace show — Shows the current workspace.
  • terraform workspace delete <workspace_name> — Deletes an empty workspace. A workspace can only be deleted if its state is empty (i.e., it has no resources managed by Terraform). You cannot delete the current workspace, and the default workspace cannot be deleted.
  • terraform workspace -help — Displays help for workspace commands.

Hands-On Guide: Using Workspaces

Step 1: Install Terraform

  • Install Terraform: here

Step 2: Writing Config Files

  • Create a directory for the project.
  • Now create the required config files.
  • main.tf — Defines the EC2 instance resource.
  • variables.tf — Defines variables used in the configurations.
  • outputs.tf — Defines outputs to get information about the EC2 instance.
  • dev.tfvars — Environment-specific variables for development.
  • staging.tfvars — Environment-specific variables for staging.
  • prod.tfvars — Environment-specific variables for production.

Step 3: Initialize Terraform

  • Here, initialize the terraform configuration using the command:
terraform init

Step 4: Configure Terraform Workspaces

  • Create workspaces for different environments.
terraform workspace new dev
terraform workspace new staging
terraform workspace new prod
  • List available workspaces:
terraform workspace list

Step 5: Deploy Infra to Dev Environment

  • Select the dev workspace:
terraform workspace select dev
  • Plan the configuration:
terraform plan -var-file="dev.tfvars"
  • Apply the configuration:
terraform apply -auto-approve -var-file="dev.tfvars"
  • Repeat the same for the remaining environments.

Step 6: Verification

  • Verify the launched EC2 instances in the AWS.

Hands-On Guide: Using Azure DevOps Pipeline

Step 1: Config Files

  • Write the terraform configuration files.
  • main.tf — Defines the EC2 instance resource.
  • variables.tf — Defines variables used in the configurations.
  • dev.tfvars — Environment-specific variables for development.
  • staging.tfvars — Environment-specific variables for staging.
  • prod.tfvars — Environment-specific variables for production.

Step 2: Store the AWS access keys.

  • The AWS access keys must be stored as ‘Pipeline Variables’ or ‘Environment Variables’ for securely managing and accessing AWS resources during pipeline execution.
  • These variables can be used at various scopes within Azure Pipelines.
  • Below snap is the process of how to store them:
  • We can also use Service Connection in this case.

Step 3: Write a pipeline definition in YAML

Step 4: Create a pipeline and run.

  • I’ve created the pipeline definition file in the Azure Repos itself.
  • Below snap is the process of how to create a pipeline and run it:
  • Before running the pipeline select the environment as shown below:

Step 5: Results

Let’s Connect:

Feel free to reach out, share your thoughts, or ask any questions you may have. I’m excited to engage with you and learn from each other as we navigate this exciting field!

LinkedIn: Sai Manasa

GitHub: Sai Manasa

Happy Learning 😄

--

--