Home > front end >  Lambda raise a syntax error on a match operation
Lambda raise a syntax error on a match operation

Time:04-17

I'm trying to create a Lambda function that works on my machine, but when I test it I receive the following error:

Response
{
  "errorMessage": "Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 64)",
  "errorType": "Runtime.UserCodeSyntaxError",
  "requestId": "ae5f2481-a836-4b4e-8c8d-a82ccd12c630",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\" Line 64\n        match operation:\n"
  ]
}

Function Logs
START RequestId: ae5f2481-a836-4b4e-8c8d-a82ccd12c630 Version: $LATEST
[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 64)
Traceback (most recent call last):
  File "/var/task/lambda_function.py" Line 64
        match operation:END RequestId: ae5f2481-a836-4b4e-8c8d-a82ccd12c630
REPORT RequestId: ae5f2481-a836-4b4e-8c8d-a82ccd12c630  Duration: 1.34 ms   Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 35 MB  Init Duration: 117.69 ms

Request ID
ae5f2481-a836-4b4e-8c8d-a82ccd12c630

So after hours wasted:

  • I cannot see any syntax error.
  • I've tried bundling the enum package (don't know if it is necessary).
  • I've also commented any line that uses external parameters.
  • I'm using Python 3.9 in Lambda and python 3.10 in local.
  • The the logs point always to line 64 match operation: in the function get_url

This is the code until the function with the error:

import requests
import json
from enum import Enum
import urllib.parse
import boto3
import base64

max_number_of_digit = 1
base_url = "https://somesite.io"
app_version = "version-test/"
bubble_api_token = "some api token"
pdf_creator_api_token = 'some other api token'


class APP_ENTITY(str, Enum):
    PROJECT = "project"
    COMPANY = "company"
    PAYMENT = "payment"
    TRANCHE = "tranche"
    COST_ITEMS = "payment"
    DOCUMENT = "document"


class OPERATIONS(int, Enum):
    CREATE = 1
    DELETE = 2
    PATCH = 3
    READ = 4
    GET = 5


document = dict()
customer = dict()
base_assets = [
    'bank.png',
    'calculator.png',
    'card.png',
    'caret.png',
    'jetbrains-mono-v11-latin-500.woff2',
    'jetbrains-mono-v11-latin-regular.woff2',
    'line.png',
    'logo.svg',
    'poppins-v19-latin-300.woff2',
    'poppins-v19-latin-500.woff2',
    'poppins-v19-latin-600.woff2',
    'poppins-v19-latin-regular.woff2',
    'style.css'
]


def set_app_version(event):
    global app_version
    result = "version-test/"
    try:
        result = event['queryStringParameters']['appversion']
        result = "" if (result.lower() != 'test') else "version-test/"
    except:
        result = "version-test/"
    app_version = result
    return result


def get_url(operation: OPERATIONS, entity: APP_ENTITY, cursor: int = 0, record_id: str = "", project_id: str = ""):
    match operation:
        case OPERATIONS.CREATE:
            result = base_url   \
                "/{}api/1.1/obj/{}/bulk".format(app_version, entity)
        case OPERATIONS.DELETE:
            result = base_url   \
                "/{}api/1.1/obj/{}/".format(app_version, entity)
        case OPERATIONS.PATCH:
            result = base_url   \
                "/{}api/1.1/obj/{}/{}".format(app_version, entity, record_id)
        case OPERATIONS.READ:
            match entity:
                case APP_ENTITY.COST_ITEMS:
                    result = base_url   \
                        "/{}api/1.1/obj/{}/?api_token={}&cursor={}&constraints=".format(
                            app_version, entity, bubble_api_token, cursor)
                    constraints = [
                        {
                            "key": "type",
                            "constraint_type": "equals",
                            "value": "development"
                        },
                        {
                            "key": "project",
                            "constraint_type": "equals",
                            "value": project_id
                        }]
                    constraints = urllib.parse.quote_plus(
                        json.dumps(constraints))
                    result = result constraints
        case OPERATIONS.GET:
            result = base_url   \
                "/{}api/1.1/obj/{}/{}".format(app_version, entity, record_id)
    return result

Below a screenshot of the lambda console. Lambda console

And here a screenshot of the error: Lambda error

CodePudding user response:

match and case are new additions in Python 3.10. Lambda does not support Python 3.10, thus you are getting the error. You have to port back your code to work with Python 3.9

  • Related