A well-structured Terraform template repository designed for managing your infrastructure..
This template includes GitHub Actions workflows for automated Terraform operations:
Triggers: All pushes and pull requests
- β Format checking - Ensures code is properly formatted
- β Terraform validation - Validates syntax and configuration
- β Security scanning - Runs tfsec for security best practices
- β PR comments - Provides feedback directly on pull requests
Triggers: Main branch pushes, PRs, manual dispatch
- π Multi-environment planning - Plans for dev, stage, test environments
- π‘οΈ Advanced security scanning - tfsec and Checkov integration
- π° Cost estimation - Infracost integration for cost analysis
- π Automated deployment - Apply changes to specified environments
- ποΈ Controlled destruction - Manual destroy capability with confirmations
For the full pipeline, configure these GitHub secrets:
AZURE_CLIENT_ID # Service Principal Client ID
AZURE_CLIENT_SECRET # Service Principal Secret
AZURE_SUBSCRIPTION_ID # Azure Subscription ID
AZURE_TENANT_ID # Azure Tenant ID
INFRACOST_API_KEY # Infracost API key (optional, for cost estimation)
The full pipeline supports manual execution with options for:
- Environment selection: dev, stage, test, prod
- Action selection: plan, apply, destroy
This template provides a standardized, scalable foundation for Terraform projects. While the structure is cloud-agnostic, all examples, documentation, and default configurations are optimized for Microsoft Azure.
terraform-template/
βββ README.md # This file
βββ makefile # Automation commands for Terraform operations
βββ LICENSE # Project license
β
βββ config/ # Environment-specific configurations
β βββ dev.tfvars # Development environment variables
β βββ stage.tfvars # Staging environment variables
β βββ test.tfvars # Test environment variables
β βββ prod.tfvars # Production environment variables
β
βββ backend.tf # Terraform backend configuration (state storage)
βββ providers.tf # Provider configurations and version constraints
βββ variables.tf # Input variable declarations
βββ outputs.tf # Output value definitions
βββ data.tf # Data source references (existing resources)
βββ main.tf # Miscellaneous resources without dedicated files
β
βββ compute.tf # Compute resources (VMs, containers, functions)
βββ networking.tf # Network resources (VNets, NSGs, load balancers)
βββ storage.tf # Storage resources (storage accounts, containers)
βββ database.tf # Database resources (SQL, CosmosDB, caches)
-
Clone the repository:
git clone https://github.com/PseudoCoding/terraform-template.git cd terraform-template
-
Authenticate with Azure:
az login
-
Configure your environment:
# Edit the appropriate config file vim config/dev.tfvars
-
Initialize and plan:
make init ENV=dev make plan ENV=dev
-
Apply changes:
make apply ENV=dev
This template includes a comprehensive Makefile for common Terraform operations:
Command | Description |
---|---|
make help |
Show all available commands |
make init ENV=<env> |
Initialize Terraform for specified environment |
make plan ENV=<env> |
Plan changes for specified environment |
make apply ENV=<env> |
Apply changes for specified environment |
make destroy ENV=<env> |
Destroy resources (with confirmation) |
make fmt |
Format all Terraform files |
make validate ENV=<env> |
Validate configuration |
make clean |
Clean temporary files |
Example:
make plan ENV=dev # Plan for development environment
make apply ENV=prod # Apply to production environment
The template supports multiple environments through the config/
directory:
dev.tfvars
- Development environmentstage.tfvars
- Staging environmenttest.tfvars
- Testing environmentprod.tfvars
- Production environment
Each file contains environment-specific variables that override defaults.
backend.tf
- Only backend configuration (state storage)providers.tf
- Only provider configurations and version constraintsvariables.tf
- Only variable declarationsoutputs.tf
- Only output value definitionsdata.tf
- Only data sources (references to existing resources)main.tf
- Resources that don't fit in other categoriescompute.tf
- VMs, containers, functions, AKS clustersnetworking.tf
- VNets, subnets, NSGs, load balancers, DNSstorage.tf
- Storage accounts, blob containers, file sharesdatabase.tf
- SQL databases, CosmosDB, Redis cache
- Single Responsibility - Each file has one clear purpose
- Environment Agnostic - Core files work across all environments
- Azure Optimized - Examples and defaults target Azure services
- Scalable Structure - Easy to extend as infrastructure grows
- Team Friendly - Clear organization reduces merge conflicts
This template is biased towards Azure with:
- Azure Resource Manager (ARM) provider examples
- Azure-specific resource naming conventions
- Azure best practices in documentation
- Azure CLI integration for authentication
- Azure Storage backend configuration examples
- Compute: Virtual Machines, Container Instances, App Services, AKS
- Networking: Virtual Networks, Subnets, NSGs, Load Balancers
- Storage: Storage Accounts, Blob Storage, File Shares
- Database: SQL Database, CosmosDB, Redis Cache
- Security: Key Vault, Managed Identity, RBAC
- Create a new
.tfvars
file in theconfig/
directory - Define environment-specific variables
- Use with makefile:
make plan ENV=<your-env>
- Create a new
.tf
file (e.g.,security.tf
) - Add comprehensive documentation header
- Update this README with the new file purpose
While optimized for Azure, this template can be adapted for AWS or GCP:
- Update provider configurations in
providers.tf
- Replace Azure examples with target cloud provider
- Update documentation and variable names accordingly
- Use descriptive resource names with environment prefixes
- Apply consistent tagging across all resources
- Use remote state for team collaboration (not local backend)
- Implement proper RBAC and security policies
- Regular state file backups and disaster recovery planning
- Code reviews for all infrastructure changes
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Follow the established file organization patterns
- Update documentation as needed
- Commit changes (
git commit -m 'Add amazing feature'
) - Push to branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For questions or issues:
- Check the Issues page
- Review the Terraform Azure Provider Documentation
- Consult Azure documentation
Note: This template prioritizes Azure cloud services. For other cloud providers, consider adapting the examples and configurations accordingly.