tl;dl

Use custom docker image.

Error on sls deploy

When I used pyquery and lxml through serverless-python-requirements, I got error on sls deploy

$ sls deploy

Serverless: Installing required Python packages with python3.6...
Serverless: Docker Image: lambci/lambda:build-python3.6

  Error --------------------------------------------------

  Command "/var/lang/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-i6bvdeoo/lxml/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" --no-user-cfg install --record /tmp/pip-kscbnc2r-record/install-record.txt --single-version-externally-managed --compile --home=/tmp/tmpslfe9grl" failed with error code 1 in /tmp/pip-build-i6bvdeoo/lxml/
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.


     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Stack Trace --------------------------------------------

Error: Command "/var/lang/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-i6bvdeoo/lxml/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" --no-user-cfg install --record /tmp/pip-kscbnc2r-record/install-record.txt --single-version-externally-managed --compile --home=/tmp/tmpslfe9grl" failed with error code 1 in /tmp/pip-build-i6bvdeoo/lxml/
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

    at ServerlessPythonRequirements.installRequirements (/Users/user/code/node_modules/serverless-python-requirements/lib/pip.js:106:11)
From previous event:
    at PluginManager.invoke (/Users/user/.config/yarn/global/node_modules/serverless/lib/classes/PluginManager.js:372:22)
    at PluginManager.spawn (/Users/user/.config/yarn/global/node_modules/serverless/lib/classes/PluginManager.js:390:17)
    at Deploy.BbPromise.bind.then.then (/Users/user/.config/yarn/global/node_modules/serverless/lib/plugins/deploy/deploy.js:123:50)
From previous event:
    at Object.before:deploy:deploy [as hook] (/Users/user/.config/yarn/global/node_modules/serverless/lib/plugins/deploy/deploy.js:113:10)
    at BbPromise.reduce (/Users/user/.config/yarn/global/node_modules/serverless/lib/classes/PluginManager.js:372:55)
From previous event:
    at PluginManager.invoke (/Users/user/.config/yarn/global/node_modules/serverless/lib/classes/PluginManager.js:372:22)
    at PluginManager.run (/Users/user/.config/yarn/global/node_modules/serverless/lib/classes/PluginManager.js:403:17)
    at variables.populateService.then (/Users/user/.config/yarn/global/node_modules/serverless/lib/Serverless.js:102:33)
    at runCallback (timers.js:781:20)
    at tryOnImmediate (timers.js:743:5)
    at processImmediate [as _immediateCallback] (timers.js:714:5)
From previous event:
    at Serverless.run (/Users/user/.config/yarn/global/node_modules/serverless/lib/Serverless.js:89:74)
    at serverless.init.then (/Users/user/.config/yarn/global/node_modules/serverless/bin/serverless:42:50)
    at <anonymous>

(。ŏ﹏ŏ)

Solution

Use custom docker image.

  • 1: Comment out lxml on requirements.txt
cat requirements.txt

# [comment out lxml]
# lxml==3.5.0
pyquery==1.2.10
  • 2: Create Dockerfile with lxml
cat Dockerfile

FROM lambci/lambda:build-python3.6

RUN pip install --upgrade pip
RUN pip install lxml
  • 3: Modify serverless.yml to use the Dockerfile
cat serverless.yml

custom:
  pythonRequirements:
    dockerizePip: true
    # [Add dockerFile]
    dockerFile: ./Dockerfile

plugins:
  - serverless-python-requirements

Then, I could sls deploy without pip errors.
(*°∀°)=3

One thought on “[Serverless] How to use Python3 lxml on AWS Lambda

  1. For some reason is not working for me.

    Unable to import module ‘handler’: No module named lxml.html

    I did exactly what you do.

Leave a Reply

Your email address will not be published. Required fields are marked *