“KeyError: 'Records'” in AWS S3 - Lambda trigger

前端 未结 1 1364
挽巷
挽巷 2020-12-19 03:09

I have the following lambda function code for simply printing out the Author and metadata of an uploaded event of an S3 bucket:

from __future__ import print_         


        
相关标签:
1条回答
  • 2020-12-19 03:42

    bit late to the party. But here is my first post!

    EXPLANATION:

    When you test in lambda panel -> def lambda_handler(event, context) <- event is injected directly.

    However in AWS API its neccessary to add Mapping Template or otherwise -> event <- is empty, thus resulting in quizzing:

    "errorType": "KeyError", "errorMessage": "'Records'"

    this is null pointer. Records doesnt exist, since -> event <- doesnt exist.

    SOLUTION:

    You need to configure Integration Request inside AWS API. Click on Body Mapping Templates. Then add Mapping Template Set content type to application/json Then edit generated mapping template:

    {
      "body" : $input.json('$'),
      "headers": {
        #foreach($header in $input.params().header.keySet())
        "$header": "$util.escapeJavaScript($input.params().header.get($header))" #if($foreach.hasNext),#end
    
        #end
      },
      "method": "$context.httpMethod",
      "params": {
        #foreach($param in $input.params().path.keySet())
        "$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end
    
        #end
      },
      "query": {
        #foreach($queryParam in $input.params().querystring.keySet())
        "$queryParam": "$util.escapeJavaScript($input.params().querystring.get($queryParam))" #if($foreach.hasNext),#end
    
        #end
      }  
    }

    And edit Lambda function:

    replace:

    for record in event['Records']:

    with:

    for record in event['query']['Records']

    don't know whether stack will ping you with this answer - so i call you @Dawny33 @KevinOelen @franklinsijo

    As for explanation i figured it on my own. However "mapping template" comes from https://medium.com/simple-thoughts-amplified/passing-variables-from-aws-api-gateway-to-lambda-3c5d8602081b

    0 讨论(0)
提交回复
热议问题