Module Development
Guidelines for developing Terraform modules.
Getting Started
Use our template repositories as a starting point:
Module Structure
Required Files
main.tf: Main resource definitionsvariables.tf: Input variablesoutputs.tf: Output valuesversions.tf: Provider requirementsREADME.md: DocumentationCHANGELOG.md: Version history.gitignore: Git ignore rules.checkov.yml: Checkov configuration
Optional Files
examples/: Usage examplestests/: Test configurationsLICENSE: License file
Development Workflow
- Create feature branch:
git checkout -b feature/new-module - Write code: Implement module functionality
- Add tests: Create test configurations
- Update documentation: Keep README and CHANGELOG updated
- Run checks: Format, validate, lint, scan
- Create PR: Submit pull request for review
- Merge: After approval, merge to
devbranch
Code Standards
Formatting
terraform fmt -recursive
Validation
terraform init
terraform validate
Linting
tflint --init
tflint
Security Scanning
checkov -d . --framework terraform --framework cis_azure
Testing
Plan Testing
terraform init
terraform plan
Integration Testing
Consider using Terratest for comprehensive testing:
package test
import (
"testing"
"github.com/gruntwork-io/terratest/modules/terraform"
)
func TestModule(t *testing.T) {
terraformOptions := &terraform.Options{
TerraformDir: "../examples/basic",
}
defer terraform.Destroy(t, terraformOptions)
terraform.InitAndApply(t, terraformOptions)
}
Documentation
README.md
Include:
- Module description
- Usage examples
- Input/output documentation
- Requirements
- Examples
CHANGELOG.md
Document all changes:
## [1.0.0] - 2024-01-01
### Added
- Initial release
### Changed
- N/A
### Fixed
- N/A
Versioning
Follow semantic versioning:
- MAJOR: Breaking changes
- MINOR: New features (backward compatible)
- PATCH: Bug fixes (backward compatible)