0
0
Terraformcloud~5 mins

Why the workflow matters in Terraform - Why It Works

Choose your learning style9 modes available
Introduction
When you manage infrastructure with code, following the right steps in order is very important. This helps avoid mistakes and keeps your cloud resources safe and organized.
When you want to create a new server or database in the cloud using code.
When you need to update your cloud resources without breaking anything.
When you want to see what changes will happen before applying them.
When you want to keep track of your infrastructure changes over time.
When you want to share your infrastructure setup with your team safely.
Config File - main.tf
main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
  required_version = ">= 1.3.0"
}

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

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

This file tells Terraform to use the AWS provider in the us-east-1 region. It defines one resource: an S3 bucket named "example-terraform-bucket-12345" with private access.

The terraform block sets the provider version and Terraform version requirements.

Commands
This command sets up Terraform in your folder. It downloads the AWS provider plugin so Terraform can talk to AWS.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "~> 4.0"... - Installing hashicorp/aws v4.60.0... - Installed hashicorp/aws v4.60.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 if you apply your changes. It helps you check before making any real changes.
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" + force_destroy = false + id = (known after apply) + region = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy.
This command makes the changes in your cloud. The flag skips asking for confirmation to speed up the process.
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 - Skip confirmation prompt to apply changes immediately
This command removes the resources you created. The flag skips confirmation to delete immediately.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Refreshing state... [id=example-terraform-bucket-12345] An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # aws_s3_bucket.example_bucket will be destroyed - resource "aws_s3_bucket" "example_bucket" { - acl = "private" -> null - bucket = "example-terraform-bucket-12345" -> null - force_destroy = false -> null - id = "example-terraform-bucket-12345" -> null - region = "us-east-1" -> null } Plan: 0 to add, 0 to change, 1 to destroy. aws_s3_bucket.example_bucket: Destroying... aws_s3_bucket.example_bucket: Destruction complete after 1s Destroy complete! Resources: 1 destroyed.
-auto-approve - Skip confirmation prompt to destroy resources immediately
Key Concept

If you remember nothing else from this pattern, remember: always initialize, plan, apply, and destroy in order to safely manage your infrastructure.

Common Mistakes
Running 'terraform apply' without running 'terraform init' first
Terraform won't have the necessary provider plugins downloaded, so it will fail to apply changes.
Always run 'terraform init' once before planning or applying changes.
Skipping 'terraform plan' and applying changes directly
You might make unintended changes or delete resources by accident.
Run 'terraform plan' to review changes before applying them.
Not destroying resources when cleaning up
Resources stay running and can cost money or cause conflicts.
Use 'terraform destroy' to remove resources when you no longer need them.
Summary
Run 'terraform init' to set up Terraform and download providers.
Use 'terraform plan' to preview changes before applying.
Apply changes with 'terraform apply' to create or update resources.
Clean up with 'terraform destroy' to remove resources safely.