Bird
Raised Fist0
Terraformcloud~5 mins

Terraform apply for execution - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Terraform helps you create and change cloud resources automatically. The 'terraform apply' command makes your planned changes happen in the cloud.
When you want to create a new server or database in the cloud using code.
When you have changed your infrastructure code and want to update the cloud resources accordingly.
When you want to make sure your cloud setup matches the code you wrote.
When you want to safely add or remove cloud resources without doing it manually.
When you want to track changes to your cloud setup over time.
Config File - main.tf
main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
  required_version = ">= 1.0"
}

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

resource "aws_s3_bucket" "example_bucket" {
  bucket = "example-terraform-bucket-123456"
  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-123456" with private access.

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 before making any changes. It helps you check your changes safely.
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-123456" + id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the changes to your cloud. The '-auto-approve' flag skips the manual 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-123456] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
→
-auto-approve - Skips the confirmation prompt to apply changes immediately
This command shows the current state of your infrastructure as Terraform understands it.
Terminal
terraform show
Expected OutputExpected
aws_s3_bucket.example_bucket: id = example-terraform-bucket-123456 acl = private bucket = example-terraform-bucket-123456
Key Concept

If you remember nothing else from this pattern, remember: 'terraform apply' makes your planned cloud changes real and live.

Common Mistakes
Running 'terraform apply' without running 'terraform init' first
Terraform needs to download provider plugins before it can apply changes, so it will fail without initialization.
Always run 'terraform init' once before your first 'terraform apply' in a new folder.
Not reviewing the plan before applying changes
You might accidentally delete or change resources you did not intend to, causing downtime or data loss.
Run 'terraform plan' and carefully check the output before running 'terraform apply'.
Using 'terraform apply' without '-auto-approve' in automated scripts
The command will wait for manual confirmation and block automation.
Use 'terraform apply -auto-approve' in scripts to avoid manual intervention.
Summary
Run 'terraform init' to prepare Terraform and download necessary plugins.
Use 'terraform plan' to preview changes before applying them.
Apply changes with 'terraform apply', optionally using '-auto-approve' to skip confirmation.
Check the current infrastructure state with 'terraform show'.

Practice

(1/5)
1. What does the terraform apply command do in simple terms?
easy
A. It backs up your cloud resources to local storage.
B. It creates or changes cloud resources based on your configuration.
C. It only shows a list of resources without making changes.
D. It deletes all your cloud resources immediately.

Solution

  1. Step 1: Understand the purpose of terraform apply

    The command is used to make your cloud setup real by creating or updating resources defined in your configuration files.
  2. Step 2: Compare options with command behavior

    Only It creates or changes cloud resources based on your configuration. correctly describes this behavior. Options A, B, and C describe actions terraform apply does not perform.
  3. Final Answer:

    It creates or changes cloud resources based on your configuration. -> Option B
  4. Quick Check:

    terraform apply = create/change resources [OK]
Hint: Remember: apply means 'make changes happen' [OK]
Common Mistakes:
  • Thinking terraform apply only shows plans without applying
  • Confusing terraform apply with terraform destroy
  • Assuming terraform apply backs up resources
2. Which of the following is the correct syntax to run terraform apply without asking for approval?
easy
A. terraform apply -skip-confirm
B. terraform apply --no-approval
C. terraform apply -auto-approve
D. terraform apply --force

Solution

  1. Step 1: Recall terraform apply options for skipping approval

    The official option to skip manual approval is -auto-approve.
  2. Step 2: Verify other options

    Options B, C, and D are not valid terraform flags and will cause errors.
  3. Final Answer:

    terraform apply -auto-approve -> Option C
  4. Quick Check:

    -auto-approve skips approval [OK]
Hint: Use -auto-approve to skip approval prompt [OK]
Common Mistakes:
  • Using incorrect flags like --no-approval
  • Trying to use --force which is invalid
  • Assuming terraform apply never asks for approval
3. Given this terraform apply output snippet:
Plan: 2 to add, 1 to change, 0 to destroy.

What will happen after you approve the apply?
medium
A. Two resources will be created, one resource will be updated, none deleted.
B. Two resources will be deleted, one created, none updated.
C. One resource will be created, two updated, none deleted.
D. No changes will happen because destroy count is zero.

Solution

  1. Step 1: Interpret the plan summary

    The plan says 2 to add means 2 new resources will be created, 1 to change means 1 existing resource will be updated, and 0 to destroy means no resources will be deleted.
  2. Step 2: Match the plan to the options

    Only Two resources will be created, one resource will be updated, none deleted. correctly describes this outcome. Options A, C, and D misunderstand the counts.
  3. Final Answer:

    Two resources will be created, one resource will be updated, none deleted. -> Option A
  4. Quick Check:

    Plan add=2, change=1, destroy=0 [OK]
Hint: Add = create, change = update, destroy = delete [OK]
Common Mistakes:
  • Mixing up add and destroy counts
  • Assuming zero destroy means no changes at all
  • Confusing change with destroy
4. You run terraform apply but get an error: Error: No configuration files found. What is the likely cause?
medium
A. You ran terraform apply in a directory without terraform files.
B. You forgot to initialize terraform with terraform init.
C. You used the wrong terraform version.
D. You did not approve the plan before applying.

Solution

  1. Step 1: Understand the error message

    The error says no configuration files found, meaning terraform cannot find any .tf files in the current folder.
  2. Step 2: Evaluate options

    You ran terraform apply in a directory without terraform files. matches this cause. You forgot to initialize terraform with terraform init. would cause a different error about initialization. You used the wrong terraform version. is unlikely to cause this specific error. You did not approve the plan before applying. is unrelated because approval happens after plan generation.
  3. Final Answer:

    You ran terraform apply in a directory without terraform files. -> Option A
  4. Quick Check:

    No .tf files = error [OK]
Hint: Check current folder for .tf files before apply [OK]
Common Mistakes:
  • Assuming terraform init missing causes this error
  • Thinking approval is needed before apply runs
  • Blaming terraform version without checking files
5. You want to automate terraform apply in a CI/CD pipeline but avoid manual approval. Which command and practice is best?
hard
A. Use terraform apply --force to skip all checks.
B. Run terraform apply directly without plan or approval.
C. Run terraform plan only, then manually approve changes later.
D. Use terraform apply -auto-approve after running terraform plan and saving the plan file.

Solution

  1. Step 1: Understand CI/CD automation needs

    Automation requires no manual approval and safe application of changes. Saving a plan file ensures the exact changes are applied.
  2. Step 2: Evaluate command options

    Use terraform apply -auto-approve after running terraform plan and saving the plan file. correctly uses terraform plan to create a plan file, then applies it with -auto-approve to skip manual confirmation. Run terraform apply directly without plan or approval. skips planning, which is risky. Run terraform plan only, then manually approve changes later. requires manual approval, not automated. Use terraform apply --force to skip all checks. uses an invalid flag.
  3. Final Answer:

    Use terraform apply -auto-approve after running terraform plan and saving the plan file. -> Option D
  4. Quick Check:

    Plan then apply with -auto-approve for automation [OK]
Hint: Plan first, then apply with -auto-approve in automation [OK]
Common Mistakes:
  • Skipping plan step in automation
  • Using invalid flags like --force
  • Expecting manual approval in CI/CD