Create an S3 Bucket on AWS using Terraform

In this article, I will show you how to use Terraform to create an S3 bucket on AWS. Before proceeding, I assume that you are familiar with S3 bucket if not then you can click here to see the steps to create an S3 bucket from the AWS console. 


  1. Basic understanding of Terraform.
  2. Terraform installed on your system.
  3. AWS Account (Create if you don’t have one).
  4. 'access_key' & 'secret_key' of an AWS IAM User. (Click here to learn to create an IAM user with 'access_key' & 'secret_key' on AWS, )

What we will do

  1. Write Terraform configuration files for S3 Bucket.
  2. Create an S3 Bucket using the Terraform configuration files.
  3. Delete the created S3 Bucket using Terraform.

Write Terraform configuration files for S3 Bucket

Create a dedicated directory where you can create terraform configuration files.

Use the following command to create a directory and change your present working directory to it.

mkdir terraform
cd terraform/

 I am using "vim" as an editor to write in files, you can use an editor of your choice and copy paste the following configurations to create, terraform.tfvars and


Create '' which is responsible to create an S3 Bucket  on AWS. This will read values of variables from and terraform.tfvars.

provider "aws" {
      region     = "${var.region}"
      access_key = "${var.access_key}"
      secret_key = "${var.secret_key}"

resource "aws_s3_bucket" "this" {
  bucket                               = "${var.bucket_name}"
  force_destroy                        = "${var.force_destroy}"
  region                               = "${var.region}"
  tags                                 = "${merge(var.tags, map("Name", format("%s", var.bucket_name)))}"


  • bucket: The name of the bucket. If omitted, Terraform will assign a random, unique name.
  • force_destroy: A boolean that indicates all objects should be deleted from the bucket so that the bucket can be destroyed without error.
  • region: The AWS region this bucket should reside in.
  • tags: A map of tags to assign to the bucket.

Change the value of "region" if you want to create the Bucket in some region other than what I have specified.

Create '' which contains the declaration and definition of the variables.

variable "access_key" {
     description = "Access key to AWS console"
variable "secret_key" {
     description = "Secret key to AWS console"
variable "region" {
     description = "Region of AWS VPC"

variable "bucket_name" {
  description = "(Required) Creates a unique bucket name"
  type        = "string"
  default     = "test-bucket-rahul-delete"

variable "force_destroy" {
  description = "(Optional) A boolean that indicates all objects should be deleted from the bucket so that the bucket can be destroyed without error"
  type        = "string"
  default     = true

variable "tags" {
  description = "(Optional) A mapping of tags to assign to the bucket"
  type        = "map"
  default     = {"env": "test"}

Once you have created '', do not forget to change values assigned to variable. You must change the values highlighted as these are specific to my environment. You can keep the rest variables as is.

Create 'terraform.tfvars' which contains the definition of access_key and secret_key variables defined in the above file along with "region" variable.

The following keys need to be changed with the keys of your IAM user.

vim terraform.tfvars
region = "eu-west-3"
access_key = "AKIAQ6GAIA5XFLXF6HOV"
secret_key = "/lJ3tFDkIYqr0rNX7aJqaXyJR8uCeFMiwuEW6aA/"

Now, you should have 3 files, viz,, terraform.tfvars and


Create an S3 Bucket using the Terraform configuration files

Before you execute the following commands make sure you have configured the valid access_key and secret_key.

The first command to be used is 'terraform init'. This command downloads and installs plugins for providers used within the configuration. In our case it is AWS.

 terraform init

Terraform init

The second command to be used is 'terraform plan'. This command is used to see the changes that will take place on the infrastructure.

 terraform plan

 terraform plan

'terraform apply' command will create the resources on the AWS mentioned in the file. You will be prompted to provide your input to create the resources.

terraform apply

terraform apply

When you execute the above command, upon successful completion, you can see that 1 new resource has been added and 0 has been destroyed.

You can go to the AWS S3 console to verify if the S3 Bucket is created or not.


Delete S3 Bucket using Terraform

If you no longer require a resource you created using the configuration mentioned in the file, You can use the "terraform destroy" command to delete all those resources. Here, the S3 bucket will get deleted upon executing the following command.

terraform destroy

 terraform destroy

In the above screenshot, you can see that the resource has been destroyed.


In this article, we saw the steps to create an S3 Bucket using Terraform. We also saw how the Bucket we created can be deleted in just one command.

Share this page:

0 Comment(s)