Skip to content

how to manage AWS EC2 instances using Terraform with support for multiple environments via Terraform workspaces.

Notifications You must be signed in to change notification settings

chintanboghara/Terraform-AWS-EC2-with-Workspaces

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Terraform AWS EC2 with Workspaces

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.

Repository Structure

.
├── 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.

Overview

This project uses Terraform workspaces to manage separate EC2 instances for different environments, each with its own instance type:

  • dev: t2.micro
  • stage: t2.small
  • prod: t2.nano

The AMI ID is shared across all environments and defined in terraform.tfvars. The AWS region is set to ap-south-1.

Prerequisites

  • 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).

How to Use This Repository

1. Clone the Repository

git clone https://github.com/<your-username>/terraform-aws-ec2-workspaces.git
cd terraform-aws-ec2-workspaces

2. Initialize Terraform

Initialize the Terraform working directory to download provider plugins:

terraform init

3. Manage Workspaces

Terraform 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, or prod):
terraform workspace new dev
  • Switch to an existing workspace:
terraform workspace select dev

The instance type is automatically set based on the active workspace.

4. Apply the Configuration

Provision the EC2 instance for the selected workspace:

terraform apply

Review the plan and type yes to confirm.

5. Verify Resources

  • Log in to the AWS Management Console.
  • Go to the EC2 dashboard and confirm the instance type matches the selected workspace:
    • dev: t2.micro
    • stage: t2.small
    • prod: t2.nano

6. Clean Up

Destroy resources to avoid unnecessary costs:

terraform destroy

Confirm with yes. Ensure the correct workspace is selected, as each has its own state.

Configuration Details

Module: modules/ec2_instance/main.tf

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.

Root: main.tf

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 in terraform.tfvars.
    • instance_type: A map linking workspaces to instance types.
  • Module: Calls the ec2_instance module, dynamically selecting the instance type based on the workspace.

Variables: terraform.tfvars

ami = "ami-053b12d3152c0cc71"
  • Specifies the AMI ID for the ap-south-1 region. Verify its validity for your use case.

Key Features

  • Workspaces: Manage dev, stage, and prod environments with separate states.
  • Modularity: Reusable EC2 instance logic in a module.
  • Dynamic Configuration: Instance types adjust automatically based on the workspace.

Example Workspaces

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.

Additional Notes

  • Region: Fixed to ap-south-1 in 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.

About

how to manage AWS EC2 instances using Terraform with support for multiple environments via Terraform workspaces.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages