1

AWS Lambda Python 3.8 runtime says it can execute 32bit libraries. I have a 32 bit executable which was built in C. When I execute the following code - I get:

wrong ELF class: ELFCLASS32

I tried the same on Amazon Linux 2 docker image. The error I get is same. possibly python gives CDLL a 64 bit instance.

Using the 32 bit docker image from ubuntu 32bit/ubuntu:16.04 it works fine.

import platform 
print(platform.architecture())

I will have to invoke Python in 32 bit architecture.

C Code:

#include <stdio.h>

int foo() {
    printf("Hello, world!\n");
    return 0;
}

Python Code:

import ctypes

def lambda_handler(event, context):
    libhi = ctypes.CDLL("my32bitlib.so")
    libhi.foo()

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

lambda_handler(None, None)
user 923227
  • 1,963
  • 3
  • 20
  • 39
  • Did you try this code on Amazon Linux 2 (on EC2) before moving to Lambda/Python3.8? – jarmod Mar 20 '20 at 20:20
  • Yes, I tried it on Amazon Linux 2 docker image - I get the same error! – user 923227 Mar 20 '20 at 20:38
  • 2
    OK, I would investigate it further on EC2 before you move back to Lambda. I'm not sure what, if any, restrictions there are around supported 32-bit binaries and libraries on AL2. – jarmod Mar 20 '20 at 20:41

1 Answers1

0

I'm pretty sure https://aws.amazon.com/about-aws/whats-new/2018/09/amazon-linux-2-now-supports-32-bit-applications-and-libraries/ just means they've installed gcc-multilib and so on so the system has /usr/lib32/*.so available, so you can run 32-bit binary executables. And those 32-bit executables in 32-bit processes can load 32-bit libraries.

They haven't invented any magic that lets you use a 32-bit library from a 64-bit Python process. The CPU will be in 64-bit mode when executing user-space code in that process, and thus it won't be able to correctly execute 32-bit x86 machine code in a 32-bit library.

If they had switched the normal /usr/bin/python over to being a 32-bit executable, that would have broken 64-bit libraries.

I think you're being overly optimistic about what that news announcement means; it's probably not relevant for you because your program is written in Python. You can't mix 32 and 64-bit code and regular python is still a 64-bit program.


They might possibly have installed a 32-bit build of Python that you could use if you want to load legacy 32-bit libraries in a Python program. Maybe look for a python32 directory or binary, if that's what they called it? But don't be surprised if you don't find anything.

If so, change the #!/usr/bin/python line at the start of your script to whatever the right path is. Or install your own 32-bit Python build in /usr/local and use that.

Peter Cordes
  • 245,674
  • 35
  • 423
  • 606
  • What changes do I have to do in my python script so that I am able to run the 32 bit library? – user 923227 Mar 20 '20 at 22:45
  • @user923227: Just the path in the `#!/usr/bin/python` line. Change that to a the path to a 32-bit Python interpreter, if there is one installed. – Peter Cordes Mar 20 '20 at 22:47
  • No. This did not solve the issue. It is getting the same error. It is still invoking the `64 bit` python interpreter, from `platform.architecture()` – user 923227 Mar 22 '20 at 23:54
  • @user923227: Then you picked the wrong path, or your python scripts are being invoked by something that runs `/usr/bin/python` on them without looking at the #! line. (So you should change whatever runs them.) – Peter Cordes Mar 23 '20 at 00:36
  • [Looking for the python path](https://stackoverflow.com/a/6767329/923227) - I found there is only `python3.8` installed! – user 923227 Mar 23 '20 at 20:20
  • @user923227: You might have to install a 32-bit build of Python yourself. It wouldn't surprise me if there is no 32-bit `python` already installed on AWS. – Peter Cordes Mar 24 '20 at 01:18