Terraform

Can we create an S3 Bucket using Terraform

Terraform is an Infrastructure as a Code utility that allows to provision and manage cloud infrastructure quickly, efficiently, and safely.

Using this tutorial, you will learn how to create an Amazon S3 bucket using Terraform.

Let’s get started.

Step 1 – Signup for AWS Account

The first step is to create an Amazon Web Services account. Creating an account will allow you to access all Amazon Cloud services.

Open your browser and navigate to the following resource.

https://aws.amazon.com/free

Follow the signup process provided to get access to the AWS Cloud services.

Step 2 – Install Terraform

To use Terraform, you need to install it on your local system. You can find Terraform as a binary package for your distribution or install it via a package manager.

This tutorial will use the package manager to install it on an Ubuntu system.

Start by updating your system as:

sudo apt-get update

Install the following packages to use Hashicorp’s signature and repositories.

sudo apt-get install -y gnupg software-properties-common curl

Download and add Hashicorp’s GPG key:

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

Add the repositories:

sudo apt-add-repository "deb [arch=amd64]

https://apt.releases.hashicorp.com $(lsb_release -cs) main"

Update and Install Terraform:

sudo apt-get update && sudo apt-get install terraform

Verify Terraform is installed:

[email protected]:~$ terraform -version

Terraform v1.0.11

on linux_amd64

Step 3 – Install AWS CLI

To use Terraform on AWS, you need to install the AWS CLI tools. Open the terminal and enter the commands:

sudo apt-get update && sudo apt-get install unzip -y

Download the AWS archive:

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

Unzip the archive as:

unzip awscliv2.zip

Navigate into the directory and install

cd aws && sudo ./install

Check if installed using the command:

aws –-version

aws-cli/2.4.0 Python/3.8.8 Linux/5.10.16.3-microsoft-standard-

WSL2 exe/x86_64.ubuntu.20 prompt/off

Step 4 – Write S3 Terraform Configuration

Once you have all the tools and utilities installed, the next step is to create a Terraform configuration to provision an S3 bucket on AWS.

Terraform requires every configuration to reside in its directory. Start by creating a working directory as:

mkdir aws-s3

Navigate into the directory and create a Terraform configuration.

cd aws-s3 && touch s3-bucket.tf

Open the file and add the following configuration to create an S3 bucket using your favorite text editor.

terraform {

  required_providers {

    aws = {

      source = "hashicorp/aws"

    }

  }

  required_version = ">= 0.12"

}

provider "aws" {

  profile = "default"

  region  = "us-east-1"

}

resource "aws_s3_bucket" "b" {

  bucket = "my_s3_bucket"

  acl    = private

}

Save and close the file.

Step 5 – Initialize Directory

Once you have configuration created, initialize the directory using the command:

terraform init

You should see an output as below as Terraform installs the required plugins.

ubuntu@cs:~/aws-s3$ terraform init

Initializing the backend...

Initializing provider plugins...

- Finding latest version of hashicorp/aws...

- Installing hashicorp/aws v3.66.0...

Terraform will download and install the AWS provider plugin as defined in the configuration file. This will create a hidden .terraform directory and store all your providers’ plugins.

Step 6 – Format and Validate Terraform Configuration

Although this step is not required, it is good to ensure you use the recommended formatting and validate if the configuration file contains any errors.

Use the command below to format the file.

terraform fmt

Next, validate the configuration file using the command:

terraform validate

The command should return a success message if no errors are found.

Step 7 – Apply the Changes

Once completed, run the command to apply the changes as:

terraform apply

Wrap Up

This tutorial shows you how to use Terraform to create an Amazon S3 bucket in a few simple steps. Check the documentation to learn more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list