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
Terraform apply for execution
📖 Scenario: You are working as a cloud engineer. You want to create a simple infrastructure using Terraform. You will start by defining a basic resource, then configure a variable, apply the main Terraform logic, and finally run the Terraform apply command to execute the deployment.
🎯 Goal: Build a Terraform configuration that defines an AWS S3 bucket resource, uses a variable for the bucket name, and applies the configuration to create the bucket.
📋 What You'll Learn
Create a Terraform resource for an AWS S3 bucket with a specific name
Define a variable for the bucket name
Use the variable in the resource configuration
Run the Terraform apply command to deploy the infrastructure
💡 Why This Matters
🌍 Real World
Terraform is widely used to automate cloud infrastructure deployment. Defining resources and variables is a fundamental skill for managing cloud resources efficiently.
💼 Career
Cloud engineers and DevOps professionals use Terraform to create, update, and manage infrastructure as code, enabling repeatable and consistent deployments.
Progress0 / 4 steps
1
Define the AWS S3 bucket resource
Create a Terraform resource block named aws_s3_bucket with the resource name my_bucket. Set the bucket attribute to the exact string "my-unique-bucket-12345".
Terraform
Hint
Use the resource keyword followed by the resource type aws_s3_bucket and the resource name my_bucket. Inside the block, set the bucket attribute to the bucket name string.
2
Add a variable for the bucket name
Define a Terraform variable named bucket_name with the default value "my-unique-bucket-12345".
Terraform
Hint
Use the variable keyword followed by the variable name bucket_name. Inside the block, set the default attribute to the bucket name string.
3
Use the variable in the S3 bucket resource
Modify the aws_s3_bucket resource named my_bucket to use the variable bucket_name for the bucket attribute instead of the fixed string.
Terraform
Hint
Replace the fixed bucket name string with var.bucket_name to use the variable value.
4
Run Terraform apply to deploy the bucket
Add the Terraform command terraform apply -auto-approve as a comment to indicate running the apply step to create the S3 bucket.
Terraform
Hint
Add a comment with the exact command terraform apply -auto-approve to show the deployment step.
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
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.
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.
Final Answer:
It creates or changes cloud resources based on your configuration. -> Option B
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
Step 1: Recall terraform apply options for skipping approval
The official option to skip manual approval is -auto-approve.
Step 2: Verify other options
Options B, C, and D are not valid terraform flags and will cause errors.
Final Answer:
terraform apply -auto-approve -> Option C
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
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.
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.
Final Answer:
Two resources will be created, one resource will be updated, none deleted. -> Option A
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
Step 1: Understand the error message
The error says no configuration files found, meaning terraform cannot find any .tf files in the current folder.
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.
Final Answer:
You ran terraform apply in a directory without terraform files. -> Option A
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
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.
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.
Final Answer:
Use terraform apply -auto-approve after running terraform plan and saving the plan file. -> Option D
Quick Check:
Plan then apply with -auto-approve for automation [OK]
Hint: Plan first, then apply with -auto-approve in automation [OK]