My 2nd Day with Terraform: Deploying an S3 Bucket and Managing Resources

Soumen BhuniaSoumen Bhunia
3 min read

Hey there! Today, I want to share my second day of working with Terraform. It’s been a journey from basic bucket creation to adding unique identifiers. I’ll document every single command, file, and snippet I used.

Setting Up

First things first, I exported my environment variables and initialized the Terraform project:

source ../.env
terraform init

Terraform initialized successfully:

Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "6.0.0-beta2"...
- Installing hashicorp/aws v6.0.0-beta2...
- Installed hashicorp/aws v6.0.0-beta2 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl...
Terraform has been successfully initialized!

Validating Configuration

Before deploying any resources, I always validate my configurations to catch syntax errors:

terraform validate

Success!

Success! The configuration is valid.

Planning the Infrastructure

I ran a plan to see what resources Terraform would create:

terraform plan

Terraform showed:

Plan: 1 to add, 0 to change, 0 to destroy.

Applying the Plan

Now, I applied the plan:

terraform apply

Terraform asked for confirmation:

Do you want to perform these actions?
  Only 'yes' will be accepted to approve.

  Enter a value: yes

I entered yes and saw:

aws_s3_bucket.demo-bucket: Creation complete after 6s [id=soumen-tf-bucket-05]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

The Terraform Configuration

Here’s the configuration I used:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "6.0.0-beta2"
    }
  }
}

provider "aws" {
  region = "us-east-2"
}

resource "aws_s3_bucket" "demo-bucket" {
  bucket = "soumen-tf-bucket-05"
}

Adding an S3 Object

I wanted to upload a file to the bucket. I created a file called myfile.txt:

Hi this is soumen

Updated my Terraform configuration:

resource "aws_s3_object" "bucket-data" {
  bucket = aws_s3_bucket.demo-bucket.bucket
  source = "./myfile.txt"
  key    = "mydata.txt"
}

Validated again:

terraform validate
Success! The configuration is valid.

Applied the plan:

terraform apply
Do you want to perform these actions?
  Only 'yes' will be accepted to approve.

  Enter a value: yes
aws_s3_object.bucket-data: Creation complete after 1s [id=mydata.txt]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Adding Randomized Bucket Names

To make the bucket name unique, I added a random_id resource:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "6.0.0-beta2"
    }
    random = {
      source  = "hashicorp/random"
      version = "3.7.2"
    }
  }
}

provider "aws" {
  region = "us-east-2"
}

resource "random_id" "ran_id" {
  byte_length = 6
}

resource "aws_s3_bucket" "demo-bucket" {
  bucket = lower("soumen-bucket-${random_id.ran_id.hex}")
}

resource "aws_s3_object" "bucket-data" {
  bucket = aws_s3_bucket.demo-bucket.bucket
  source = "./myfile.txt"
  key    = "mydata.txt"
}

output "name" {
  value = random_id.ran_id.hex
}

Initialized and applied:

terraform init -upgrade
terraform validate
terraform apply

Approved the actions:

Do you want to perform these actions?
  Only 'yes' will be accepted to approve.

  Enter a value: yes

Terraform generated a random ID and created the bucket:

Outputs:
name = "4e2c4c1033fd"

Main points

Initialize a Terraform project

Validate and plan resources

Create an S3 bucket and upload files

Use random IDs for unique bucket names

Output random identifiers

Thanks for Reading!

Hope this gives you some perspective.

Coming Up Next:

More hands-on cloud projects and DevOps tips — stay tuned!

Let’s Connect:

Share your thoughts in the comments or reach out on LinkedIn. Your feedback means a lot! You can also check out my work on GitHub.

0
Subscribe to my newsletter

Read articles from Soumen Bhunia directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Soumen Bhunia
Soumen Bhunia

Hello, my name is Soumen Bhunia, and I'm a student at a university with a focus on cybersecurity and advanced networking. My passion is using machine learning, cloud computing, DevOps, and MLOps to create systems that are intelligent, scalable, and secure. By incorporating automation and intelligence into every tier of digital infrastructure, I hope to address current cybersecurity issues.