|
| 1 | +# Contributing to terraform-aws-backup |
| 2 | + |
| 3 | +Thank you for your interest in contributing to the terraform-aws-backup module! This document outlines our coding standards, development workflow, and review process. |
| 4 | + |
| 5 | +## 🚀 Quick Start |
| 6 | + |
| 7 | +1. Fork the repository |
| 8 | +2. Create a feature branch: `git checkout -b feature/your-feature-name` |
| 9 | +3. Make your changes following our coding standards |
| 10 | +4. Run pre-commit hooks: `pre-commit run --all-files` |
| 11 | +5. Test your changes with examples |
| 12 | +6. Submit a pull request |
| 13 | + |
| 14 | +## 📋 Code Quality Standards |
| 15 | + |
| 16 | +### Terraform Code Style |
| 17 | + |
| 18 | +#### Variable Definitions |
| 19 | +- **Descriptive names**: Use clear, descriptive variable names |
| 20 | +- **Consistent types**: Always specify variable types explicitly |
| 21 | +- **Validation rules**: Add validation for all input variables where applicable |
| 22 | +- **Documentation**: Include helpful descriptions with examples |
| 23 | + |
| 24 | +```hcl |
| 25 | +# ✅ Good |
| 26 | +variable "vault_kms_key_arn" { |
| 27 | + description = "The server-side encryption key that is used to protect your backups" |
| 28 | + type = string |
| 29 | + default = null |
| 30 | +
|
| 31 | + validation { |
| 32 | + condition = var.vault_kms_key_arn == null ? true : ( |
| 33 | + can(regex("^arn:aws:kms:", var.vault_kms_key_arn)) && |
| 34 | + !can(regex("alias/aws/", var.vault_kms_key_arn)) |
| 35 | + ) |
| 36 | + error_message = "The vault_kms_key_arn must be a valid customer-managed KMS key ARN." |
| 37 | + } |
| 38 | +} |
| 39 | +
|
| 40 | +# ❌ Bad |
| 41 | +variable "key" { |
| 42 | + type = string |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +#### Resource Organization |
| 47 | +- **Logical grouping**: Group related resources together |
| 48 | +- **Clear naming**: Use descriptive resource names |
| 49 | +- **Conditional creation**: Use locals for complex conditions |
| 50 | +- **Documentation**: Comment complex logic |
| 51 | + |
| 52 | +```hcl |
| 53 | +# ✅ Good |
| 54 | +locals { |
| 55 | + should_create_vault = var.enabled && var.vault_name != null |
| 56 | + should_create_lock = local.should_create_vault && var.locked |
| 57 | +} |
| 58 | +
|
| 59 | +resource "aws_backup_vault" "ab_vault" { |
| 60 | + count = local.should_create_vault ? 1 : 0 |
| 61 | + # ... |
| 62 | +} |
| 63 | +
|
| 64 | +# ❌ Bad |
| 65 | +resource "aws_backup_vault" "ab_vault" { |
| 66 | + count = var.enabled && var.vault_name != null ? 1 : 0 |
| 67 | + # ... |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +#### Error Messages |
| 72 | +- **Contextual information**: Include current values and guidance |
| 73 | +- **Clear language**: Use simple, direct language |
| 74 | +- **Helpful guidance**: Explain what the user should do |
| 75 | + |
| 76 | +```hcl |
| 77 | +# ✅ Good |
| 78 | +error_message = "changeable_for_days must be between 3 and 365 days. Current value: ${var.changeable_for_days}. This parameter controls the vault lock compliance period." |
| 79 | +
|
| 80 | +# ❌ Bad |
| 81 | +error_message = "Invalid value." |
| 82 | +``` |
| 83 | + |
| 84 | +### Constants and Magic Numbers |
| 85 | +- **No magic numbers**: Replace hardcoded values with named constants |
| 86 | +- **Configurable defaults**: Make defaults configurable via variables |
| 87 | +- **Clear naming**: Use descriptive names for constants |
| 88 | + |
| 89 | +```hcl |
| 90 | +# ✅ Good |
| 91 | +variable "default_lifecycle_delete_after_days" { |
| 92 | + description = "Default number of days after creation that a recovery point is deleted" |
| 93 | + type = number |
| 94 | + default = 90 |
| 95 | +} |
| 96 | +
|
| 97 | +delete_after = try(lifecycle.value.delete_after, var.default_lifecycle_delete_after_days) |
| 98 | +
|
| 99 | +# ❌ Bad |
| 100 | +delete_after = try(lifecycle.value.delete_after, 90) |
| 101 | +``` |
| 102 | + |
| 103 | +## 🔒 Security Standards |
| 104 | + |
| 105 | +### Input Validation |
| 106 | +- **Validate all inputs**: Use validation blocks for all variables |
| 107 | +- **Prevent misuse**: Block common misconfigurations |
| 108 | +- **Security-first defaults**: Choose secure defaults |
| 109 | + |
| 110 | +### Sensitive Data |
| 111 | +- **No hardcoded secrets**: Never commit secrets or keys |
| 112 | +- **Secure defaults**: Use customer-managed keys over AWS-managed |
| 113 | +- **Minimal permissions**: Follow principle of least privilege |
| 114 | + |
| 115 | +## 🧪 Testing Requirements |
| 116 | + |
| 117 | +### Before Submitting |
| 118 | +1. **Linting**: All linting rules must pass |
| 119 | + ```bash |
| 120 | + terraform fmt -check -recursive |
| 121 | + tflint --config=.tflint.hcl |
| 122 | + ``` |
| 123 | + |
| 124 | +2. **Security scanning**: Checkov security checks must pass |
| 125 | + ```bash |
| 126 | + checkov -d . --framework terraform |
| 127 | + ``` |
| 128 | + |
| 129 | +3. **Examples**: Test all relevant examples |
| 130 | + ```bash |
| 131 | + cd examples/simple_plan |
| 132 | + terraform init |
| 133 | + terraform plan |
| 134 | + ``` |
| 135 | + |
| 136 | +4. **Backwards compatibility**: Verify no breaking changes |
| 137 | + ```bash |
| 138 | + # Before changes |
| 139 | + terraform plan -out=before.plan |
| 140 | + # After changes |
| 141 | + terraform plan -out=after.plan |
| 142 | + # Compare plans should show identical resources |
| 143 | + ``` |
| 144 | + |
| 145 | +## 📝 Code Review Checklist |
| 146 | + |
| 147 | +### For Reviewers |
| 148 | + |
| 149 | +#### ✅ Code Quality |
| 150 | +- [ ] Code follows Terraform best practices |
| 151 | +- [ ] Variables have proper validation and documentation |
| 152 | +- [ ] No magic numbers or hardcoded values |
| 153 | +- [ ] Error messages are helpful and contextual |
| 154 | +- [ ] Complex logic is simplified and well-commented |
| 155 | + |
| 156 | +#### ✅ Security |
| 157 | +- [ ] No secrets or sensitive data in code |
| 158 | +- [ ] Input validation prevents common misconfigurations |
| 159 | +- [ ] Security scanning (Checkov) passes |
| 160 | +- [ ] Uses secure defaults (customer-managed keys, etc.) |
| 161 | + |
| 162 | +#### ✅ Compatibility |
| 163 | +- [ ] Changes are backwards compatible |
| 164 | +- [ ] All existing examples still work |
| 165 | +- [ ] terraform plan produces identical results for existing configurations |
| 166 | +- [ ] No breaking changes to variable or output interfaces |
| 167 | + |
| 168 | +#### ✅ Documentation |
| 169 | +- [ ] README updated if needed |
| 170 | +- [ ] Variable descriptions are clear and helpful |
| 171 | +- [ ] Examples demonstrate new features |
| 172 | +- [ ] CHANGELOG updated for user-facing changes |
| 173 | + |
| 174 | +#### ✅ Testing |
| 175 | +- [ ] All linting checks pass |
| 176 | +- [ ] Security scans pass |
| 177 | +- [ ] Examples work correctly |
| 178 | +- [ ] terraform-docs generates correct documentation |
| 179 | + |
| 180 | +### For Contributors |
| 181 | + |
| 182 | +#### Before Submitting PR |
| 183 | +- [ ] Followed coding standards outlined above |
| 184 | +- [ ] Added/updated tests for new functionality |
| 185 | +- [ ] Updated documentation as needed |
| 186 | +- [ ] Ran pre-commit hooks successfully |
| 187 | +- [ ] Tested examples work correctly |
| 188 | +- [ ] Verified backwards compatibility |
| 189 | + |
| 190 | +#### PR Description Should Include |
| 191 | +- [ ] Clear description of changes |
| 192 | +- [ ] Reasoning for the changes |
| 193 | +- [ ] Any breaking changes (should be rare) |
| 194 | +- [ ] Testing performed |
| 195 | +- [ ] Screenshots/examples if applicable |
| 196 | + |
| 197 | +## 🔧 Development Environment Setup |
| 198 | + |
| 199 | +### Prerequisites |
| 200 | +- Terraform >= 1.0 |
| 201 | +- Pre-commit hooks |
| 202 | +- tflint with AWS ruleset |
| 203 | +- Checkov |
| 204 | + |
| 205 | +### Setup |
| 206 | +```bash |
| 207 | +# Install pre-commit |
| 208 | +pip install pre-commit |
| 209 | + |
| 210 | +# Install hooks |
| 211 | +pre-commit install |
| 212 | + |
| 213 | +# Install tflint |
| 214 | +curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash |
| 215 | + |
| 216 | +# Install tflint AWS ruleset |
| 217 | +tflint --init |
| 218 | + |
| 219 | +# Install checkov |
| 220 | +pip install checkov |
| 221 | +``` |
| 222 | + |
| 223 | +### Pre-commit Configuration |
| 224 | +Our pre-commit configuration includes: |
| 225 | +- Terraform formatting (`terraform fmt`) |
| 226 | +- Terraform validation (`terraform validate`) |
| 227 | +- Terraform documentation (`terraform-docs`) |
| 228 | +- Terraform linting (`tflint`) |
| 229 | +- Security scanning (`checkov`) |
| 230 | +- Secrets detection (`detect-secrets`) |
| 231 | +- Spell checking for documentation |
| 232 | +- General file quality checks |
| 233 | + |
| 234 | +## 🎯 Contribution Guidelines |
| 235 | + |
| 236 | +### Types of Contributions |
| 237 | +- **Bug fixes**: Always welcome |
| 238 | +- **Feature enhancements**: Discuss in issues first |
| 239 | +- **Documentation improvements**: Very helpful |
| 240 | +- **Example additions**: Great for community |
| 241 | + |
| 242 | +### Breaking Changes |
| 243 | +- **Avoid when possible**: Strive for backwards compatibility |
| 244 | +- **Major version only**: Breaking changes only in major releases |
| 245 | +- **Clear migration path**: Provide migration guide |
| 246 | +- **Advance notice**: Discuss in issues before implementing |
| 247 | + |
| 248 | +### Code Organization |
| 249 | +- **Maintain standard structure**: Keep standard Terraform file layout |
| 250 | +- **Logical grouping**: Group related functionality together |
| 251 | +- **Clear separation**: Separate concerns appropriately |
| 252 | +- **Consistent patterns**: Follow existing code patterns |
| 253 | + |
| 254 | +## 🏷️ Versioning and Releases |
| 255 | + |
| 256 | +We follow [Semantic Versioning](https://semver.org/): |
| 257 | +- **MAJOR**: Breaking changes |
| 258 | +- **MINOR**: New features (backwards compatible) |
| 259 | +- **PATCH**: Bug fixes |
| 260 | + |
| 261 | +## 🤝 Community |
| 262 | + |
| 263 | +- **Be respectful**: Follow our code of conduct |
| 264 | +- **Be collaborative**: Help others learn and contribute |
| 265 | +- **Be constructive**: Provide helpful feedback |
| 266 | +- **Be patient**: Reviews take time for quality |
| 267 | + |
| 268 | +## 📚 Additional Resources |
| 269 | + |
| 270 | +- [Terraform Best Practices](https://www.terraform-best-practices.com/) |
| 271 | +- [AWS Backup Documentation](https://docs.aws.amazon.com/backup/) |
| 272 | +- [Module Examples](./examples/) |
| 273 | +- [Issue Templates](./.github/ISSUE_TEMPLATE/) |
| 274 | + |
| 275 | +--- |
| 276 | + |
| 277 | +Thank you for contributing to terraform-aws-backup! Your contributions help make AWS backup management easier for everyone. |
0 commit comments