Home > database >  Error when testing Python lambda in AWS console
Error when testing Python lambda in AWS console

Time:09-24

I'm trying to implment a lambda using python. I'm getting the error below when testing it in the aws console.

lambda_handler

from datetime import datetime
import json
# import os
import boto3

DATA_ENGINEERING_BUCKET = "kinesis-archive"
RATE_PATH = "archive/rates"
s3_client = boto3.client('s3')

def lambda_handler(event, context):
    for record in event["Records"]:
        print(f"Event: {record['eventName']}/{record['eventID']}")
        table_arn, _ = record["eventSourceARN"].split("/stream")
        if record["PK"].startswith('CLASS'):
            key = '#'.join([record['PK'], record['SK']])
            s3_client.put_object(
                Body = json.dumps(record),
                Bucket = DATA_ENGINEERING_BUCKET,
                Key= '/'.join([RATE_PATH, key])
            )
    return { "statusCode": 200, "body": "OK" }

test input

{
  "Records": [
    {
      "kinesis": {
        "partitionKey": "partitionKey-03",
        "kinesisSchemaVersion": "1.0",
        "data": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0IDEyMy4=",
        "sequenceNumber": "49545115243490985018280067714973144582180062593244200961",
        "approximateArrivalTimestamp": 1428537600
      },
      "eventSource": "aws:kinesis",
      "eventID": "shardId-000000000000:49545115243490985018280067714973144582180062593244200961",
      "invokeIdentityArn": "arn:aws:iam::EXAMPLE",
      "eventVersion": "1.0",
      "eventName": "aws:kinesis:record",
      "eventSourceARN": "arn:aws:kinesis:EXAMPLE",
      "awsRegion": "us-east-1",
      "PK": "CLASS#CA",
      "SK": "1"
    }
  ]
}

error

Response
{
  "errorMessage": "not enough values to unpack (expected 2, got 1)",
  "errorType": "ValueError",
  "requestId": "d998cbd1-d537-4f24-8e30-65f7c350cf07",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 17, in lambda_handler\n    table_arn, _ = record[\"eventSourceARN\"].split(\"/stream\")\n"
  ]
}

Function Logs
START RequestId: d998cbd1-d537-4f24-8e30-65f7c350cf07 Version: $LATEST
Event: aws:kinesis:record/shardId-000000000000:49545115243490985018280067714973144582180062593244200961
[ERROR] ValueError: not enough values to unpack (expected 2, got 1)
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 17, in lambda_handler
    table_arn, _ = record["eventSourceARN"].split("/stream")END RequestId: d998cbd1-d537-4f24-8e30-65f7c350cf07
REPORT RequestId: d998cbd1-d537-4f24-8e30-65f7c350cf07  Duration: 1.41 ms   Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 67 MB  
XRAY TraceId: 1-614ccd64-284f0f0f425eb6004bb5e1d0   SegmentId: 1fbd881072d3ec61 Sampled: true

Request ID
d998cbd1-d537-4f24-8e30-65f7c350cf07

CodePudding user response:

The error is below

d = {"eventSourceARN": "arn:aws:kinesis:EXAMPLE"}
table_arn, _ = d["eventSourceARN"].split("/stream")

Error:

ValueError: not enough values to unpack (expected 2, got 1)

It looks like you assume that the split will return 2 elements. But it is not true.

the solution is not to assume how many elements to return - check in runtime how many elements you have in the list that is returned by split

  • Related