On Day 3 of my #30DaysOfAWS with Terraform challenge, I learned how to create and manage an AWS S3 bucket using Terraform. This is a very short blog as creating an S3 bucket with terraform will be very easy and quick.
Topics Covered
Authentication and Authorization to AWS resources
S3 bucket management
Prerequisites
Create AWS Account: Sign up for AWS free tier if you don't have an account
Install AWS CLI: Download and install from AWS official website
Configure Credentials: Set up your AWS access keys
Install TerraformL Download and Install from terraform official docs.
Authentication
Before creating resources, you need to configure AWS credentials for Terraform to authenticate with AWS APIs.
Authentication Methods
- AWS CLI Configuration: aws configure
- Environment Variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
- IAM Roles: For EC2 instances or AWS services
- AWS Profiles: Named credential profiles
In this blog, we will be using AWS CLI Configuration for authenticating with AWS.
Login to AWS, create a User and add the required permissions for that user like S3BucketFullAccess and other things if necessary.
Then go to Security Tab of that user and create access and secret access keys and keep them very confidential as exposing them would provide your aws account access to others.
aws configure
Enter your:
AWS Access Key ID
AWS Secret Access Key
Default region (e.g., us-east-1)
Default output format (json) [optional]
S3 bucket management:
S3 is one of the most commonly used AWS services for storing objects, hosting static websites, logs, backups, and more.
S3 bucket names must be globally unique, should contain only lowercase letters, numbers, hyphens and should be within 3 to 63 characters.
Below is the code block for creating a sample S3 Bucket:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
provider "aws" {
# Configuration options
region = "us-east-1"
}
# Create a S3 bucket
resource "aws_s3_bucket" "my_test_bucket" {
bucket = "my-test-bucket-26112025"
tags = {
Name = "Anil"
Environment = "Development"
}
}
The above block is a basic code for creating S3 bucket without any additional parameters. if you want to add some parameters like blocking public access and Enabling Versioning, you can do them by doing like below:
versioning_configuration {
status = "Enabled"
}
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
Run the below commands now for creation of S3 bucket in AWS through Terraform.
terraform init: For initialization of backend and provider plugins
terraform plan: For blueprint of what resources are we excatly creating.
terraform apply: For finally creation of resources.
terrform apply --auto-approve: It creates resources without any manual approval from us.
terraform destroy: To destroy the bucket.
You can use --auto-approve for both apply and destroy.
Once S3 bucket creation is successfull, Then try changing the tag Name or creating another new tag.
No need to run terraform init again, It is a one time setup.
And then run terraform plan or terraform apply again.
Then you can see the change like One resource will be getting modified.
After applying Terraform apply, you can finally see the tags getting changed on the properties tab of the S3 bucket.
Conclusion:
This is all you need to get started with creating S3 bucket using Terraform. Excited to create many more resources and doing more projects in AWS Cloud through terraform.
See you in the next blog.
Below is the Youtube Video for reference: Tech Tutorials with Piyush — “Creating an S3 bucket using Terraform”
Top comments (0)