Open the Lambda console https://console.aws.amazon.com/lambda
Click Create a function
Accept the default Author from scratch
Enter function name as Lambda-List-S3
Select Python 3.7 runtime
Expand Permissions, click Use an existing role, then select the Lambda-List-S3-Role
Click Create function
Replace the example function code with the following
Replace bucketname with the S3 bucket name from account 2
import json
import boto3
import os
import uuid
def lambda_handler(event, context):
try:
# Create an S3 client
s3 = boto3.client('s3')
# Call S3 to list current buckets
objlist = s3.list_objects(
Bucket='bucketname',
MaxKeys = 10)
print (objlist['Contents'])
return str(objlist['Contents'])
except Exception as e:
print(e)
raise e
Click Save.
Click Test, accept the default event template, enter an event name for the test, then click Create
Click Test again, and in a few seconds the function output should highlight green and you can expand the detail to see the response from the S3 API