Comprehensive Guide to AWS S3 Bucket Creation and Management with Terraform
Introduction
Amazon S3 (Simple Storage Service) is one of the most popular services provided by AWS. It offers scalable, secure, and high-performance object storage, which can be used for various purposes like data storage, backups, hosting static websites, and more.
In this article, we will go through the process of creating and managing an S3 bucket using Terraform.
Terraform is a tool that allows you to define and provision infrastructure using code. This guide is designed to be easy to follow, even for beginners.
What is Amazon S3?
Amazon S3 is a service that lets you store and retrieve any amount of data at any time, from anywhere on the web.
It's like a digital storage unit that can hold an unlimited amount of data, such as files, images, videos, and more.
What is Terraform?
Terraform is a tool that allows you to write code to define and manage your infrastructure.
With Terraform, you can describe your cloud resources (like S3 buckets, virtual machines, etc.) in configuration files, and then Terraform will create those resources for you. It's like writing a recipe and having a robot chef cook it for you exactly as you described.
Prerequisites
Before we start, make sure you have:
An AWS account.
AWS CLI installed and configured with your AWS credentials.
Terraform installed on your computer.
Step-by-Step Solution
Step 1 : Install and Configure AWS CLI
If you haven’t installed AWS CLI yet, follow these steps:
Download and install the AWS CLI from the official AWS website.
Open your terminal (Command Prompt, PowerShell, or a terminal emulator).
Configure AWS CLI with your credentials by running:
aws configure
You will be prompted to enter your AWS Access Key, Secret Key, region, and output format. You can find your Access Key and Secret Key in the AWS Management Console.
Step 2 : Install Terraform
Download and install Terraform from the official Terraform website. Follow the instructions for your operating system to install it.
Step 3 : Create a Terraform Configuration File
Create a new directory for your Terraform project:
mkdir terraform-s3-bucket cd terraform-s3-bucket
Create a new file named
main.tf
and open it in your preferred text editor. This file will contain the Terraform configuration for creating an S3 bucket.
Step 4 : Define the AWS Provider
In the main.tf
file, specify the AWS provider. The provider allows Terraform to interact with AWS resources:
provider "aws" {
region = "us-west-2"
}
Replace us-west-2
with your desired AWS region.
Step 5 : Create an S3 Bucket
Next, define the S3 bucket resource:
resource "aws_s3_bucket" "example" {
bucket = "my-unique-bucket-name"
acl = "public-read"
}
Replace my-unique-bucket-name
with a unique name for your bucket. The acl
(Access Control List) is set to public-read
to allow public read access. This means anyone can view the files in your bucket, but only you can upload or delete them.
Step 6 : Create an S3 Bucket Policy
To give read-only access to a specific IAM user or role, define an S3 bucket policy:
resource "aws_s3_bucket_policy" "example" {
bucket = aws_s3_bucket.example.bucket
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = ["s3:GetObject"]
Effect = "Allow"
Principal = {
AWS = "arn:aws:iam::123456789012:user/my-iam-user"
}
Resource = ["${aws_s3_bucket.example.arn}/*"]
},
]
})
}
Replace 123456789012:user/my-iam-user
with the ARN (Amazon Resource Name) of your IAM user or role. This policy allows the specified IAM user to read (but not write or delete) objects in your bucket.
Step 7 : Enable Versioning on the S3 Bucket
To enable versioning on your S3 bucket, which keeps track of changes to your files, add the following configuration:
resource "aws_s3_bucket_versioning" "example" {
bucket = aws_s3_bucket.example.bucket
versioning_configuration {
status = "Enabled"
}
}
Versioning is useful because it allows you to recover previous versions of your files if they are accidentally deleted or overwritten.
Step 8 : Initialize and Apply the Configuration
Initialize the Terraform configuration. This command sets up the necessary plugins for Terraform to work with AWS:
terraform init
Apply the configuration to create the S3 bucket and other resources. Terraform will show you a plan of what it will do. Type
yes
to confirm and proceed:terraform apply
Conclusion
Congratulations! You have successfully created and managed an AWS S3 bucket using Terraform. Here’s a quick recap of what we did:
Installed and configured AWS CLI and Terraform.
Created an S3 bucket with public read access.
Set up an S3 bucket policy to allow read-only access to a specific IAM user or role.
Enabled versioning on the S3 bucket to keep track of all changes.
By following these steps, you can use Terraform to automate the creation and management of AWS resources, making your cloud infrastructure more consistent and efficient.
Additional Resources
I hope this guide helps you get started with AWS S3 and Terraform.
HAPPY LEARNING 🙂