Terraform Essentials: Alias & Providers, State Files, Workspaces, and Outputs

Terraform, an open-source infrastructure as code tool, enables developers to define and provision data center infrastructure using a declarative configuration language. In this blog, we delve into four essential Terraform features: Alias & Providers, State Files, Workspaces, and Outputs. Let’s also explore practical examples and commands to strengthen our understanding.

Alias & Providers

Providers in Terraform are responsible for interacting with APIs of the cloud platform or service you intend to configure. Sometimes, you may need multiple instances of the same provider within a single configuration—this is where alias comes into play.

Example Configuration

provider "aws" {
  region = "us-east-1"
}

provider "aws" {
  region = "ap-south-1"
  alias  = "mumbai"
}

resource "aws_instance" "one" { 
  tags = {
    Name = "my-${terraform.workspace}-server"
  }
  ami           = "ami-0166fe664262f664c"
  instance_type = "t2.micro"
  count         = 1
}

resource "aws_instance" "two" { 
  provider = aws.mumbai
  tags = {
    Name = "my-${terraform.workspace}-server"
  }
  ami           = "ami-0327f51db613d7bd2"
  instance_type = "t2.micro"
  count         = 1
}

output "abc" {
  value = [
    aws_instance.one[0].public_ip,
    aws_instance.one[0].private_ip,
    aws_instance.one[0].id
  ]
}

In this configuration, two instances are deployed in different AWS regions using the same provider, one with an alias.

State Files

State files are critical in Terraform as they store information about your infrastructure’s current state. This helps Terraform manage updates and ensure that your desired and actual states align.

Common Commands

  1. View State File
terraform show
  1. Remove Specific Resource/
terraform state rm aws_instance.one
  1. List Resources in State
terraform state list

Be cautious while working with state files to avoid unintended disruptions in your infrastructure.

Workspaces

Workspaces allow you to maintain multiple state files within the same directory. This is useful for managing environments like Dev, Testing, and Prod.

Managing Workspaces

  1. List Workspaces
terraform workspce list
  1. Create a New Workspace
terraform workspace new prod
  1. Select an Existing Workspace
terraform workspace select testing
  1. Delete a Workspace
terraform workspace delete prod

Each workspace maintains its own state file, allowing isolated management of environments.

Outputs

Outputs in Terraform provide visibility into key information about your resources. This is especially helpful for passing values between modules or for debugging purposes.

Example Configuration

output "abc" {
  value = [
    aws_instance.one[0].public_ip,
    aws_instance.one[0].private_ip,
    aws_instance.one[0].id
  ]
}

Run the following command to view outputs after applying your configuration:

terraform output

Practical Example: Destroying Specific Resources

When managing resources, there may be times you need to destroy a specific resource while preserving others. Use the --target flag for this purpose:

terraform destroy --auto-approve --target=aws_instance.one

This command destroys only the specified resource.

Terraform’s versatility shines through features like Alias & Providers, State Files, Workspaces, and Outputs. By mastering these aspects, you can efficiently manage infrastructure across multiple environments and maintain clarity in resource configuration.