title: Why do I get a KeyError when testing an S3 triggered Lambda that invalidates files in CloudFront?
body: <p>I have a Python 3.7 Lambda function that invalidates CloudFront cache:</p>
  <pre class="lang-py prettyprint-override"><code>from __future__ import print_function
  
  import boto3
  import time
  
  def lambda_handler(event, context):
      print(event)
      for items in event[&quot;Records&quot;]:
          path = &quot;/&quot; + items[&quot;s3&quot;][&quot;object&quot;][&quot;key&quot;]
      print(path)
      client = boto3.client('cloudfront')
      invalidation = client.create_invalidation(DistributionId='distributionid__ID',
          InvalidationBatch={
              'Paths': {
                  'Quantity': 1,
                  'Items': [path]
          },
          'CallerReference': str(time.time())
      })
  </code></pre>
  <p>This is the attached IAM policy:</p>
  <pre><code>{
      &quot;Version&quot;: &quot;2012-10-17&quot;,
      &quot;Statement&quot;: [
          {
              &quot;Effect&quot;: &quot;Allow&quot;,
              &quot;Action&quot;: [
                  &quot;logs:CreateLogGroup&quot;,
                  &quot;logs:CreateLogStream&quot;,
                  &quot;logs:PutLogEvents&quot;
              ],
              &quot;Resource&quot;: &quot;arn:aws:logs:*:*:*&quot;
          },
          {
              &quot;Effect&quot;: &quot;Allow&quot;,
              &quot;Action&quot;: [
                  &quot;cloudfront:CreateInvalidation&quot;
              ],
              &quot;Resource&quot;: [
                  &quot;*&quot;
              ]
          }
      ]
  }
  </code></pre>
  <p>This is my test JSON:</p>
  <pre><code>{
    &quot;key1&quot;: &quot;*&quot;
  }
  </code></pre>
  <p>Whenever I invoke my Lambda function, I get this error:</p>
  <pre><code>{'key 1': '*'}
  [ERROR] KeyError: 'Records'
  Traceback (most recent call last):
  Version: $LATEST
      File &quot;/var/task/lambda_function.py&quot;, line 8, in lambda_handler
          for items in event[&quot;Records&quot;]:
  </code></pre>