Steps To List S3 Buckets by using boto3
Step 1: First create an IAM role for Lambda service with AmazonS3FullAccess.
Step 2: Then move to Lambda dashboard and select create function . Then create a lambda function using python . Here i attached the role to Lambda function that is created in step 1 .
Step 3: Here i have used boto3 module to get the S3 names and creation date . After writing this code select deploy .
import json
import boto3
client = boto3.client('s3')
def lambda_handler(event, context):
response = client.list_buckets()
s3_buckets_list = response['Buckets']
length = len(s3_buckets_list)
if length!=0:
for i in range(length):
print(s3_buckets_list[i]['Name']," : ",s3_buckets_list[i]['CreationDate'])
else:
print("There are no S3 buckets . ")
Step 4: Configure a demo test event.
Step 5: Now test the lambda ,if your account does not have any S3 buckets then you will see output “There are no S3 buckets .”
Step 6: Create some S3 buckets to test the lambda function .
Step 7: Now test the lambda function . This time lambda function lists all the S3 buckets with their creation time .
How To List S3 Buckets With Boto3 ?
Amazon S3 and Amazon Lambda are two crucial AWS services used in different organizations across the industry. Amazon S3 is used to store and manage data while Amazon Lambda provides serverless computing service to run code without any management of the server. In this guide, I will first discuss briefly Amazon S3 and Amazon Lambda. Then i will guide you through the different steps to list all the S3 buckets using the boto3 module on Amazon Lambda.
Contact Us