Create Lambda in account 1

  1. Open the Lambda console https://console.aws.amazon.com/lambda

  2. Click Create a function

  3. Accept the default Author from scratch

  4. Enter function name as Lambda-List-S3

  5. Select Python 3.7 runtime

  6. Expand Permissions, click Use an existing role, then select the Lambda-List-S3-Role

  7. Click Create function

  8. 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
      
  9. Click Save.

  10. Click Test, accept the default event template, enter an event name for the test, then click Create

  11. 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