DilLip_Chowdary
DilLip_Chowdary

Reputation: 1191

How to add custom libraries and binaries in AWS Lambda?

I have deployed my app with python36 runtime long ago, and now AWS Deprecated this runtime.

So while migrating from python36 to python38 or python39, I observed that AWS removed many in-built libraries in Amazon Linux 2 which is used for Python38 or python39.

Here we need to add binaries if we're using them in our App.

how to add custom binaries to the lambda package? Suggest ways to do it, if you know.

References:

Upvotes: 3

Views: 5251

Answers (1)

DilLip_Chowdary
DilLip_Chowdary

Reputation: 1191

Starting with python3.8, AWS Lambda removed many Binaries like tar, find file, cat, which etc.

So if you're using any of these binaries in our package, we can add them using layers.

Method 1:

Add libraries in layers in the custom directory, and let lambda know the directory by setting LD_LIBRARY_PATH and PATH environment Variables.

Let's say you've added your binaries in custom_bins directory then you've to set PATH as /var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin:/opt/custom_bins

and if you've added your custom libraries in custom_libs directory then you've to set LD_LIBRARY_PATH as /var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib:/opt/custom_libs

Method 2:

By Default, Lambda will detect libraries in the /opt/lib path and binaries in /opt/bin, as we know layers will be available in /opt directory, we can create a layer with /lib and /bin directories containing appropriate libraries & binaries.

Layer Structure:

custom_libs/
├── bin
│   ├── curl
│   └── bin_2
│   └── bin_3
└── lib
    ├── libbz2.so.1
    ├── lib_2
    ├── lib_3
    └── magic

Here, we need not set any environment variables as we uploaded our libraries & binaries in the path detected by lambda by default.

Note: If your binaries require a magic file then you can add a magic file and specify a magic file path using the MAGIC environment variable.

Upvotes: 2

Related Questions