DEV Community

Randika Madhushan Perera
Randika Madhushan Perera

Posted on

Deploying Apps to AWS with Terraform - Create S3

5. Create an S3 bucket with Terraform

Introduction

Amazon Web Services (AWS) offers Simple Storage Service (S3) for scalable object storage. Managing AWS infrastructure can be streamlined using Terraform, an Infrastructure as Code (IaC) tool. This article provides a step-by-step guide to creating an S3 bucket in AWS using Terraform.

Prerequisites

  • Basic knowledge of AWS services, particularly S3.
  • Terraform installed on your machine.
  • AWS CLI installed and configured with appropriate credentials.

Step 01: Initialize Terraform

Create a new directory for your Terraform project and initialize it: In my scenario, I have a directory called s3_bucket_codes and created the below Terraform code.

terraform-bucket.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region = "eu-west-2"
}

resource "aws_s3_bucket" "terraformbucket1995" {
  bucket = "terraform-bucket1995"

  tags = {
    Name        = "terraform-bucket1995"
    Environment = "Dev"
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 02: Init, Plan, and Apply

Run the following command to init your configuration:

terraform init
Enter fullscreen mode Exit fullscreen mode

Run the following command to plan and apply your configuration:

terraform plan
terraform apply
Enter fullscreen mode Exit fullscreen mode

Step 03: Verify the Bucket Creation

Once Terraform successfully applies the configuration, verify the creation of the S3 bucket in the AWS Management Console or using the AWS CLI.

Step 04: Clean Up Resources

To delete the created S3 bucket, simply run:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

This will remove the S3 bucket and any other resources defined in your Terraform configuration.

Top comments (0)