SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for AWS Amazon Web Services, Redshift, AWS Lambda Functions, S3 Buckets, VPC, EC2, IAM

All AWS S3 Buckets List using Lambda Function with Python


In this AWS tutorial, I want to show how serverless developers can create an AWS Lambda function using Python to list all Amazon S3 bucket names. Besides AWS Lambda codes in Python, the Lambda execution code should have the required permissions attached as a policy to access related resources.

Let's start to build your AWS Lambda function.
Login to AWS Console with your user.
Among Services under Compute section, click Lambda
Press on Create function button

Type a name for your Lambda function.
Choose "Python 3.6" as the Runtime for the Lambda function.
Choose an existing role for the Lambda function we started to build.
I had already a Lambda role but I'm not sure if it is 100 % appropriate for this function.

Copy and paste following Python code into the Lambda function code inline editor.

import json
import boto3

s3 = boto3.resource('s3')

def lambda_handler(event, context):

 bucketlist = []

 for bucket in s3.buckets.all():
  bucketlist.append(bucket.name)

 return {
  "statusCode": 200,
  "body": bucketlist
 }
Code

Click Save button then you are ready to test the Lambda function.
Press Test button

If you got following error message Syntax error in module 'lambda function name': expected an indented block, it is easy to resolve the problem.
Python code requires correct indents for successfull execution.
If you add missing indents in your code or remove extra indents, the code will not produce syntax error.

If you get following error message, then it is related with roles and permissions missing policy to access all S3 buckets list.

An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denied: ClientError

The Python code "s3.buckets.all()" causes above access denied error message because the Lambda execution role does not have the required access policy.

Check execution role of the lambda function

Then go to Services > IAM (Identity and Access Management)
Click on Roles from left menu and choose the related role

On Permissions tab, click "Add inline policy" link button

Using "Visual editor" start with Service selection
Click "Choose a service" text button

Type "S3" to filter Simple Storage Service S3 and click on filtered S3 service
Then as seen in following screenshot, mark "ListAllMyBuckets" checkbox

Since I want to list all S3 buckets, this ListAllMyBuckets action allows to access to "All resources"

Click "Review policy" button type a name then "Create policy"
Now on the AWS role you will see the new policy added among Permissions policies

Now, you can switch to your Lambda function.
If you are already in the Lambda function exit from service and reopen it.
Then I hope, it will be possible to execute Lambda function successfully

The response of the Lambda function request will include the names of all S3 buckets in your S3 service dashboard.



AWS


Copyright © 2004 - 2024 Eralper YILMAZ. All rights reserved.