DEV Community

Cover image for Creating AWS S3 Bucket using Terraform
Brian Mengo
Brian Mengo

Posted on

Creating AWS S3 Bucket using Terraform

Introduction
The first resource I create and provision using terraform is the AWS S3 bucket. By writing terraform configuration files (.tf files) to initialise providers and use terraform commands to interact with AWS APIs and provision resources.

AWS CLI for credentials setup before creating any resource in AWS using aws configure.
The below image shows the procedure of provisioning a simple S3 Bucket.

Terraform Configuration
The set up for this resource involves only two block: the provider configuration block and the S3 bucket resource block

The Provider Block
Since we are using the AWS provider we setup by specifying the region in which the resource will be created

provider "aws" {
  region = "us-east-1"
}
Enter fullscreen mode Exit fullscreen mode

S3 Bucket Resource Block

resource "aws_s3_bucket" "demo_bucket" {
  bucket = "devbrio53-store"

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

The Terraform resource block starts with the keyword resource, followed by the resource type aws_s3_bucket and a user-defined resource name (e.g., first_bucket).

The bucket name must be globally unique across AWS regions.

Tags are added as a nested block with key-value pairs, e.g., Name and Environment
Tags are strings enclosed in double quotes and enclosed within curly braces, representing a map/dictionary data type in Terraform.

Using Terraform AWS provider documentation to find the correct resource block for an S3 bucket (aws_s3_bucket).
The documentation provides example usage, argument references (mandatory and optional parameters), and guides how to customise the resource block.

Running Terraform Commands to Provision the Bucket

Terraform initialisation is run using the command:

terraform init
Enter fullscreen mode Exit fullscreen mode

This downloads the necessary provider plugins and initialises the backend.

Next, a plan is created using:

terraform plan
Enter fullscreen mode Exit fullscreen mode

This performs a dry run to preview the changes Terraform will apply without actually creating or modifying resources.

The command to apply the changes and create the bucket is:

terraform apply
Enter fullscreen mode Exit fullscreen mode

Terraform prompts for confirmation before applying changes; typing yes proceeds with the creation.

Running terraform plan again shows

  • Zero resources to add
  • One resource to change
  • Zero resources to destroy

Terraform performs a state comparison between the current AWS environment and the configuration file to detect changes.

To delete the created bucket and clean up resources, the command is:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

This removes everything created by the Terraform configuration. It’s a handy way to keep your AWS account clean while learning.

Today’s learning wasn’t just about creating an S3 bucket.

It was about understanding how Terraform connects with AWS, and why authentication is the foundation of everything in cloud.

A simple bucket but a meaningful step forward.

Top comments (0)