Listing the objects in a S3 bucket in a Lambda Python function

0

I have many S3 issues, that is, accessing the S3 area via a Python Lambda function. Let me do something simple: List what objects are in a bucket.

From a new IAM account, newly created: S3 area:

  • A bucket
  • 2 objects (folders) in it
  • One of them has 2 folders in it

The newly Lambda Python (from the new IAM account) is below. Original core from https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html

import json import boto3

session = boto3.Session( aws_access_key_id='ACCESS-KEY-HERE', aws_secret_access_key='SECRET-ACCESS-KEY')

def lambda_handler(event, context):

s3 = boto3.resource('s3')
bucket = s3.Bucket('dayones22')
for obj in bucket.objects.all():
    print(obj.key)
    
return {
    'statusCode': 200,
    'body': json.dumps('Hello from Lambda!')
}

IT NEVER EXECUTES. No coding errors are reported. Here’s what is…. EXECUTION RESULTS: Test Event Name TestPrintS3Tree67

Response { "errorMessage": "2024-02-26T17:14:30.738Z 2ddd5621-075e-4d9d-8772-34f1305908d9 Task timed out after 3.01 seconds" }

Function Logs START RequestId: 2ddd5621-075e-4d9d-8772-34f1305908d9 Version: $LATEST 2024-02-26T17:14:30.738Z 2ddd5621-075e-4d9d-8772-34f1305908d9 Task timed out after 3.01 seconds

END RequestId: 2ddd5621-075e-4d9d-8772-34f1305908d9 REPORT RequestId: 2ddd5621-075e-4d9d-8772-34f1305908d9 Duration: 3011.07 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 82 MB Init Duration: 316.57 ms

Request ID 2ddd5621-075e-4d9d-8772-34f1305908d9

I have more Access issue, however, the most simply thing to do seems to be listing what is in the bucket.

HELP Please!

  • Not related to the question of listing objects in S3, but when using Lambda, there is no need to specify IAM creds (Access Key and Secret Key). Rather, the Lambda role will be used, and it should have the required permissions, in your case to call S3.

    https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html: AWS_ACCESS_KEY, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN – The access keys obtained from the function's execution role.

Petrus
asked 2 months ago305 views
1 Answer
1
Accepted Answer

It seems like your AWS Lambda function is timing out without completing its task of listing objects in an S3 bucket. This issue can be due to a few different reasons. Let's go through some steps you can take to troubleshoot and hopefully resolve the issue:

  • Set the function timeout to a higher value, like 30 seconds or 1 minute, through the AWS Console or CLI.
  • Ensure the Lambda function's IAM role has s3:ListBucket permission for the S3 bucket. For testing, you can use the AmazonS3ReadOnlyAccess policy but refine permissions for production use.
  • Incorporate additional logging before and after the S3 list operation to identify where the function is getting stuck.
  • Use the AWS CLI to test listing objects in the bucket by assuming the Lambda's IAM role, verifying the role's permissions. You can use the IAM policy simulator.
profile picture
EXPERT
answered 2 months ago
profile picture
EXPERT
reviewed a month ago
profile pictureAWS
EXPERT
reviewed 2 months ago
  • Thank you sir! I increased the time and it worked. This one answer will solve many of my issues! You other items have been documented. I truly appreciate what your effort here. Thank you!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions