This repository demonstrates how to manage AWS EC2 instances using Terraform with support for multiple environments (e.g., dev
, stage
, and prod
) via Terraform workspaces.
.
|-- modules/
| |-- ec2_instance/
| |-- main.tf
|
|-- main.tf
|-- terraform.tfvars
|-- README.md
This module defines the EC2 instance resource. It includes variables for specifying the AMI ID and the instance type.
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"
: Specifies the AWS provider and the region.- Variables:
ami
: Specifies the Amazon Machine Image (AMI) ID.instance_type
: Specifies the EC2 instance type.
- Resource:
aws_instance
: Creates the EC2 instance using the specified AMI and instance type.
The root main.tf
integrates the ec2_instance
module and uses Terraform workspaces to dynamically select the instance type for different environments.
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"
: Specifies the AWS provider and the region.variable "ami"
: Specifies the AMI ID (defined interraform.tfvars
).variable "instance_type"
: Defines a map of instance types for different environments.dev
: Default instance type ist2.micro
.stage
: Default instance type ist2.small
.prod
: Default instance type ist2.nano
.
- Module Integration:
- Uses the
ec2_instance
module to provision EC2 instances. lookup
: Selects the instance type based on the active workspace.
- Uses the
The terraform.tfvars
file specifies values for variables.
ami = "ami-053b12d3152c0cc71"
ami
: The AMI ID to use for all instances.
- Terraform installed on your system.
- AWS CLI configured with appropriate permissions.
-
Clone the Repository:
git clone https://github.com/<your-username>/terraform-aws-ec2-workspaces.git cd terraform-aws-ec2-workspaces
-
Initialize Terraform:
terraform init
-
Create a Workspace:
-
To create and switch to a workspace:
terraform workspace new <workspace-name>
Replace
<workspace-name>
withdev
,stage
, orprod
. -
To list available workspaces:
terraform workspace list
-
-
Apply the Terraform Configuration:
terraform apply
Confirm the plan to provision resources.
-
Verify the Created Resources:
- Log in to your AWS Management Console.
- Navigate to EC2 Instances to see the newly created instance.
-
Clean Up Resources: To destroy the resources:
terraform destroy
- Environment-Specific Configurations: Use Terraform workspaces to manage configurations for
dev
,stage
, andprod
environments. - Modular Design: The EC2 instance logic is encapsulated in a reusable module.
- Dynamic Instance Types: Selects the instance type dynamically based on the active workspace.
Workspace | Instance Type |
---|---|
dev | t2.micro |
stage | t2.small |
prod | t2.nano |