1---2name: terraform3description: Terraform Conventions4---5# Terraform Conventions67## General Instructions89- Use Terraform to provision and manage infrastructure.10- Use version control for your Terraform configurations.1112## Security1314- Always use the latest stable version of Terraform and its providers.15 - Regularly update your Terraform configurations to incorporate security patches and improvements.16- Store sensitive information in a secure manner, such as using AWS Secrets Manager or SSM Parameter Store.17 - Regularly rotate credentials and secrets.18 - Automate the rotation of secrets, where possible.19- Use AWS environment variables to reference values stored in AWS Secrets Manager or SSM Parameter Store.20 - This keeps sensitive values out of your Terraform state files.21- Never commit sensitive information such as AWS credentials, API keys, passwords, certificates, or Terraform state to version control.22 - Use `.gitignore` to exclude files containing sensitive information from version control.23- Always mark sensitive variables as `sensitive = true` in your Terraform configurations.24 - This prevents sensitive values from being displayed in the Terraform plan or apply output.25- Use IAM roles and policies to control access to resources.26 - Follow the principle of least privilege when assigning permissions.27- Use security groups and network ACLs to control network access to resources.28- Deploy resources in private subnets whenever possible.29 - Use public subnets only for resources that require direct internet access, such as load balancers or NAT gateways.30- Use encryption for sensitive data at rest and in transit.31 - Enable encryption for EBS volumes, S3 buckets, and RDS instances.32 - Use TLS for communication between services.33- Regularly review and audit your Terraform configurations for security vulnerabilities.34 - Use tools like `trivy`, `tfsec`, or `checkov` to scan your Terraform configurations for security issues.3536## Modularity3738- Use separate projects for each major component of the infrastructure; this:39 - Reduces complexity40 - Makes it easier to manage and maintain configurations41 - Speeds up `plan` and `apply` operations42 - Allows for independent development and deployment of components43 - Reduces the risk of accidental changes to unrelated resources44- Use modules to avoid duplication of configurations.45 - Use modules to encapsulate related resources and configurations.46 - Use modules to simplify complex configurations and improve readability.47 - Avoid circular dependencies between modules.48 - Avoid unnecessary layers of abstraction; use modules only when they add value.49 - Avoid using modules for single resources; only use them for groups of related resources.50 - Avoid excessive nesting of modules; keep the module hierarchy shallow.51- Use `output` blocks to expose important information about your infrastructure.52 - Use outputs to provide information that is useful for other modules or for users of the configuration.53 - Avoid exposing sensitive information in outputs; mark outputs as `sensitive = true` if they contain sensitive data.5455## Maintainability5657- Prioritize readability, clarity, and maintainability.58- Use comments to explain complex configurations and why certain design decisions were made.59- Write concise, efficient, and idiomatic configs that are easy to understand.60- Avoid using hard-coded values; use variables for configuration instead.61 - Set default values for variables, where appropriate.62- Use data sources to retrieve information about existing resources instead of requiring manual configuration.63 - This reduces the risk of errors, ensures that configurations are always up-to-date, and allows configurations to adapt to different environments.64 - Avoid using data sources for resources that are created within the same configuration; use outputs instead.65 - Avoid, or remove, unnecessary data sources; they slow down `plan` and `apply` operations.66- Use `locals` for values that are used multiple times to ensure consistency.6768## Style and Formatting6970- Follow Terraform best practices for resource naming and organization.71 - Use descriptive names for resources, variables, and outputs.72 - Use consistent naming conventions across all configurations.73- Follow the **Terraform Style Guide** for formatting.74 - Use consistent indentation (2 spaces for each level).75- Group related resources together in the same file.76 - Use a consistent naming convention for resource groups (e.g., `providers.tf`, `variables.tf`, `network.tf`, `ecs.tf`, `mariadb.tf`).77- Place `depends_on` blocks at the very beginning of resource definitions to make dependency relationships clear.78 - Use `depends_on` only when necessary to avoid circular dependencies.79- Place `for_each` and `count` blocks at the beginning of resource definitions to clarify the resource's instantiation logic.80 - Use `for_each` for collections and `count` for numeric iterations.81 - Place them after `depends_on` blocks, if they are present.82- Place `lifecycle` blocks at the end of resource definitions.83- Alphabetize providers, variables, data sources, resources, and outputs within each file for easier navigation.84- Group related attributes together within blocks.85 - Place required attributes before optional ones, and comment each section accordingly.86 - Separate attribute sections with blank lines to improve readability.87 - Alphabetize attributes within each section for easier navigation.88- Use blank lines to separate logical sections of your configurations.89- Use `terraform fmt` to format your configurations automatically.90- Use `terraform validate` to check for syntax errors and ensure configurations are valid.91- Use `tflint` to check for style violations and ensure configurations follow best practices.92 - Run `tflint` regularly to catch style issues early in the development process.9394## Documentation9596- Always include `description` and `type` attributes for variables and outputs.97 - Use clear and concise descriptions to explain the purpose of each variable and output.98 - Use appropriate types for variables (e.g., `string`, `number`, `bool`, `list`, `map`).99- Document your Terraform configurations using comments, where appropriate.100 - Use comments to explain the purpose of resources and variables.101 - Use comments to explain complex configurations or decisions.102 - Avoid redundant comments; comments should add value and clarity.103- Include a `README.md` file in each project to provide an overview of the project and its structure.104 - Include instructions for setting up and using the configurations.105- Use `terraform-docs` to generate documentation for your configurations automatically.106107## Testing108109- Write tests to validate the functionality of your Terraform configurations.110 - Use the `.tftest.hcl` extension for test files.111 - Write tests to cover both positive and negative scenarios.112 - Ensure tests are idempotent and can be run multiple times without side effects.