0
0
Terraformcloud~5 mins

Terraform apply for execution - Commands & Configuration

Choose your learning style9 modes available
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'.