Skip to content

Commit dc13773

Browse files
author
CloudNinjaDev
committed
Refactor Terraform configuration for MongoDB deployment: modularize EC2 setup, add environment-specific variables, and create README for multi-environment usage.
1 parent b01bb5b commit dc13773

11 files changed

Lines changed: 263 additions & 8 deletions

File tree

terraform/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Multi-Environment Terraform Setup
2+
3+
This repository uses a modular Terraform structure to deploy MongoDB EC2 instances across multiple environments.
4+
5+
## Structure
6+
7+
```
8+
terraform/
9+
├── modules/
10+
│ └── ec2/ # Reusable EC2 module
11+
├── production/ # Production environment
12+
│ ├── main.tf
13+
│ ├── variables.tf
14+
│ ├── terraform.tf
15+
│ └── production.tfvars
16+
└── staging/ # Staging environment
17+
├── main.tf
18+
├── variables.tf
19+
├── terraform.tf
20+
└── staging.tfvars
21+
```
22+
23+
## How It Works
24+
25+
**Single Module, Multiple Environments:** The same EC2 module in `modules/ec2/` is reused by both staging and production environments with different `.tfvars` files.
26+
27+
## Environment Differences
28+
29+
| Setting | Staging | Production |
30+
|---------|---------|------------|
31+
| Instance Type | t3.small | t3.large |
32+
| Volume Size | 10 GB | 20 GB |
33+
| Tags | staging | production |
34+
35+
## Usage
36+
37+
### Deploy to Staging
38+
39+
```bash
40+
cd terraform/staging
41+
terraform init
42+
terraform plan -var-file=staging.tfvars
43+
terraform apply -var-file=staging.tfvars
44+
```
45+
46+
### Deploy to Production
47+
48+
```bash
49+
cd terraform/production
50+
terraform init
51+
terraform plan -var-file=production.tfvars
52+
terraform apply -var-file=production.tfvars
53+
```
54+
55+
### Destroy Resources
56+
57+
```bash
58+
# Staging
59+
cd terraform/staging
60+
terraform destroy -var-file=staging.tfvars
61+
62+
# Production
63+
cd terraform/production
64+
terraform destroy -var-file=production.tfvars
65+
```
66+
67+
## Outputs
68+
69+
Both environments output:
70+
- `instance_id` - EC2 instance ID
71+
- `instance_public_ip` - Public IP address
72+
- `instance_private_ip` - Private IP address
73+
74+
## Customization
75+
76+
To modify settings for each environment, edit the respective `.tfvars` file:
77+
- Staging: `terraform/staging/staging.tfvars`
78+
- Production: `terraform/production/production.tfvars`
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
resource "aws_security_group" "mongodb" {
2-
name = "mongodb-sg"
3-
description = "Security group for MongoDB EC2 instance"
2+
name = "mongodb-sg-${var.environment}"
3+
description = "Security group for MongoDB EC2 instance (${var.environment})"
44
vpc_id = var.vpc_id
55

66
ingress {
@@ -28,23 +28,25 @@ resource "aws_security_group" "mongodb" {
2828
}
2929

3030
tags = {
31-
Name = "MongoDB Security Group"
31+
Name = "MongoDB-SG-${var.environment}"
32+
Environment = var.environment
3233
}
3334
}
3435

3536
resource "aws_instance" "mongodb" {
36-
ami = "ami-001fbc42d7df13f78"
37+
ami = var.ami_id
3738
instance_type = var.instance_type
3839
key_name = var.key_name
39-
40+
4041
vpc_security_group_ids = [aws_security_group.mongodb.id]
4142

4243
root_block_device {
43-
volume_size = 8
44+
volume_size = var.root_volume_size
4445
volume_type = "gp3"
4546
}
4647

4748
tags = {
48-
Name = "MongoDB-Instance"
49+
Name = "MongoDB-${var.environment}"
50+
Environment = var.environment
4951
}
5052
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
terraform {
22
required_version = ">= 1.0"
3-
3+
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
variable "environment" {
2+
description = "Environment name (staging, production)"
3+
type = string
4+
}
5+
16
variable "aws_region" {
27
description = "AWS region"
38
type = string
@@ -21,3 +26,15 @@ variable "vpc_id" {
2126
type = string
2227
default = "vpc-0c4669393c94b8f86"
2328
}
29+
30+
variable "ami_id" {
31+
description = "AMI ID for the EC2 instance"
32+
type = string
33+
default = "ami-001fbc42d7df13f78"
34+
}
35+
36+
variable "root_volume_size" {
37+
description = "Root volume size in GB"
38+
type = number
39+
default = 8
40+
}

terraform/production/main.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module "ec2_mongodb" {
2+
source = "../modules/ec2"
3+
4+
environment = var.environment
5+
aws_region = var.aws_region
6+
instance_type = var.instance_type
7+
key_name = var.key_name
8+
vpc_id = var.vpc_id
9+
ami_id = var.ami_id
10+
root_volume_size = var.root_volume_size
11+
}
12+
13+
output "instance_id" {
14+
description = "ID of the EC2 instance"
15+
value = module.ec2_mongodb.instance_id
16+
}
17+
18+
output "instance_public_ip" {
19+
description = "Public IP address of the EC2 instance"
20+
value = module.ec2_mongodb.instance_public_ip
21+
}
22+
23+
output "instance_private_ip" {
24+
description = "Private IP address of the EC2 instance"
25+
value = module.ec2_mongodb.instance_private_ip
26+
}

terraform/production/terraform.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "~> 5.0"
8+
}
9+
}
10+
}
11+
12+
provider "aws" {
13+
region = var.aws_region
14+
}

terraform/production/variables.tf

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
variable "environment" {
2+
description = "Environment name"
3+
type = string
4+
default = "production"
5+
}
6+
7+
variable "aws_region" {
8+
description = "AWS region"
9+
type = string
10+
default = "us-west-2"
11+
}
12+
13+
variable "instance_type" {
14+
description = "EC2 instance type"
15+
type = string
16+
default = "t3.large"
17+
}
18+
19+
variable "key_name" {
20+
description = "Key pair name for SSH access"
21+
type = string
22+
}
23+
24+
variable "vpc_id" {
25+
description = "VPC ID for the instance"
26+
type = string
27+
}
28+
29+
variable "ami_id" {
30+
description = "AMI ID for the EC2 instance"
31+
type = string
32+
default = "ami-001fbc42d7df13f78"
33+
}
34+
35+
variable "root_volume_size" {
36+
description = "Root volume size in GB"
37+
type = number
38+
default = 20
39+
}

terraform/staging/main.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module "ec2_mongodb" {
2+
source = "../modules/ec2"
3+
4+
environment = var.environment
5+
aws_region = var.aws_region
6+
instance_type = var.instance_type
7+
key_name = var.key_name
8+
vpc_id = var.vpc_id
9+
ami_id = var.ami_id
10+
root_volume_size = var.root_volume_size
11+
}
12+
13+
output "instance_id" {
14+
description = "ID of the EC2 instance"
15+
value = module.ec2_mongodb.instance_id
16+
}
17+
18+
output "instance_public_ip" {
19+
description = "Public IP address of the EC2 instance"
20+
value = module.ec2_mongodb.instance_public_ip
21+
}
22+
23+
output "instance_private_ip" {
24+
description = "Private IP address of the EC2 instance"
25+
value = module.ec2_mongodb.instance_private_ip
26+
}

terraform/staging/terraform.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "~> 5.0"
8+
}
9+
}
10+
}
11+
12+
provider "aws" {
13+
region = var.aws_region
14+
}

0 commit comments

Comments
 (0)