DEV Community

Cover image for How to provision and S3 storage with Terraform: Plan, check, Apply, approve
Emmanuel E. Ebenezer
Emmanuel E. Ebenezer

Posted on

How to provision and S3 storage with Terraform: Plan, check, Apply, approve

The four-step or should I say, two-step ritual every engineer configuring infrastructure eventually lives by.

In my previous posts, we explored what Terraform is, why it matters, its components, how to install it, and how to prepare it for real use.
Now it’s time to actually use it. Today, we’ll be provisioning an object storage bucket on AWS — using code.

If you haven’t been following from the start, here are some helpful resources to get you up to speed (assuming you use a Linux system):
What is terraform
Install terraform
Get started with Terraform on AWS

Alright, let's dive in.
A quick overview of what we would be doing today.

How to provision an S3 on AWS with Terraform

First, create a folder and your Terraform configuration file.

mkdir tf-practice
cd tf-practice
touch main.tf
Enter fullscreen mode Exit fullscreen mode

Before you go on, be sure to have aws credentials that would have access to the s3 resource.
Open main.tf with your favourite editor and paste this configuration.
Feel free to customise it using the documentation: S3 resource documentation.

terraform {
    required_providers {
      aws = {
        source = "hashicorp/aws"
        version = "~> 6.0"
      }
    }

}
provider "aws" {
    region = "eu-west-2"
    # profile = "<leave commented if you have only one aws profile>"
}
resource "aws_s3_bucket" "demo-resource-creation" {
  bucket = "<use a name you know nobody else would>"
  force_destroy = true

  tags = {
    Name        = "My bucket"
    Environment = "Demo"
  }
}
Enter fullscreen mode Exit fullscreen mode

Now we create the resources.
Make sure you’re still in the folder we created (tf-practice) and you’ve installed Terraform properly.Then use the following commands to perform the infrastructure magic lol.

terraform plan
Enter fullscreen mode Exit fullscreen mode
terraform apply
Enter fullscreen mode Exit fullscreen mode

Approve the changes and…
Ta-daa! You’ve just provisioned an S3 bucket on AWS using code.

Here’s something fun to try: update the tags to see how Terraform tracks and manages resource changes.

resource "aws_s3_bucket" "demo-resource-creation" {
  bucket = "<use a name you know nobody else would>"
  force_destroy = true

  tags = {
    Name        = "My bucket 2.0"
    Environment = "New Demo"
  }
}
Enter fullscreen mode Exit fullscreen mode

Run terraform apply again and check the AWS console, your updated tags should appear.
That still feels magical to me.

Now what do these commands do.

Terraform plan : A dry-run, it shows what Terraform will create, change, or destroy before it touches or changes anything.

Terraform apply: This is the command that tells Terraform to call the AWS API and actually create or update your resources.

And of course, my favourite command:

terraform destroy
It removes everything Terraform created, super helpful for cleaning up and avoiding unnecessary cloud bills.

When you are satisfied with the newly ingested IaC wisdom, run terraform destroy to destroy our practice s3 bucket.

Terraform plan : A dry-run.It shows what Terraform will create, change, or destroy before it touches or changes anything.

Terraform apply: This is the command that tells Terraform to call the AWS API and actually create or update your resources.

And of course, my favourite command Terraform destroy.
It removes everything Terraform created, super helpful for cleaning up and avoiding unnecessary cloud bills.

When you are satisfied with the newly ingested IaC wisdom, run terraform destroy to destroy our practice s3 bucket.

Learning this was genuinely exciting, and sharing it feels even better.
When I think about how software blends with infrastructure, and the kinds of tools I'd to build for cloud operations someday, understanding this flow helps shape my wild imaginations.

Hey, I'm upskilling with Piyush and the CloudOps Community on discord.

Thank you for reading and see ya tomorrow.

Top comments (0)