AWS Managed Lambda Layer for panda seems to fail in Cloudformation

0

Hi, I have used the following YML in a cloudformation resource

  S3LambdaLoadFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket: !Ref lambdaFunctionRepo
        S3Key: !Ref lambdaS3ObjectName
      Description: Lambda function stored in S3 bucket
      Handler: index.lambda_handler
      Role: !GetAtt LambdaIAMLoadRole.Arn
      Environment:
        Variables:
          region: !Ref Region
          table: !Ref DynamoDBTable     
          s3bucket: !Ref NotificationBucket 
      MemorySize: 1024
      Runtime: python3.11
      Layers:
	- arn:aws:lambda:ap-northeast-2:336392948345:layer:AWSSDKPandas-Python311:4

I have used this layer for python pandas succesfully with this python version before (in a stack which I configured manually via the AWS console).

Now, when I try this the non-manual way, using a cloudformation yml template, the cloudformation run finishes succesfully and it also does what it is intended to do - which is to create a lambda function from the code stored as a zip in s3.

However, when I next try to run that lambda thus created, the lambda function fails immediately with

"[ERROR] Runtime.ImportModuleError: Unable to import module 'index': No module named 'index'"

I can share the full lambda function code if you want me to, but only the first part seems relevant for this message, which is to show that the reference to 'index' is not coming from my own code, but instead refers to the failing of the packaging of my code and its lambda layer dependency:

import json
import calendar
import time
import urllib.parse
import boto3
import pandas as pd
import io

aws_session = boto3.Session()
env_region = process.env.region
client = aws_session.client('s3', region_name=env_region)
#s3 = boto3.client('s3')
    
def lambda_handler(event, context):

Am I making a mistake? How to debug this error?

asked 2 months ago159 views
2 Answers
1
Accepted Answer

The error message you shared is talking about the module named index this is unlikely to be related to the layer you are specifying. The Handler you have specified is index.lambda_handlercan you double check that the code you have put in thatr S3 location does indeed have an index.py in the root of the zip file? You would get that error message if either this file does not exist in that zip OR the zip file does not contain index.py at it's root.

profile pictureAWS
danjhd
answered 2 months ago
  • Thanks Danj. Indeed I figured out meanwhile what the problem was. I used the wrong function handler in the cloudformation template. Instead of the convention required for python I took the one for nodejs.

0

Hi,

I think that the issue come from the fact that you don't specifcy "Handler" in your CFN def.

Doc at https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-handler says that it is required to specify handler when deploy from a zip.

Handler: the name of the method within your code that Lambda calls to run your 
function. Handler is required if the deployment package is a .zip file archive. The 
format includes the file name. It can also include namespaces and other qualifiers, 
depending on the runtime

Best,

Didier

profile pictureAWS
EXPERT
answered 2 months ago
  • Hi, thanks for a quick reaction. Unfortunately I don't understand your answer. My resource contains "Handler: index.lambda_handler". I have added markdown to make my question better readable. Also, I do not use a zip file. The pandas layer is an AWS managed layer which is available via Arn, ref. https://aws-sdk-pandas.readthedocs.io/en/stable/layers.html. If this turns out to be a bug, what is the best way to raise it? Via this forum or is another route preferred?

  • I've used this lambda layer: "arn:aws:lambda:eu-west-1:336392948345:layer:AWSSDKPandas-Python38:12", try & see if this solves your error

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions