How to Create an S3 Bucket with Terraform

Terraform simplifies deployment of S3 buckets and reduces provisioning time from several hours to a few seconds.

How to Create an S3 Bucket with Terraform

3 minutes
image_pdfimage_print

S3 buckets are Amazon’s cloud storage service. It’s not unusual for a large enterprise to deploy numerous S3 buckets for development, testing, and other services. 

Instead of manually provisioning several S3 buckets, you can use Terraform to deploy them within seconds. After creating a Terraform file, you can continue to deploy S3 buckets within seconds without having to configure each one of them.

When Would You Use S3 with Terraform?

If you need to deploy several S3 buckets for an application, you can manually configure them, but this takes several minutes, and you would need to repeat the same process next time you need AWS cloud storage

Instead, you can use a Terraform template to streamline the process. A Terraform template contains settings for the S3 bucket and can be used to send commands to the API to deploy multiple S3 buckets simultaneously within seconds so you don’t have to repeat the same steps manually. The only step you need to take is creating the Terraform files so they deploy the S3 buckets.

How to Create an S3 Bucket Using Terraform: An Example

In this example, two Terraform template files are created. They’re named bucket.tf and variables.tf. Both of these files will contain the S3 bucket name, configurations, access keys to user profiles so that the bucket can be accessible, and a region to store the bucket.

Step 1: Create the bucket.tf File

The bucket.tf file stores the name, region, and access keys for the S3 bucket. Use the following code in the bucket.tf file:

provider "aws" {

    access_key = "${var.aws_access_key}"

    secret_key = "${var.aws_secret_key}"

    region = "${var.region}"

}

module "s3" {

    source = "<path-to-S3-folder>"

    bucket_name = "your_bucket_name"       

}

resource "aws_s3_bucket" "temps3" {

    bucket = "${var.bucket_name}" 

    acl = "${var.acl_value}"   

}

Step 2: Create the variables.tf File

The variables.tf file contains the variable references you see in the bucket.tf file. Create a Terraform file with the following code:

variable "bucket_name" {}

variable "acl_value" {

    default = "private"

}

variable "aws_access_key" {

default = “<your_access_key>”

}

variable "aws_secret_key" {

default = “<your_secret_key>”

 }

variable "region" {

    default = "region"

}

Step 3: Execute the Commands in Terraform

To execute commands in Terraform, you first must initialize it. You can then apply the changes to your environment to create the S3 bucket. Type the following commands in Terraform:

terraform init

terraform apply

How to Create S3 Bucket Instances in Terraform

You use the same steps as above to create bucket instances. Follow these steps to create the bucket.tf file and variables.tf file and deploy S3 bucket instances.

Step 1: Create the bucket.tf File

The bucket.tf file stores the basic configurations for the S3 bucket instance. Use the following code in the bucket.tf file:

provider "aws" {

    access_key = "${var.aws_access_key}"

    secret_key = "${var.aws_secret_key}"

    region = "${var.region}"

}

module "s3" {

    source = "<path-to-S3-folder>"

    bucket_name = "your_bucket_name"       

}

resource "aws_s3_bucket" "temps3" {

    bucket = "${var.bucket_name}" 

    acl = "${var.acl_value}"   

}

Step 2: Create the variables.tf File

The variables.tf file contains the variable references you see in the bucket.tf file. Create a Terraform file with the following code:

variable "bucket_name" {}

variable "acl_value" {

    default = "private"

}

variable "aws_access_key" {

default = “<your_access_key>”

}

variable "aws_secret_key" {

default = “<your_secret_key>”

 }

variable "region" {

    default = "region"

}

Step 3: Execute the Commands in Terraform

If you already have Terraform initialized from the previous section, then you do not need to execute it again. If not, you must initialize Terraform before applying environment changes. Type the following commands in Terraform, but exclude the first command if you already have Terraform initialized:

terraform init

terraform apply

Conclusion

Terraform template files can be used to repeat the same process numerous times instead of manually configuring AWS cloud services every time you need them. You can cut down provisioning time from several hours to a few seconds.

Navigating changes at Broadcom VMware by modernizing your virtualization strategy for future flexibility, certainty and scale

Written By: