[PYTHON] Findings when AWS Lambda gives "ELF load command address / offset not properly aligned" error

background

When developing AWS Lambda with Python, you may use the Serverless Framework and the serverless-python-requirements plugin. At that time, I was a little clogged with an error in the title, so I will summarize what was useful for solving it.

Operating environment

This article has been confirmed in the following environment.

Until you get an error

In this article, we will consider the following configuration.

requirements.txt


opencv-python

handler.py


import cv2 

def hello(event, context):
    response = {
        "statusCode": 200,
        "body": cv2.__version__
    }

    return response

serverless.yml


service: test

provider:
  name: aws
  runtime: python3.7

functions:
  hello:
    handler: handler.hello

custom:
  pythonRequirements:
    dockerizePip: true
    slim: true

plugins:
  - serverless-python-requirements

This is just a function that returns the version of OpenCV. If you deploy this and run it with sls invoke -f hello, you will get the following error.

{
    "errorMessage": "Unable to import module 'handler': /var/task/cv2/cv2.cpython-37m-x86_64-linux-gnu.so: ELF load command address/offset not properly aligned",
    "errorType": "Runtime.ImportModuleError"
}

In this article, I will summarize what to try in such situations.

Review the plugin settings

For this ʻELF load command address / offset not properly aligned` error, see the plugin README.md -not-to-strip-binaries)

custom:
  pythonRequirements:
    slim: true
    strip: false

It seems that you should write. So, rewrite serverless.yml and try again. If there is no error here, I think there is no problem. In this article, I will introduce the knowledge when an error occurs here.

Check the cache

serverless-python-requirements has a cache function so that you don't have to pip every time. https://github.com/UnitedIncome/serverless-python-requirements/blob/master/README.md#caching

The previous change to strip: false seems to use the cache, and the changed package will not be reflected. For example, sls remove does not remove the cache, so ʻELF load command address / offset not properly aligned` will continue to appear.

Therefore,

$ rm -rf /Users/xxx/Library/Caches/serverless-python-requirements

It may be solved by deleting the cache and then redeploying as in. (Of course, the path will change depending on the environment.)

Summary

If ELF load command address / offset not properly aligned occurs

I think it's good. If you have any other knowledge, please let us know.

Recommended Posts

Findings when AWS Lambda gives "ELF load command address / offset not properly aligned" error
When the Linux command $ yum -y install gives an error (yum: command not found)