AWS Lambda Test KeyError (Python 3.9)

Asked 2 years ago, Updated 2 years ago, 432 views

I am a beginner at lambda and python.
Executed the following code:

import boto3
import json
from collections import OrderedDict

def handler(event, context):
    # Define required constants
    BUCKET_NAME=event ['bucket_name']
    BUCKET_KEY=event ['bucket_key']
    UPLOAD_BUCKET_KEY = 'b.json'
    
    # a.json loading
    s3 = boto3.client('s3')
    obj=s3.get_object(Bucket=BUCKET_NAME, key=BUCKET_KEY)
    data=json.loads(obj['Body'].read().decode('utf-8')))
    print(f'{BUCKET_KEY} content: {data}')
    
    # b. Uploading sjon
    s3 = boto3.resource('s3')
    obj=s3.Bucket(BUCKET_NAME).Object(UPLOAD_BUCKET_KEY)
    data=OrderDict(file_name=UPLOAD_BUCKET_KEY, author=data['author'], age=(data['age']+1))
    
    res=obj.put(Body=json.dumps(data))
    ifres ['ResponseMetadata'] ['HTTPSstatusCode'] == 200:
        print(f'[SUCCESS] upload {UPLOAD_BUCKET_KEY}')
    
    

Tested on AWS Lambda.
As a result, the following error occurred:

"errorMessage": "'bucket_name',
  "errorType": "KeyError",
  "requestId": "752f59f8-f95a-4903-8412-60334115be69",
  "stackTrace": [
    "  File\"/var/task/main.py\", line7, in handler\nBUCKET_NAME=event['bucket_name']\n"
  ]
}

The lambda test event was described as follows:

{
  "BUCKET_NAME": "bucket_name",
  "BUCKET_KEY": "bucket_key"
}

The handler is set to main.handler and specifies to call a function of this code.
I'm having trouble knowing the cause of the error.

We fixed it in the AWS lambda test event.

{
  "bucket_name": "lambda-test-keepin",
  "bucket_key": "a.json"
}

The error changed when I ran the test again.

VersionId, SSECcustomerAlgorism, SSECcustomerKey, SSECcustomerKeyMD5, RequestPayer, PartNumber, ExpectedBucketOwner",
  "errorType": "ParamValidationError",
  "requestId": "ed79a6d9-2510-42fe-8105-d43732124bb2",
  "stackTrace": [
    "  File\"/var/task/main.py\", line13, in handler\nobj=s3.get_object(Bucket=BUCKET_NAME, key=BUCKET_KEY)\n",
    "  File\"/var/runtime/botocore/client.py\", line386, in_api_call\nreturn self._make_api_call(operation_name, kwargs)\n",
    "  File\"/var/runtime/botocore/client.py\", line677, in_make_api_call\nrequest_dict=self._convert_to_request_dict(\n",
    "  File\"/var/runtime/botocore/client.py\", line725, in_convert_to_request_dict\nrequest_dict=self._serialize_to_request(\n",
    "  File\"/var/runtime/botocore/validate.py\", line 337, serialize_to_request\nraiseParamValidationError(report=report.generate_report())\n"
  ]

}

python python3 aws-lambda

2022-09-30 22:02

1 Answers

I haven't written much in Python, but

{
  "BUCKET_NAME": "bucket_name",
  "BUCKET_KEY": "bucket_key"
}

The key in the dictionary is "BUCKET_NAME", "BUCKET_KEY", so if you want to refer to the value, you can use

#Define required constants
    BUCKET_NAME=event ['BUCKET_NAME']
    BUCKET_KEY=event ['BUCKET_KEY']

Isn't that right?

Python Lambda Function Handler - AWS Lambda


2022-09-30 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.