Creating an AWS S3 bucket with Terraform moves provisioning from manual console clicks to declarative infrastructure as code. Terraform defines the desired state of an S3 bucket, tracks it in a state file, and applies changes repeatably. The workflow is initialize, plan, apply, verify, and then layer advanced configurations such as versioning, logging, and access controls.
The reference implementations cover the community module for S3 buckets, a basic provider configuration for a bucket, and hands-on projects that demonstrate the init-plan-apply sequence and object uploads.
Why Terraform for S3
Infrastructure as Code allows S3 buckets to be defined declaratively, ensuring consistency and version control. Using Terraform eliminates manual clicking in the AWS Console, reducing human error. State Management tracks the state of the bucket, making updates and deletions safer and more predictable. Advanced Features can be layered directly within Terraform code, including Versioning, Logging, and IAM policies.
Why Terraform is attractive for infrastructure provisioning in AWS is that Terraform allows you to define infrastructure using code, providing an easily manageable way of expressing desired states with versioning, collaboration, and automation. Terraform has excellent support for AWS.
Specific benefits of learning how to create an AWS S3 bucket using Terraform include:
- Repeatable: Terraform configurations ensure S3 buckets will be created consistently every time the Terraform apply command is run.
- Version-controlled: Terraform configurations allow you to track changes made to S3 buckets over time, providing an effective means for rolling back to previous versions of configuration if necessary.
- Reusable: Terraform modules can be created to store common S3 bucket configurations. This saves both time and effort, as you won't need to write the same configuration repeatedly.
- State Management: Terraform maintains a state file that keeps track of the current state of the infrastructure, including AWS S3 buckets. This state file enables Terraform to understand differences between the declared configuration and the actual infrastructure, facilitating updates and modifications.
When working with applications hosted on the cloud, a data storage solution is typically needed. Storage options that can be set up in the cloud include object storage, block storage, and file storage. While they vary depending on the provider, they typically offer scalability, flexibility, and cost-effectiveness and can be easily set up with an IaC tool such as Terraform.
Prerequisites for Creating an S3 Bucket with Terraform
Before diving into the hands-on process, ensure you have these necessary prerequisites in place.
- AWS Account: An active AWS account to access and utilize AWS services, including S3
- AWS CLI for managing credentials and verifying resources
- Terraform installed locally
- Understanding of Terraform providers, resource blocks, state management, and the sequence of terraform init, plan, and apply
A project example notes services used were AWS S3 for storage and the AWS CLI for managing credentials and verifying resources. Key concepts learnt include infrastructure as code, Terraform providers, resource blocks, state management, the sequence of terraform init, plan, and apply, as well as automating the creation and configuration of cloud resources, applying access controls, and uploading objects to S3 buckets in a repeatable and consistent way.
Basic Terraform Configuration
To get started, specify desired properties, such as name, ACLs, and storage class, within a Terraform configuration file. Terraform will take care of creating the buckets in your AWS account.
A minimal main.tf file contains an AWS provider and a basic S3 bucket resource.
```hcl
provider "aws" {
region = "us-east-1"
}
resource "awss3bucket" "my_bucket" {
bucket = "my-unique-bucket-name"
tags = {
Name = "MyS3Bucket"
Environment = "Production"
}
}
```
In this example, the AWS provider specifies the region and a basic S3 bucket resource. The configuration uses "us-east-1" as the preferred AWS region and "my-unique-bucket-name" as the name for the S3 bucket.
Standard Workflow
The standard workflow for creating an S3 bucket is init, plan, apply, verify.
Step 1: Initialize Terraform
Navigate to your Terraform configuration directory in the terminal and run the following command to initialize your working directory:
terraform init
This command downloads the necessary provider plugins and sets up your Terraform environment. The first command is terraform init. This is the first command to run. It initializes the working directory and downloads the AWS provider.
Step 2: Preview Changes
After initialization, run the following command to preview the changes Terraform will make to your infrastructure:
terraform plan
Review the output to ensure Terraform will create the resources as expected. The second command would be to run a Terraform plan. This command shows the bucket, public access block, and ownership controls that Terraform is about to create.
Step 3: Apply Changes
To create the S3 bucket, execute the following command:
terraform apply
Terraform will prompt you to confirm execution of the plan. Type yes and press Enter. Terraform will then create the S3 bucket and output information about the resources created. Apply the Terraform configuration using the terraform apply command, which will eventually create an S3 bucket in AWS.
Step 4: Verify the S3 Bucket
Visit the AWS Management Console and navigate to the S3 service. You should see the newly created bucket with the name you specified in your Terraform configuration.
After the apply completes, you should see a new S3 bucket named spacelift-test1-s3 in your AWS account.
Advanced Configurations
Advanced configurations build on the basic bucket resource.
Versioning
Enabling versioning for your S3 bucket helps manage and retain multiple versions of an object.
```hcl
resource "awss3bucket" "my_bucket" {
bucket = "my-unique-bucket-name"
tags = {
Name = "MyS3Bucket"
Environment = "Production"
}
}
resource "awss3bucketversioning" "versioningexample" {
bucket = awss3bucket.mybucket.id
versioningconfiguration {
status = "Enabled"
}
}
```
The module approach supports versioning as a feature.
Logging
You can configure S3 bucket logging to record all requests made to your bucket.
Object Upload
After creating a bucket, files can be uploaded using the awss3object resource.
In the previous step, an S3 bucket was created with Terraform. In this step, files will be uploaded to that bucket using the awss3object resource.
If working from older examples, you may still see awss3bucket_object, but that resource has been deprecated.
Terraform AWS Modules for S3
A Terraform module which creates S3 bucket on AWS with all or almost all features provided by Terraform AWS provider.
Supported features include:
- static web-site hosting
- access logging
- versioning
- CORS
- lifecycle rules
- server-side encryption
- object locking
- Cross-Region Replication
- ELB log delivery bucket policy
- ALB/NLB log delivery bucket policy
- WAF log delivery bucket policy
- Account-level Public Access Block
- S3 Directory Bucket
- S3 Table Bucket
- S3 Vectors
Example module usage:
hcl
module "s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket = "my-s3-bucket"
acl = "private"
control_object_ownership = true
object_ownership = "ObjectWriter"
versioning = {
enabled = true
}
}
Log delivery bucket example:
hcl
module "s3_bucket_for_logs" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket = "my-s3-bucket-for-logs"
acl = "log-delivery-write"
force_destroy = true
control_object_ownership = true
object_ownership = "ObjectWriter"
attach_elb_log_delivery_policy = true
}
Variant with ALB and NLB log delivery:
hcl
module "s3_bucket_for_logs" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket = "my-s3-bucket-for-logs"
force_destroy = true
control_object_ownership = true
object_ownership = "ObjectWriter"
attach_elb_log_delivery_policy = true
attach_lb_log_delivery_policy = true
}
WAF logs module start:
hcl
module "s3_bucket_for_waf_logs" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket =
The module centralizes common settings such as object ownership control and log delivery policies.
Hands-On Project Experience
A documented project demonstrates creating S3 buckets using Terraform.
Project Link: View Project
Author: Abdulrahman Abdulkadir
Email: [email protected]
In this project, S3 buckets are created using Terraform. The goal is to automate provisioning of AWS S3 storage by writing Infrastructure as Code, ensuring consistency, repeatability, and easy management of bucket configurations such as versioning, access policies, and lifecycle rules.
This project took approximately 3 to 4 hours to complete. The most challenging part was correctly configuring AWS credentials and ensuring Terraform had the proper access to create and manage resources. It was most rewarding to see the S3 bucket and objects successfully deployed and managed automatically through Terraform, demonstrating the power of infrastructure as code.
The project was done to gain hands-on experience with Terraform and understand how to automate creation and management of AWS resources like S3 buckets.
Command Reference
The following table summarizes the core Terraform commands used for S3 bucket creation.
| Command | Purpose |
|---|---|
| terraform init | Initializes working directory and downloads AWS provider |
| terraform plan | Shows bucket, public access block, and ownership controls to be created |
| terraform apply | Creates S3 bucket in AWS |
Feature Comparison
The following table compares basic resource approach versus module approach.
| Aspect | Basic resource | Module |
|---|---|---|
| Configuration | Manual resource blocks | Parameterized module |
| Features | Manual addition per resource | Built-in support for static web-site hosting, access logging, versioning, CORS, lifecycle rules, server-side encryption, object locking, Cross-Region Replication, ELB/ALB/NLB/WAF log delivery policies, Public Access Block, S3 Directory/Table/Vectors |
| Reusability | Limited | High |
Conclusion
Terraform create s3 bucket workflows progress from a minimal provider and resource declaration to repeatable, version-controlled modules that encapsulate logging, versioning, encryption, and access policies. The init-plan-apply cycle provides safety through preview and state tracking. The community module expands coverage to ELB, ALB, NLB, and WAF log delivery, object ownership control, and newer S3 features. Hands-on practice shows that credential configuration is the most common friction point, while successful automated deployment validates the benefits of infrastructure as code for S3 storage.