Create Lambda in account 1

  1. Open the Lambda console

  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):
              # Create an S3 client
              s3 = boto3.client('s3')
              # Call S3 to list current buckets
              objlist = s3.list_objects(
                              MaxKeys = 10)
              print (objlist['Contents'])
              return str(objlist['Contents'])
          except Exception as 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