Legacy Dev Forum Posts

 View Only

Sign Up

Lambda function with python script - webchat

  • 1.  Lambda function with python script - webchat

    Posted 06-05-2025 19:34

    ayoub | 2023-06-21 13:27:52 UTC | #1

    basing on the project deploy web chat blue print I'm trying to change the main.go for the lambda function script to main.py script :

    import json
    
    class OrderStatusRequest:
        def __init__(self, OrderNumber):
            self.OrderNumber = OrderNumber
    
    class OrderStatusResponse:
        def __init__(self, CustomerId, CustomerFirstName, CustomerLastName, OrderNumber, OrderStatus):
            self.CustomerId = CustomerId
            self.CustomerFirstName = CustomerFirstName
            self.CustomerLastName = CustomerLastName
            self.OrderNumber = OrderNumber
            self.OrderStatus = OrderStatus
    
    class OrderStatusResponseEncoder(json.JSONEncoder):
        def default(self, obj):
            if isinstance(obj, OrderStatusResponse):
                return {
                    "CustomerId": obj.CustomerId,
                    "CustomerFirstName": obj.CustomerFirstName,
                    "CustomerLastName": obj.CustomerLastName,
                    "OrderNumber": obj.OrderNumber,
                    "OrderStatus": obj.OrderStatus
                }
            return super().default(obj)
    
    def lambda_handler():
        response = OrderStatusResponse(
            CustomerId="1214-22442-1888",
            CustomerFirstName="Roberties",
            CustomerLastName="Berhneyrt",
            OrderNumber="12345567",
            OrderStatus="Shipped"
        )
        return json.dumps(response, cls=OrderStatusResponseEncoder)

    I changed the lambda.tf to this :

    resource "aws_lambda_function" "lambda_function" {
      function_name    = "${local.resource_name_prefix}-lambda"
      description      = "Lambda to be consumed by Genesys Cloud Architect Flow"
      filename         = var.lambda_zip_file
      handler          = "main.lambda_handler"
      source_code_hash = var.lambda_source_code_hash
      role             = aws_iam_role.lambda_execution_role.arn
      runtime          = local.go_runtime
      timeout          = local.lambda_timeout
    }

    But I got this error : { "errorMessage": "Unable to import module 'main': No module named 'main'", "errorType": "Runtime.ImportModuleError", "stackTrace": [] }

    NB: with golang every thing is going on well


    John_Carnell | 2023-06-22 15:30:01 UTC | #2

    Hi Ayoub,

    Two things:

    1. With the python Lambda I believe the handler attribute should be "lambdahandler" and not "main.lambdahandler". "main" is the package name for the Golang Lambda.
    2. Are you looking at deploying a WebChat solution? WebChat is our original chat product and while we still support it, no further R & D work is being performed on it. The go-forward solution is to use Web Messaging. We have a web messaging blueprint with the same flows and code, but just using WebMessaging as a solution.

    Thanks, John Carnell Director, Developer Engagement


    ayoub | 2023-06-23 08:11:50 UTC | #3

    thanks for responding; for the first one, changing the "main.lambdahandler" to "lambdahandler" returns me this error: { "errorMessage": "Bad handler 'main': not enough values to unpack (expected 2, got 1)", "errorType": "Runtime.MalformedHandlerName", "requestId": ".................", "stackTrace": [] }


    tim.smith | 2023-06-23 13:43:24 UTC | #4

    For more information about how to write an AWS Lambda function using Python, please refer to the AWS documentation: https://docs.aws.amazon.com/lambda/latest/dg/lambda-python.html.


    ayoub | 2023-06-23 15:02:33 UTC | #5

    thanks for responding; my problem is not about creating or dealing with lambda function, I'm familiar with it. the problem I'm facing is the one I have explained in the post. Thanks


    tim.smith | 2023-06-23 15:06:19 UTC | #6

    My apologies, I assumed that error was from AWS when failing to run your lambda function. Can you provide some context about which part of your code is throwing that error?


    ayoub | 2023-06-23 15:30:59 UTC | #7

    Sure; the prblem mainly in the lambda.tf file; It can not read the lambda function and always return me this error : { "errorMessage": "Bad handler 'main': not enough values to unpack (expected 2, got 1)", "errorType": "Runtime.MalformedHandlerName", "requestId": ".................", "stackTrace": [] } the file that contains the lambda function is main.tf so I tried to replace script to this :

    resource "aws_lambda_function" "lambda_function" {
      function_name    = "${local.resource_name_prefix}-lambda"
      description      = "Lambda to be consumed by Genesys Cloud Architect Flow"
      filename         = var.lambda_zip_file
      handler          = "main.lambda_handler"
      source_code_hash = var.lambda_source_code_hash
      role             = aws_iam_role.lambda_execution_role.arn
      runtime          = local.go_runtime
      timeout          = local.lambda_timeout
    }

    then I got this error : { "errorMessage": "Unable to import module 'main': No module named 'main'", "errorType": "Runtime.ImportModuleError", "stackTrace": [] }


    John_Carnell | 2023-07-03 18:02:28 UTC | #8

    Hi Ayoub,

    I suggest you take a look at the terraform AWS provider docs. This a problem that really centers more around how the AWS terraform provider is deploying the Lambda. We are using the Terraform AWS provider "out of box", but we wrote our Lambdas in Golang so not sure what the issue might be.

    Thanks, John


    John_Carnell | 2023-07-03 18:02:31 UTC | #9


    This post was migrated from the old Developer Forum.

    ref: 20582