0
0
Terraformcloud~5 mins

Test file structure in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
When working with Terraform, organizing your files clearly helps you manage your infrastructure easily. A good file structure separates variables, resources, and outputs so you can find and update things quickly.
When you start a new Terraform project and want to keep your code clean and easy to understand.
When you want to share your Terraform code with teammates and make it simple for them to follow.
When your infrastructure grows and you need to add more resources without confusion.
When you want to reuse variables and outputs across different parts of your project.
When you want to avoid mistakes by clearly separating configuration parts.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example_bucket" {
  bucket = "example-terraform-bucket-12345"
  acl    = "private"
}

This main.tf file defines the Terraform version, sets the AWS provider with a region, and creates a simple S3 bucket resource. This is the core file where resources live.

Commands
This command initializes the Terraform project by downloading necessary provider plugins and setting up the backend.
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.
This command shows what Terraform will do when you apply your configuration. It previews the changes without making them.
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-bucket-12345" + id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the changes to create the resources defined in your configuration. The -auto-approve flag skips the confirmation prompt.
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-bucket-12345] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without asking for confirmation
This command deletes all resources created by Terraform in this project. The -auto-approve flag skips the confirmation prompt.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Destroying... aws_s3_bucket.example_bucket: Destruction complete after 1s Destroy complete! Resources: 1 destroyed.
-auto-approve - Automatically approve the destroy without asking for confirmation
Key Concept

If you remember nothing else from this pattern, remember: organize your Terraform files clearly to keep your infrastructure code simple and easy to manage.

Common Mistakes
Putting all Terraform code in one big file without separating variables and resources.
This makes the code hard to read and update, especially as the project grows.
Use separate files like variables.tf for variables, main.tf for resources, and outputs.tf for outputs.
Not running terraform init before other commands.
Terraform needs to download providers and set up the project before planning or applying.
Always run terraform init first when starting or after changing providers.
Summary
Create a main.tf file to define your provider and resources clearly.
Run terraform init to prepare your project before planning or applying.
Use terraform plan to preview changes and terraform apply to create resources.
Organize files to keep your infrastructure code clean and easy to maintain.