0
0
Terraformcloud~5 mins

Module structure and conventions in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform modules help organize and reuse infrastructure code. They solve the problem of repeating the same code by grouping resources into reusable units.
When you want to create a reusable set of infrastructure components like a network or server setup.
When you need to share infrastructure code across multiple projects or teams.
When you want to keep your Terraform code clean and easy to manage by splitting it into smaller parts.
When you want to version control infrastructure components separately.
When you want to test parts of your infrastructure independently before combining them.
Config File - main.tf
main.tf
variable "region" {
  description = "The cloud region to deploy resources"
  type        = string
  default     = "us-east-1"
}

resource "aws_s3_bucket" "example_bucket" {
  bucket = "example-terraform-module-bucket"
  acl    = "private"
  tags = {
    Name        = "ExampleBucket"
    Environment = "Dev"
  }
}

output "bucket_name" {
  description = "The name of the S3 bucket"
  value       = aws_s3_bucket.example_bucket.bucket
}

This file defines a simple Terraform module.

  • variable declares an input value for the module.
  • resource creates an AWS S3 bucket with a fixed name and tags.
  • output exposes the bucket name to whoever uses this module.
Commands
Initializes the Terraform working directory and downloads necessary provider plugins.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
Checks the module files for syntax errors and validates the configuration is correct.
Terminal
terraform validate
Expected OutputExpected
Success! The configuration is valid.
Shows the changes Terraform will make to create the resources defined in the module.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_s3_bucket.example_bucket will be created + resource "aws_s3_bucket" "example_bucket" { + acl = "private" + bucket = "example-terraform-module-bucket" + force_destroy = false + id = (known after apply) + tags = { + "Environment" = "Dev" + "Name" = "ExampleBucket" } } Plan: 1 to add, 0 to change, 0 to destroy.
Applies the planned changes to create the resources without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Creating... aws_s3_bucket.example_bucket: Creation complete after 2s [id=example-terraform-module-bucket] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips interactive approval before applying changes
Key Concept

If you remember nothing else from this pattern, remember: Terraform modules group related infrastructure code into reusable, easy-to-manage units.

Common Mistakes
Not including an output block to expose important resource attributes.
Without outputs, users of the module cannot access created resource details needed for integration.
Always define outputs for key resource attributes that other modules or configurations will need.
Hardcoding values inside the module instead of using variables.
Hardcoded values reduce reusability and flexibility of the module.
Use variables for values that may change between deployments and provide sensible defaults.
Not running 'terraform init' before other commands.
Terraform needs to download providers and initialize the working directory before planning or applying.
Always run 'terraform init' first when starting with a new or updated module.
Summary
Create a main.tf file defining variables, resources, and outputs for the module.
Run 'terraform init' to prepare the working directory and download providers.
Use 'terraform validate' to check for syntax and configuration errors.
Run 'terraform plan' to preview changes Terraform will make.
Apply changes with 'terraform apply -auto-approve' to create resources.