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.
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:
Install the following packages to use Hashicorp’s signature and repositories.
Download and add Hashicorp’s GPG key:
Add the repositories:
https://apt.releases.hashicorp.com $(lsb_release -cs) main"
Update and Install Terraform:
Verify Terraform is installed:
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:
Download the AWS archive:
Unzip the archive as:
Navigate into the directory and install
Check if installed using the command:
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:
Navigate into the directory and create a Terraform configuration.
Open the file and add the following configuration to create an S3 bucket using your favorite text editor.
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:
You should see an output as below as Terraform installs the required plugins.
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.
Next, validate the configuration file using the command:
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:
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.