Lambda function with python script - webchat

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

Hi Ayoub,

Two things:

  1. With the python Lambda I believe the handler attribute should be "lambda_handler" and not "main.lambda_handler". "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

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

For more information about how to write an AWS Lambda function using Python, please refer to the AWS documentation: Building Lambda functions with Python - AWS Lambda.

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

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?

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": []
}

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