Terraform Directory Structure
1. Overview
Organizing your Terraform project into a structured directory layout helps manage your infrastructure code more effectively. Below is a common structure with explanations of each file's purpose.
2. File Structure
terraform-project/
βββ main.tf
βββ variables.tf
βββ outputs.tf
βββ terraform.tfvars
βββ provider.tf
βββ modules/
β βββ module1/
β β βββ main.tf
β β βββ variables.tf
β β βββ outputs.tf
β βββ module2/
β βββ main.tf
β βββ variables.tf
β βββ outputs.tf
βββ backend.tf
βββ versions.tf
3. Explanation of Each File
3.1 main.tf
-
Purpose: Contains the main configuration of your Terraform project. This is where you define resources such as EC2 instances, S3 buckets, etc.
-
Example:
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" tags = { Name = "example-instance" } }
3.2 variables.tf
-
Purpose: Defines variables used in your Terraform configurations. This file specifies the input variables and their types, default values, and descriptions.
-
Example:
variable "instance_type" { description = "Type of EC2 instance" type = string default = "t2.micro" }
3.3 outputs.tf
-
Purpose: Defines output values that are returned after Terraform applies your configuration. Useful for passing information between modules or displaying important data.
-
Example:
output "instance_id" { value = aws_instance.example.id }
3.4 terraform.tfvars
-
Purpose: Contains the actual values for the variables defined in
variables.tf
. This file is used to provide variable values duringterraform apply
orterraform plan
. -
Example:
instance_type = "t3.micro"
3.5 provider.tf
-
Purpose: Configures the providers that Terraform uses to interact with APIs. Providers define the connection settings for various cloud services or infrastructure providers.
-
Example:
provider "aws" { region = "us-west-2" }
3.6 backend.tf
-
Purpose: Configures the backend where Terraform stores its state file. Common backends include AWS S3, HashiCorp Consul, and Terraform Cloud.
-
Example:
terraform { backend "s3" { bucket = "my-tf-state-bucket" key = "terraform/state" region = "us-west-2" } }
3.7 versions.tf
-
Purpose: Specifies the versions of Terraform and providers required for your project. Ensures compatibility and prevents unexpected issues due to version changes.
-
Example:
terraform { required_version = ">= 1.0.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 3.0" } } }
3.8 modules/
-
Purpose: Contains reusable Terraform modules. Each module has its own directory with its own
main.tf
,variables.tf
, andoutputs.tf
. Modules allow for better organization and reuse of code. -
Structure:
module1/
: Contains the first module's Terraform files.module2/
: Contains the second module's Terraform files.
Example of a module:
# modules/module1/main.tf resource "aws_s3_bucket" "example" { bucket = "example-bucket" acl = "private" }
4. Best Practices
- Keep Code DRY: Reuse modules to avoid duplication.
- Organize by Functionality: Group related resources and configurations together.
- Separate Environments: Use different directories or workspaces for different environments (e.g.,
dev
,staging
,prod
).
5. Conclusion
Adhering to a structured Terraform directory layout enhances the readability and maintainability of your Terraform configurations. This organization ensures that your infrastructure code remains manageable as it grows.
Feel free to adapt this structure based on your specific needs and project requirements. If you have any more questions or need further customization, just let me know! π