Your First Terraform Apply: init, plan, apply and destroy an S3 Bucket the Safe Way
What you’ll learn
- Read every line of a small Terraform configuration split over three files
- Run init, fmt, validate, plan and apply – applying a saved plan, the way pipelines do
- Check the result in the S3 console
- Destroy it with a saved destroy plan, and delete the plan files
What you need
- Lesson 002 (Terraform, the AWS CLI signed in, AWS_PROFILE set)
The build-logs bucket
First, what we create, and why. A bucket for build logs. Versioned, so an overwritten log can always be recovered. And private, because logs can hold internal details. This time the code is in three files. Terraform reads every dot tf file in a folder as one configuration, so we give each file one job.
We work in the lesson folder. cd moves us into it. It's empty for now.
cd terraform/003-first-apply
The first file is providers.tf, where we say which provider we use, and where. touch creates it.
touch providers.tf
In providers.tf, the terraform block asks for the AWS provider, version 6. And the provider block sets the Region, us-east-1.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
The second file is main.tf, for what we build. touch creates it too.
touch main.tf
In main.tf, the bucket. Its type, aws S3 bucket, and our label, logs. Bucket prefix, and Terraform adds a unique ending. Two tags, the company and the lesson.
resource "aws_s3_bucket" "logs" {
bucket_prefix = "larkvale-lab-003-logs-"
tags = {
Company = "Larkvale Retail"
Lesson = "003"
}
}
resource "aws_s3_bucket_versioning" "logs" {
bucket = aws_s3_bucket.logs.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_public_access_block" "logs" {
bucket = aws_s3_bucket.logs.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
Versioning is its own resource. Bucket equals aws S3 bucket dot logs dot id. That's a reference to the bucket above. Terraform sees it, and knows to create the bucket first. Inside, status Enabled.
And the public access block, on the same bucket. Four settings, all true. Block public A C Ls, block public policies, ignore any public A C L, and restrict public buckets. Together, nobody outside the account can read the logs.
The last file is outputs.tf, for what Terraform prints when it's done. touch creates it, and one output prints the bucket's full name.
touch outputs.tf
output "bucket_name" {
value = aws_s3_bucket.logs.bucket
}

And these are the commands we run, in order. Init, format, validate, plan, apply, and at the end, destroy.
init, fmt and validate
terraform init downloads the AWS provider. Keep an eye on the Explorer. It adds a hidden .terraform folder, with the provider inside, and a lock file. Lesson 5 explains the lock file.
terraform init

terraform fmt lines up the code the standard way. It prints the name of every file it changed. Nothing printed, so our files were already tidy.
terraform fmt

terraform validate checks the code itself. Spelling, types, and references. The configuration is valid. It doesn't talk to AWS yet.
terraform validate

plan
Now terraform plan. Terraform signs in, checks what exists, and shows what it would change. -out=tfplan saves this exact plan to a file, so what we apply later is exactly what we reviewed. Watch the Explorer for the new file.
terraform plan -out=tfplan

There it is, tfplan, in the Explorer. Now read the plan. Each plus is something to create. Known after apply means AWS decides the value, like the bucket's full name. Plan, three to add, zero to change, zero to destroy. The bucket, its versioning, and its public access block. Exactly what we wrote.
apply
terraform apply tfplan. Applying a saved plan doesn't ask for yes, because you already reviewed it. Pipelines work the same way, later in this course. Watch the order. The bucket first, then the two settings that refer to it. And watch the Explorer, apply writes one more file.
terraform apply tfplan

Apply complete. Three added, and the output, our bucket's name.
Terraform remembers what it built, in the state file, terraform.tfstate, the new file in the Explorer. terraform state list shows the three resources it now manages. Module 3 is all about state.
terraform state list

Check it in the console
Now we test it. In S3, General purpose buckets, here's the bucket Terraform made. Open it.

On the Properties tab, Bucket Versioning is Enabled.

Further down, Tags. Company, Larkvale Retail, and Lesson, 003, from our code.

And on the Permissions tab, Block all public access is On. Everything we wrote, and nothing we didn't.

destroy
It works, and this was a lab, so we remove it. The safe way is a destroy plan. terraform plan -destroy, saved to destroy.tfplan, which appears in the Explorer. Three to destroy.
terraform plan -destroy -out=destroy.tfplan

Then apply that plan. terraform destroy does the same in one step, and asks you to type yes.
terraform apply destroy.tfplan

Apply complete. Three destroyed. The settings first, then the bucket, the reverse of how they were built.
Saved plans can hold secret values, so never commit them to Git. Delete them when you're done. rm (remove), deletes files. Both plan files leave the Explorer.
rm tfplan destroy.tfplan

Back in S3. The bucket is gone. Nothing from this lesson is left.

Summary
- Terraform reads every .tf file in a folder as one configuration
- A reference like aws_s3_bucket.logs.id links resources, and tells Terraform what to build first
- plan -out=tfplan saves the reviewed plan; apply tfplan makes exactly that change, with no surprises
- The state file records what Terraform built; terraform state list shows it
- plan -destroy -out, then apply, removes everything this code built; delete the plan files – they can hold secrets
- Cost of this lesson: free (one empty bucket for a few minutes)
What’s next
Next lesson, we read plans that do more than create. Changes, replacements, and deletes, and how to spot the one that would lose your data.