Terraform S3 Bucket Creation with Example Configuration

Creating an Amazon S3 bucket with Terraform provides repeatable, version-controlled infrastructure for object storage. The approach shown in current AWS provider patterns keeps the bucket definition minimal and manages settings such as versioning, encryption, public access blocking, and ownership controls with separate resources.

Amazon S3 is a storage service offered by AWS for storing data lakes, websites, mobile applications, backups and restores, archives, and enterprise applications. The S3 stands for Simple Storage Service, which can be scaled based on individual or organization needs. In addition to providing storage, Amazon S3 also offers comprehensive access management, which can help you set up very granular permissions.

Provider and Bucket Definition

Terraform uses provider plugins to facilitate infrastructure management across all major cloud providers. For AWS, the provider is configured with a region and optional required_providers block.

A basic provider configuration is:

hcl provider "aws" { region = "us-east-1" }

With required_providers specified:

hcl terraform { required_providers { aws = { source = "hashicorp/aws" version = "4.64.0" } } } provider "aws" {}

The bucket resource is defined with the aws_s3_bucket resource type.

hcl resource "aws_s3_bucket" "s3" { bucket = "terraform-experiments" }

Using the resource block, a new resource of type aws_s3_bucket tells Terraform that we want to create a new S3 bucket in the AWS account. Inside the aws_s3_bucket block, the name of the bucket is specified using the bucket field. In this case, the name is terraform-experiments, but any name that meets the requirements for S3 bucket names can be chosen.

It is worth noting that while the bucket field is optional, it is considered a best practice to give resources descriptive names to help with organization and management.

A more explicit example with tags is:

hcl resource "aws_s3_bucket" "my_bucket" { bucket = "my-unique-bucket-name" tags = { Name = "MyS3Bucket" Environment = "Production" } }

In this example, we have defined an AWS provider specifying 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.

The main Terraform resources used for a modern S3 setup are:

  • aws_s3_bucket
  • aws_s3_object
  • aws_s3_bucket_public_access_block
  • aws_s3_bucket_ownership_controls

These resources let you create a bucket, upload objects, block public access, and manage bucket ownership behavior.

Project Setup and Initialization Workflow

Creating an S3 Bucket starts by creating a new directory for the project and a file named main.tf, where all the Terraform configurations will be placed.

bash mkdir terraform-s3 && touch terraform-s3/main.tf

Open main.tf in your preferred text editor and follow along with the basic provider configuration and the creation of a new S3 bucket.

Before we can apply our configuration and create the S3 bucket, we need to initialize the Terraform module by running terraform init in our terminal. This command downloads the necessary provider plugins and sets up the backend for storing the state file.

Step by step initialization and apply workflow:

  • Navigate to your Terraform configuration directory in the terminal and run the following command to initialize your working directory:
    bash terraform init
    This command downloads the necessary provider plugins and sets up your Terraform environment.

  • After initialization, run the following command to preview the changes Terraform will make to your infrastructure:
    bash terraform plan
    Review the output to ensure that Terraform will create the resources as expected. If everything looks good, you can proceed to the next step.

  • To see a preview of the resource that will be created, you can run the terraform plan command in your terminal. This will show you what changes Terraform will make to your infrastructure based on the configuration in your main.tf file.

  • To create the S3 bucket, execute the following command:
    bash terraform apply
    Terraform will prompt you to confirm the execution of the plan. Type yes and press Enter. Terraform will then create the S3 bucket and output information about the resources created.

Verification

Step 6: 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.

Congratulations! You've successfully created an AWS S3 bucket using Terraform. But this is just the beginning.

The screenshot from the AWS console S3 bucket shows the created bucket.

Modern Resource Separation

In modern Terraform, keep the bucket definition minimal and manage settings like versioning or encryption with separate resources when needed.

TL;DR:
Create the bucket with the aws_s3_bucket resource after configuring the AWS provider. In modern Terraform, keep the bucket definition minimal and manage settings like versioning or encryption with separate resources when needed.

