AWS/Azure/OpenShift Linux

How to Delete AWS S3 Bucket and its Objects using  AWS CLI in Linux

S3-Bucket-1
S3 Buckets

The Amazon Simple Storage Service (Amazon S3) is a cloud hosting service that offers industry-leading performance, security, and scalability. Users and organizations of all sizes can use Amazon S3 to safeguard and store data for a variety of applications, including websites, mobile applications, data lakes, backups and restores, archives, enterprise applications, IoT devices, and big data analytics. Amazon S3 can be used to optimize, organize, and configure access to your data based on your specific business, organizational, and compliance requirements. Amazon S3 is an object cloud storage service that stores data in buckets as objects. A file and any metadata that describes the file are considered objects. A bucket is a container that holds objects. A bucket is a storage location for items in Amazon S3. A bucket can hold any number of items, and your account can hold up to 100 buckets.

The AWS Command Line Interface (AWS CLI) is a centralized management tool for your AWS services. You can control multiple AWS services from the command line and automate them using scripts with just one tool to install and set up. It is a free and open-source tool for interacting with AWS services via command-line shell commands. With minimal configuration, the AWS CLI allows you to start running commands that use functionality similar to that of the browser-based AWS Management Console from the command prompt in your terminal program.

In this post, I will take you through the following: installing the AWS CLI package on a local system, assigning permissions to the user, generating and configuring the access key ID and secret access key, and deleting the S3 bucket and its objects using the AWS CLI. We will carry out all the interactions with the AWS S3 bucket directly from the Linux terminal. To learn how to interact with the AWS S3 bucket using AWS CLI on Windows, refer to How to Programmatically Assess AWS Management Console and Create Resources from the Command-line using AWS CLI in Windows, Understanding AWS CLI: How to configure Command Line Interface [Part 2], How to add an EBS volume to AWS EC2 via the AWS Console and CLI

Prerequisite

To follow along in this write-up, you need to have the following:

  1. Install AWS CLI on your PC, see a related guide  AWS Command-Line Interface: How to configure AWS CLI [Part 1] 
  2. Active AWS Subscription
  3. Have the IAM Permission to create and manage S3 Bucket

Installing awscli package on a local system

To install the AWS CLI package on your system, run the below commands to update your system packages and then download the AWS CLI.

sudo apt update -y
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
Install-AWS-CLI
Downloading AWS CLI

After downloading the awsscli, run the sudo apt install unzip and unzip awscliv2.zip to you to install unzip and use it to unzip the file.

Install-Unzip-command-
Installing Unzip package and unzipping the awscli file

Now, install the awscli by running:

sudo ./aws/install

Please note the directory where you unzip the file to. Here the file was extracted to the AWS folder.

Install-CLI
Installing AWS CLI

Assign permissions to the user

AWS offers IAM roles and policies to limit user access to various AWS resources. Your user account must have specific permissions before performing any action on AWS. This section will grant the user account the necessary permissions to perform various tasks on S3. To begin, log in to the AWS management console and navigate to the IAM service.

Creating-an-IAM-User
Accessing IAM Portal

While on the Identity and Access Management (IAM) portal, click on User

Adding-an-IAM-User
Adding a User

Specify the username

Specify-the-user-name

Add User to a Group

Add-user-to-group
Adding a User to a group

Assign permission to the user to be able to manage S3 Bucket by clicking on the Add inline policy

Assign-permission
Assigning permission to the User

Search for and select S3 from the list of services

S3-Bucket-2
Attaching the appropriate permissions

Generating and configuring the access key ID and secret access key

You need some credentials to authorize your requests in order to use the AWS command-line interface on your local computer. AWS access key ID and secret access key are used as credentials for the AWS command-line interface. We will create and set up these credentials on our local system in this section.

To generate and configure the access key ID and secret access key, go to the Users section of the IAM console, then click on the username.

Clicking-on-the-user-name
Assessing the User

Click the Create access key button after scrolling down to the Access keys section.

Click-on-Create-access-key
Creating Access Key

Choose how you want to use the Access key

Choose-access-key-best-practices
Choosing the best practice for your access key

Download the CSV file containing the access key ID and secret access Key

Retrieve-the-access-key
Download the Acces key

Use the terminal on your local system to configure the AWS command-line credentials after they have been generated and downloaded. To configure the credentials, use the aws configure command and supply the access key ID and secret access key, set your default region, and default output format.

AWS-Configure-Access-Key
Configuring the AWS Access Key ID and Secret Access Key

We have two buckets on our account.

Listing S3 Bucket Objects with CLI

Let’s list the objects/files in one of the S3 bucket using the below command:

aws s3 ls <bucket-name>
List-bucket-objects
Listing S3 Bucket Objects

There are about six objects in one of the buckets.

Deleting S3 Bucket Object using AWS CLI

To empty all the objects in your S3 Buckets, run:

aws s3 rm s3://<bucket-name> --recursive
Deleting-S3-bucket-objects
Deleting S3 Bucket Objects

Verify that the objects have been deleted by running:

aws s3 ls <bucket-name>
Verify-the-objects-have-been-deleted
Verifying deleted S3 Bucket Objects

Deleting S3 Bucket

Now that we have emptied the S3 Bucket, let’s proceed to delete the entire bucket using the below command:

aws s3 r s3://<bucket-name>
Removing-bucket
Deleting S3 Bucket

In conclusion, AWS CLI is a powerful tool that you can use to centrally control and managed your resources on your AWS environment. In this post, you have learnt how to install and configure AWS CLI package in your Local PC’s terminal and directly interacted with AWS S3 buckets and its objects. You also got to understand how to create an IAM User and assign permission to them and finally learnt how to delete S3 bucket objects and the entire S3 bucket.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x