[SOLVED] Serverless+Sagemaker+Lambda

Hi everyone (especially those, who uses a :cocktail: of HuggingFace+Sagemaker+ServerlessFramework+Lambda).

Since yesterday I tried 20+ options to properly import Sagemaker module to use the HuggingFacePredictor within the Lambda function, but no success. This is why I am asking for help, because I couldn’t find the correct way to make it work (even on serverless.com forums).

Here is what I am trying to do within Lambda:

try:
     print('attempting to unzip_requirements...')
     import unzip_requirements  
     print('succesfully imported unzip_requirements to prepare the zipped requirements')
except ImportError:
     print('failed to import unzip_requirements - if you are running locally this is not a problem')
     pass
...
import sagemaker
from sagemaker.huggingface import HuggingFacePredictor
predictor = HuggingFacePredictor(ENDPOINT_NAME)

Here is the log:

attempting to unzip_requirements...
succesfully imported unzip_requirements to prepare the zipped requirements
...

[ERROR] PackageNotFoundError: No package metadata was found for sagemaker
Traceback (most recent call last):
  File "/var/task/serverless_sdk/__init__.py", line 144, in wrapped_handler
    return user_handler(event, context)
  File "/var/task/handler.py", line 184, in ProductMapper
    import sagemaker
  File "/tmp/sls-py-req/sagemaker/__init__.py", line 18, in <module>
    from sagemaker import estimator, parameter, tuner  # noqa: F401
  File "/tmp/sls-py-req/sagemaker/estimator.py", line 28, in <module>
    from sagemaker import git_utils, image_uris
  File "/tmp/sls-py-req/sagemaker/image_uris.py", line 22, in <module>
    from sagemaker.spark import defaults
  File "/tmp/sls-py-req/sagemaker/spark/__init__.py", line 16, in <module>
    from sagemaker.spark.processing import PySparkProcessor, SparkJarProcessor  # noqa: F401
  File "/tmp/sls-py-req/sagemaker/spark/processing.py", line 35, in <module>
    from sagemaker.local.image import _ecr_login_if_needed, _pull_image
  File "/tmp/sls-py-req/sagemaker/local/__init__.py", line 16, in <module>
    from .local_session import (  # noqa: F401
  File "/tmp/sls-py-req/sagemaker/local/local_session.py", line 23, in <module>
    from sagemaker.local.image import _SageMakerContainer
  File "/tmp/sls-py-req/sagemaker/local/image.py", line 39, in <module>
    import sagemaker.local.data
  File "/tmp/sls-py-req/sagemaker/local/data.py", line 27, in <module>
    import sagemaker.local.utils
  File "/tmp/sls-py-req/sagemaker/local/utils.py", line 22, in <module>
    from sagemaker import s3
  File "/tmp/sls-py-req/sagemaker/s3.py", line 20, in <module>
    from sagemaker.session import Session
  File "/tmp/sls-py-req/sagemaker/session.py", line 37, in <module>
    from sagemaker.user_agent import prepend_user_agent
  File "/tmp/sls-py-req/sagemaker/user_agent.py", line 21, in <module>
    SDK_VERSION = importlib_metadata.version("sagemaker")
  File "/tmp/sls-py-req/importlib_metadata/__init__.py", line 994, in version
    return distribution(distribution_name).version
  File "/tmp/sls-py-req/importlib_metadata/__init__.py", line 967, in distribution
    return Distribution.from_name(distribution_name)
  File "/tmp/sls-py-req/importlib_metadata/__init__.py", line 561, in from_name
    raise PackageNotFoundError(name)

Here is serverless.yml:

org: **********
app: rec*************-app
service: re********service

frameworkVersion: '2'

custom:
  bucket: re********bucket
  pythonRequirements:
    dockerizePip: false
    invalidateCaches: true
    zip: true
    slim: true
    strip: false
    noDeploy:
      - docutils
      - jmespath
      - pip
      - python-dateutil
      - setuptools
      - six
      - tensorboard
    useStaticCache: false
    useDownloadCache: false

package:
  include:
    - requirements.txt
  exclude:
    - aws-deployment*
    - .dockerignore
    - Dockerfile
    - README.md
    - .gitignore
    - venv/**
    - test/**

provider:
  name: aws
  runtime: python3.8
  lambdaHashingVersion: 20201221
  stage: develop-lenovo
  region: eu-central-1
  memorySize: 256
  logRetentionInDays: 30
  iam:
    role:
      statements:
      - Effect: Allow
        Action: s3:*
        Resource: "*"
      - Effect: Allow
        Action: sagemaker:InvokeEndpoint
        Resource: "*"
  s3:
    telegramPuctures:
      name: ***************
      

functions:
  PictureSaver:
    handler: handler.PictureSaver
    layers:
      arn:aws:lambda:eu-central-1:770693421928:layer:Klayers-python38-requests:20
  TableExtractorWithVeryfi: 
    handler: handler.TableExtractor
    timeout: 20 # optional, in seconds, default is 6
    layers:
      arn:aws:lambda:eu-central-1:770693421928:layer:Klayers-python38-requests:20
  ProductMapper: 
    handler: handler.ProductMapper
    timeout: 100 # optional, in seconds, default is 6
    layers:
      arn:aws:lambda:eu-central-1:770693421928:layer:Klayers-python38-requests:20



stepFunctions:
  stateMachines:
    re*********step-function-IaC:
      events:
        - http:
            path: /
            method: POST
      definition:
        StartAt: PictureSaverStep
        States:
          PictureSaverStep:
            Type: Task
            Resource:
              Fn::GetAtt: [PictureSaver, Arn]
            Next: TableExtractorStep
          TableExtractorStep:
            Type: Task
            Resource:
                Fn::GetAtt: [TableExtractorWithVeryfi, Arn]
            Next: ProductMapperStep
          ProductMapperStep:
            Type: Task
            Resource:
              Fn::GetAtt: [ProductMapper, Arn]
            End: true
      tracingConfig:
        enabled: true       
      

plugins:
  - serverless-step-functions
  - serverless-python-requirements

And this is what is inside requirements.txt

requests == 2.22.0
sagemaker == 2.59.1.post0

Can maybe some one give me a hint, what could help? It looks like I’m failing to import requirements completely…

The issue was in this line in serverless.yml:

    slim: true

Changing it to “false” solves the issue, but slightly increases the package size (20% in my case).

    slim: false
1 Like