Terraform
Basics & Fundamentals
- Infrastructure as Code (IaC)
- Declarative Syntax in IaC
- Terraform Configuration Files
- Terraform CLI
- Terraform Init
- Terraform Plan
- Terraform Apply
- Terraform Destroy
Providers & Resources
๐ Terraform Apply: Executing Plans and Creating Real Infrastructure
If terraform plan
is like a blueprint for infrastructure, then terraform apply
is the construction crew that brings the blueprint to life.
The Terraform Apply command executes the changes defined in your configuration files (.tf
) and creates, updates, or deletes cloud resources as per the plan.
๐ In other words:
terraform plan
= What Terraform will do.terraform apply
= Actually doing it.
Without terraform apply
, no infrastructure would ever be deployed. Itโs the command that turns Infrastructure as Code (IaC) into real-world infrastructure.
โ๏ธ What Does Terraform Apply Do?
When you run:
terraform apply
Terraform performs:
-
Reads Configurations (
.tf
files)- Analyzes what resources are requested.
-
Loads State File (
terraform.tfstate
)- Compares current infrastructure with desired state.
-
Creates Execution Plan
- Proposes changes (like
terraform plan
).
- Proposes changes (like
-
User Approval (unless auto-approved)
-
Prompts:
Do you want to perform these actions?Only 'yes' will be accepted to approve.
-
-
Applies Changes
- Creates, updates, or deletes resources.
-
Updates State File
- Saves the new state of the infrastructure.
๐ Terraform Apply Syntax & Options
Basic usage:
terraform apply
Useful flags:
-
terraform apply -auto-approve
Skip approval prompt (use carefully in CI/CD). -
terraform apply plan.out
Apply a previously saved plan for consistent deployments. -
terraform apply -refresh=false
Skip refreshing state before apply. -
terraform apply -var
or-var-file
Inject variables when applying.
๐ 3 Unique Real-World Examples of Terraform Apply
โ Example 1: Deploying an AWS S3 Bucket
main.tf
provider "aws" { region = "us-east-1"}
resource "aws_s3_bucket" "example" { bucket = "terraform-apply-example-bucket" acl = "private"}
Run Commands:
terraform initterraform planterraform apply
Expected Output:
aws_s3_bucket.example: Creating...aws_s3_bucket.example: Creation complete after 3sApply complete! Resources: 1 added, 0 changed, 0 destroyed.
๐ The bucket is now live in AWS.
โ Example 2: Updating an Existing Resource
Modify bucket ACL:
resource "aws_s3_bucket" "example" { bucket = "terraform-apply-example-bucket" acl = "public-read"}
Run Apply:
terraform apply
Output:
aws_s3_bucket.example: Modifying...~ acl: "private" โ "public-read"Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
๐ terraform apply
updated the ACL without recreating the bucket.
โ Example 3: Destroying Resources
Remove S3 bucket block from main.tf
.
Run Apply:
terraform apply
Output:
aws_s3_bucket.example: Destroying...aws_s3_bucket.example: Destruction complete after 2sApply complete! Resources: 0 added, 0 changed, 1 destroyed.
๐ Deleting the block in code + running terraform apply
removes the resource from AWS.
๐ฏ Why is Terraform Apply Important?
-
Turns Code into Reality
- Converts
.tf
code into running infrastructure.
- Converts
-
Ensures Consistency
- Keeps infrastructure aligned with code (no drift).
-
Automates Infrastructure Deployment
- Eliminates manual provisioning steps.
-
Supports Continuous Delivery
- Easily integrated with CI/CD pipelines.
-
Auditable & Predictable
- Execution is logged and state files maintain history.
๐ง How to Remember Terraform Apply (Exam & Interview)
Mnemonic: A.C.E
- A โ Apply changes
- C โ Create/update/destroy resources
- E โ Execute the plan
๐ Interview Answer: โTerraform apply takes the execution plan and applies it, creating, modifying, or deleting resources as needed. Itโs the command that executes Infrastructure as Code.โ
๐ Best Practices for Terraform Apply
- Always run
terraform plan
first. - Use
-auto-approve
only in CI/CD pipelines, never manually. - Apply saved plans (
plan.out
) for reproducibility. - Use remote backends (S3, Azure Blob, GCS) for collaborative apply.
- Review apply logs carefully, especially in production.
๐ฎ Future Enhancements of Terraform Apply
- Policy enforcement โ Built-in compliance checks before apply.
- AI-based warnings โ Detect potentially risky actions.
- Visual apply previews โ Show graphical representation of resource changes.
- Rollback capability โ Undo failed applies more easily.
๐ Summary
-
terraform apply
is the command that executes your infrastructure plan. -
It creates, updates, or destroys resources to match
.tf
files. -
Steps:
- Reads config โ Compares state โ Proposes changes โ Awaits approval โ Executes.
-
Symbols:
+
Resource created~
Resource updated-
Resource destroyed
-
Remember with A.C.E โ Apply, Create, Execute.
โ Final Takeaway
Without terraform apply
, Terraform would be just a planning tool. Itโs the command that transforms your code into infrastructure reality.
- For beginners, itโs how you bring your first S3 bucket, VM, or network to life.
- For DevOps engineers, itโs the backbone of automated pipelines.
- For enterprises, it ensures scalable, predictable, and secure deployments.
๐ In short: Terraform Apply = Infrastructure Creation in Action.