Thanks to visit codestin.com
Credit goes to github.com

Skip to content

amaudy/demo-infra

Repository files navigation

Demo Infrastructure - HashiCorp Best Practices Structure

This Terraform configuration follows HashiCorp's recommended best practices for organizing infrastructure code. It creates ECS Fargate clusters for multiple environments using a proper directory structure with environment separation.

Architecture Overview

This project demonstrates a production-ready Terraform structure that includes:

  • Environment Separation: Separate directories for each environment (dev, staging, prod)
  • Module Organization: Reusable modules in a dedicated modules directory
  • Shared Configuration: Common configurations and backend setup
  • Proper State Management: Environment-specific state files with S3 backend

Directory Structure (HashiCorp Best Practices)

demo-infra-restructured/
├── README.md                           # This file
├── .gitignore                          # Git ignore rules
├── backend-setup/                      # Backend infrastructure setup
│   ├── main.tf                         # S3 bucket and DynamoDB table
│   ├── variables.tf                    # Backend setup variables
│   ├── outputs.tf                      # Backend resource outputs
│   ├── terraform.tfvars.example        # Example variables
│   └── README.md                       # Backend setup instructions
├── modules/                            # Reusable modules
│   └── ecs-cluster/                    # ECS cluster module
│       ├── main.tf                     # Module resources
│       ├── variables.tf                # Module variables
│       ├── outputs.tf                  # Module outputs
│       ├── versions.tf                 # Provider requirements
│       └── README.md                   # Module documentation
├── environments/                       # Environment-specific configurations
│   ├── dev/                           # Development environment
│   │   ├── main.tf                    # Dev environment resources
│   │   ├── variables.tf               # Dev environment variables
│   │   ├── outputs.tf                 # Dev environment outputs
│   │   ├── terraform.tfvars           # Dev variable values
│   │   ├── backend.tf                 # Dev backend configuration
│   │   └── versions.tf                # Dev provider requirements
│   ├── staging/                       # Staging environment
│   │   ├── main.tf                    # Staging environment resources
│   │   ├── variables.tf               # Staging environment variables
│   │   ├── outputs.tf                 # Staging environment outputs
│   │   ├── terraform.tfvars           # Staging variable values
│   │   ├── backend.tf                 # Staging backend configuration
│   │   └── versions.tf                # Staging provider requirements
│   └── prod/                          # Production environment
│       ├── main.tf                    # Prod environment resources
│       ├── variables.tf               # Prod environment variables
│       ├── outputs.tf                 # Prod environment outputs
│       ├── terraform.tfvars           # Prod variable values
│       ├── backend.tf                 # Prod backend configuration
│       └── versions.tf                # Prod provider requirements
├── scripts/                           # Automation scripts
│   ├── deploy.sh                      # Deployment automation
│   ├── setup-backend.sh               # Backend setup automation
│   └── cleanup.sh                     # Cleanup automation
└── docs/                              # Additional documentation
    ├── DEPLOYMENT.md                  # Deployment guide
    ├── ARCHITECTURE.md                # Architecture documentation
    └── TROUBLESHOOTING.md             # Troubleshooting guide

Key Benefits of This Structure

1. Environment Isolation

  • Each environment has its own directory and state file
  • Prevents accidental changes to production from development
  • Allows environment-specific configurations

2. Module Reusability

  • Common infrastructure patterns are abstracted into modules
  • Modules can be versioned and shared across projects
  • Reduces code duplication and improves maintainability

3. Clear Separation of Concerns

  • Backend setup is separate from application infrastructure
  • Scripts are organized in their own directory
  • Documentation is centralized and comprehensive

4. Scalability

  • Easy to add new environments
  • Simple to add new modules
  • Clear patterns for team collaboration

Quick Start

1. Set Up Backend Infrastructure

# Navigate to backend setup
cd backend-setup

# Copy and configure variables
cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars with your values

# Deploy backend infrastructure
terraform init
terraform plan
terraform apply

2. Deploy Environment

# Navigate to desired environment
cd environments/dev

# Initialize with backend
terraform init

# Plan and apply
terraform plan
terraform apply

3. Use Automation Scripts

# Deploy using automation script
./scripts/deploy.sh dev plan
./scripts/deploy.sh dev apply

# Deploy to staging
./scripts/deploy.sh staging apply

# Deploy to production
./scripts/deploy.sh prod apply

Environment Differences

Development

  • Purpose: Development and testing
  • Log Retention: 7 days (cost optimization)
  • Fargate Spot: Enabled (cost savings)
  • Monitoring: Basic CloudWatch logs
  • Backup: Disabled

Staging

  • Purpose: Pre-production testing
  • Log Retention: 30 days
  • Fargate Spot: Enabled (cost savings)
  • Monitoring: Enhanced monitoring
  • Backup: Enabled

Production

  • Purpose: Live production workloads
  • Log Retention: 365 days (compliance)
  • Fargate Spot: Disabled (reliability)
  • Monitoring: Full monitoring and alerting
  • Backup: Enabled with cross-region replication

Module Usage

The ECS cluster module can be used independently:

module "ecs_cluster" {
  source = "../../modules/ecs-cluster"
  
  cluster_name = "my-cluster"
  environment  = "dev"
  
  # VPC Configuration
  vpc_id     = "vpc-12345678"
  subnet_ids = ["subnet-12345678", "subnet-87654321"]
  
  # Logging Configuration
  log_retention_days = 7
  
  # Tags
  tags = {
    Environment = "dev"
    Project     = "demo-app"
  }
}

Best Practices Implemented

1. Standard Module Structure

  • Each module follows HashiCorp's standard structure
  • Proper variable descriptions and validation
  • Comprehensive outputs for integration

2. Environment Management

  • Directory-based environment separation
  • Environment-specific variable files
  • Isolated state management

3. Code Organization

  • Logical grouping of resources
  • Clear naming conventions
  • Consistent file structure

4. Documentation

  • README files at every level
  • Inline code documentation
  • Architecture diagrams and guides

5. Automation

  • Deployment scripts for consistency
  • Backend setup automation
  • Cleanup and maintenance scripts

Security Considerations

  • State File Security: S3 backend with encryption and versioning
  • Access Control: IAM roles with least privilege
  • Network Security: Private subnets and security groups
  • Secrets Management: Integration with AWS Secrets Manager

Cost Optimization

  • Environment-Specific Settings: Different configurations for cost vs. reliability
  • Fargate Spot: Enabled in non-production environments
  • Log Retention: Shorter retention in development environments
  • Resource Tagging: Comprehensive tagging for cost allocation

Monitoring and Observability

  • CloudWatch Logs: Centralized logging with configurable retention
  • CloudWatch Metrics: ECS cluster and service metrics
  • Alarms: Environment-specific alerting (production has more comprehensive monitoring)

Next Steps

  1. Add More Modules: Create modules for other infrastructure components
  2. CI/CD Integration: Integrate with your CI/CD pipeline
  3. Monitoring: Add comprehensive monitoring and alerting
  4. Security: Implement additional security controls
  5. Documentation: Expand documentation as the project grows

Support and Troubleshooting

  • Check the docs/TROUBLESHOOTING.md for common issues
  • Review CloudWatch logs for application issues
  • Use the deployment scripts for consistent operations
  • Follow the architecture documentation for understanding the system

Contributing

When adding new infrastructure:

  1. Follow the established directory structure
  2. Create modules for reusable components
  3. Add proper documentation
  4. Include examples and tests
  5. Update this README with any new patterns

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published