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?

已提问 4 个月前267 查看次数
2 回答
1
已接受的回答

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
已回答 4 个月前
  • 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
专家
已回答 4 个月前
  • 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

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则