This repository demonstrates how to use Terraform to manage AWS EC2 instances across multiple environments (dev, stage, and prod) with Terraform workspaces. Workspaces enable environment-specific configurations, such as instance types, while keeping the infrastructure code reusable and organized.
.
├── modules/
│ └── ec2_instance/
│ └── main.tf
├── main.tf
├── terraform.tfvars
└── README.md
modules/ec2_instance/main.tf: Defines the reusable EC2 instance module.main.tf: Configures the root module and integrates the EC2 module with workspace-specific settings.terraform.tfvars: Specifies the AMI ID for the EC2 instances.
This project uses Terraform workspaces to manage separate EC2 instances for different environments, each with its own instance type:
dev:t2.microstage:t2.smallprod:t2.nano
The AMI ID is shared across all environments and defined in terraform.tfvars. The AWS region is set to ap-south-1.
- Terraform installed on your system.
- AWS CLI configured with permissions to create EC2 instances.
- AWS credentials set up (e.g., via environment variables, shared credentials file, or IAM roles).
git clone https://github.com/<your-username>/terraform-aws-ec2-workspaces.git
cd terraform-aws-ec2-workspacesInitialize the Terraform working directory to download provider plugins:
terraform initTerraform workspaces allow you to manage separate instances of your infrastructure. Use them to switch between environments.
- List available workspaces:
terraform workspace list- Create a new workspace (e.g.,
dev,stage, orprod):
terraform workspace new dev- Switch to an existing workspace:
terraform workspace select devThe instance type is automatically set based on the active workspace.
Provision the EC2 instance for the selected workspace:
terraform applyReview the plan and type yes to confirm.
- Log in to the AWS Management Console.
- Go to the EC2 dashboard and confirm the instance type matches the selected workspace:
dev:t2.microstage:t2.smallprod:t2.nano
Destroy resources to avoid unnecessary costs:
terraform destroyConfirm with yes. Ensure the correct workspace is selected, as each has its own state.
provider "aws" {
region = "ap-south-1"
}
variable "ami" {
description = "This is AMI for the instance"
}
variable "instance_type" {
description = "This is the instance type, for example: t2.micro"
}
resource "aws_instance" "example" {
ami = var.ami
instance_type = var.instance_type
}- Provider: AWS, fixed to
ap-south-1. - Variables:
ami: The AMI ID for the instance.instance_type: The EC2 instance type.
- Resource: Creates an EC2 instance with the specified AMI and instance type.
provider "aws" {
region = "ap-south-1"
}
variable "ami" {
description = "value"
}
variable "instance_type" {
description = "value"
type = map(string)
default = {
"dev" = "t2.micro"
"stage" = "t2.small"
"prod" = "t2.nano"
}
}
module "ec2_instance" {
source = "./modules/ec2_instance"
ami = var.ami
instance_type = lookup(var.instance_type, terraform.workspace, "t2.micro")
}- Provider: AWS, set to
ap-south-1. - Variables:
ami: Defined interraform.tfvars.instance_type: A map linking workspaces to instance types.
- Module: Calls the
ec2_instancemodule, dynamically selecting the instance type based on the workspace.
ami = "ami-053b12d3152c0cc71"- Specifies the AMI ID for the
ap-south-1region. Verify its validity for your use case.
- Workspaces: Manage
dev,stage, andprodenvironments with separate states. - Modularity: Reusable EC2 instance logic in a module.
- Dynamic Configuration: Instance types adjust automatically based on the workspace.
| Workspace | Instance Type |
|---|---|
dev |
t2.micro |
stage |
t2.small |
prod |
t2.nano |
Note: t2.nano for prod is for demonstration. Production typically requires larger instances.
- Region: Fixed to
ap-south-1in both the root and module configurations. - AMI: Hardcoded in
terraform.tfvars. For flexibility, consider using a data source to fetch the latest AMI. - State Management: Each workspace has its own state file. Be mindful when switching to manage the correct resources.