title: AWS Lambda function Key Error with trigger on S3
body: <p>Basic lambda function trying to get contents of the bucket but getting errors though</p>
  <pre><code>import json
  import urllib.parse
  import boto3
  
  print('Loading function')
  s3 = boto3.client('s3')
  
  
  def lambda_handler(event, context):
      #print(&quot;Received event: &quot; + json.dumps(event, indent=2))
  
      # Get the object from the event and show its content type
      bucket = event['Records'][0]['s3']['bucket']['name']
      key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
      try:
          response = s3.get_object(Bucket=bucket, Key=key)
          print(&quot;CONTENT TYPE: &quot; + response['ContentType'])
          return response['ContentType']
      except Exception as e:
          print(e)
          print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
          raise e
  </code></pre>
  <p>Here is the error message when i run the lambda function.</p>
  <p>Error message</p>
  <pre><code>{
    &quot;errorMessage&quot;: &quot;'Records'&quot;,
    &quot;errorType&quot;: &quot;KeyError&quot;,
    &quot;requestId&quot;: &quot;5c89bb8e-a70e-4c33-ba00-43174095544e&quot;,
    &quot;stackTrace&quot;: [
      &quot;  File \&quot;/var/task/lambda_function.py\&quot;, line 13, in lambda_handler\n    bucket = event['Records'][0]['s3']['bucket']['name']\n&quot;
    ]
  }
  </code></pre>
  <p>Function Logs</p>
  <pre><code>START RequestId: 5c89bb8e-a70e-4c33-ba00-43174095544e Version: $LATEST
  [ERROR] KeyError: 'Records'
  Traceback (most recent call last):
    File &quot;/var/task/lambda_function.py&quot;, line 13, in lambda_handler
      bucket = event['Records'][0]['s3']['bucket']['name']
  END RequestId: 5c89bb8e-a70e-4c33-ba00-43174095544e
  REPORT RequestId: 5c89bb8e-a70e-4c33-ba00-43174095544e  Duration: 1.89 ms   Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 69 MB  Init Duration: 356.28 ms
  </code></pre>