Terraform Directory Structure #
-
Organize Terraform configuration and workflows logically to separate environments, stages, and manage resource module versioning.
-
Example structure:
├── workflows │ └── template.yml ├── versions │ ├── modules.prod.yml │ ├── modules.stage.yml │ └── modules.dev.yml ├── environments │ ├── prod.tfvars │ ├── stage.tfvars │ └── dev.tfvars ├── modules │ ├── network │ │ ├── main.tf │ │ ├── outputs.tf │ │ ├── variables.tf │ ├── compute │ │ ├── main.tf │ │ ├── outputs.tf │ │ ├── variables.tf | ├── resource_namer │ │ ├── main.tf │ │ ├── outputs.tf │ │ ├── variables.tf │ │ ├── lookup.yaml ├── terraform │ ├── main.tf │ ├── variables.tf │ ├── outputs.tf │ ├── backend.tf │ └── provider.tf └── README.md └── pipeline.development.yaml └── pipeline.staging.yaml └── pipeline.production.yaml
Directory Breakdown #
-
workflows/:
- Contains reusable pipeline configuration files that can be run by environments for CI/CD workflows, typically in
.ymlformat. - The
workflowsfolder should be renamed according to the requirements or best practices of the CI/CD tool being used. For example, it can be named.github/workflowsfor GitHub Actions or.azuredevopsfor Azure DevOps. - This folder is only needed if the workflow yml templates in this project are local. If using remote workflows, like shown in the example workflow, the
workflowfolder may not be necessary.
- Contains reusable pipeline configuration files that can be run by environments for CI/CD workflows, typically in
-
./pipeline.yaml trigger files:
- Contains trigger yaml files representing actual deployment pipelines. These can be structured to run a single environment, or a set or environments.
- These files sit at the root of the directory and can either call a local workflows folder, or a remote set of workflows, in the a remote workflow can be setup example workflow.
- Files like
pipeline.production.yaml,pipeline.staging.yaml, andpipeline.development.yaml.ymlrepresent pipeline entrypoints for deployment for different environments. When configuring Azure DevOps pipelines, point it to one of these yaml files.
-
versions/:
- Contains versioning files to manage resource module versions across different environments.
- This directory is designed to be used by the pipeline token replacement task during runtime, allowing different versions of modules to be provided at runtime for different environments.
- Example files
production.yml,staging.yml, anddevelopment.ymlspecify module versions or specific configurations that may differ between environments.
-
environments/:
- Contains environment-specific variables files (
.tfvars) which are used to define different configurations for each environment (e.g., production, staging, development).
- Contains environment-specific variables files (
-
modules/:
- Holds reusable Terraform modules, organized by functionality, such as
networkandcompute. - Each module should include its own
main.tf,variables.tf, andoutputs.tfto define the resources, variables, and outputs for that specific module. - This folder is only needed if there are modules in the project that are local. If using remote modules, the
modulesfolder may not be necessary.
- Holds reusable Terraform modules, organized by functionality, such as
-
terraform/:
- Contains the main Terraform configuration files.
main.tfis the primary entry point where you define the infrastructure.variables.tfdefines input variables.outputs.tfdefines output values.backend.tfis for backend configuration, such as storing the Terraform state remotely.provider.tfcontains provider configurations.
-
README.md:
- A documentation file to describe the project, directory structure, usage instructions, and any relevant details.
A documentation file to describe the project, directory structure, usage instructions, and any relevant details.