|
| 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` |
0 commit comments