```hcl
provider "aws" {
region = "us-east-1"
}

resource "awss3bucket" "example" {
bucket = "my-unique-bucket-name-12345"
}
```

Then run terraform init, terraform plan, and terraform apply.

In the updated configuration, Terraform may also show additional S3 resources such as public access blocking and ownership controls.

Note: This apply screenshot is illustrative and may not exactly match the current code sample. The latest version of this guide uses a more modern S3 configuration, so the number of resources created may be different.

Older Terraform examples often set acl = "private" on the bucket and then discuss ACLs as the main way to manage access. That is no longer the best default for new buckets.

Advanced Configurations

Versioning

Enabling versioning for your S3 bucket helps you manage and retain multiple versions of an object. To enable versioning, modify your main.tf file:

```hcl
resource "awss3bucket" "my_bucket" {
bucket = "my-unique-bucket-name"
tags = {
Name = "MyS3Bucket"
Environment = "Production"
}
}

resource "awss3bucketversioning" "versioningexample" {
bucket = awss3bucket.mybucket.id
versioning
configuration {
status = "Enabled"
}
}
```

Logging

You can configure S3 bucket logging to record all requests made to your bucket.

Public Access Blocking and Ownership Controls

Now that the bucket and objects are in place, the next step is to make sure the bucket follows the modern S3 access model.

The resources used are:

hcl resource "aws_s3_bucket_public_access_block" "example" resource "aws_s3_bucket_ownership_controls" "example"

This approach works well when you want Terraform to manage a small number of supporting files along with the infrastructure itself. For very large numbers of files or frequent bulk uploads, use a data transfer or deployment tool rather than Terraform.

There are many more things that you can do with Terraform and the S3 Bucket. Here is a guide on how to rename an AWS S3 bucket in Terraform, which can help you rename your S3 bucket.

Benefits of Terraform for S3

To get started, specify their 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.

  • Repeatable: Terraform configurations ensure your 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 your 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 the differences between the declared configuration and the actual infrastructure, facilitating updates and modifications.

Here are specific benefits of learning how to create an AWS S3 bucket using Terraform.

Prerequisites and Requirements

Prerequisites for Creating an AWS S3 Bucket with Terraform:

  • AWS Account: An active AWS account to access and utilize AWS services, including S3
  • Terraform installed
  • AWS credentials configured for Terraform

In addition, you'd need the following installed:

  • S3 on One Page

Store data efficiently. Our S3 cheat sheet covers storage classes, lifecycle policies, and security - everything you need for object storage.

HD quality, print-friendly. Stick it next to your desk.

If you are not yet familiar with Terraform, feel free to explore our comprehensive introduction to Terraform.

Resource Comparison

The following table summarizes the core resources referenced in modern S3 Terraform patterns.

Resource Purpose Typical Attributes
awss3bucket Create the bucket bucket, tags
awss3object Upload objects bucket, key, source
awss3bucket_versioning Enable versioning bucket, versioning_configuration status
awss3bucketpublicaccess_block Block public access bucket, blockpublicacls, ignorepublicacls, blockpublicpolicy, restrictpublicbuckets
awss3bucketownershipcontrols Manage ownership bucket, rule

Conclusion

Terraform S3 bucket creation is best handled with a minimal bucket resource and separate resources for versioning, logging, public access blocking, and ownership controls. This separation matches the current AWS provider patterns and avoids the legacy reliance on ACLs.

The workflow remains consistent: define provider and bucket, initialize with terraform init, preview with terraform plan, apply with terraform apply, and verify in the AWS Management Console S3 service.

With repeatable configurations, version control, reusable modules, and state management, Terraform provides an effective means for rolling back to previous versions of configuration if necessary and for understanding differences between declared configuration and actual infrastructure.

Sources

  1. KodeKloud Blog
  2. Spacelift Blog
  3. AWS Fundamentals

Related Posts