How to compile resources for AWS Lambda?!

Mohammed Lutfalla
Apr 20 · 4 min read

AWS Lambda was launched in November 2014 as a preview and in 9th of April, 2015, the dream comes true. AWS Lambda was generally available for the users.

I’ve been using AWS Lambda for the last 2 years and I’m already in love with it. Yes, it’s not perfect and there are a lot to do in terms of performance. But, It’s already a mammoth that you can invade the web with.

Why do I use it?

Simply, FREEDOM. I’m working right now on few projects that are following the them of Serverless. I love to work without paying attention for Load Balancing or even Auto Scaling. AWS Lambda gives you the ability to focus on your logic and leave the heavy lifting of configuration and maintaining the OS and its dependencies to AWS. Cool right!?

Compiling resources?

Yes, I didn’t face any issues for last few projects as I was working with less dependencies than what I have now. Lambda works on a Linux distribution, which means that there are some files needs to be readable by Linux file system and the kernel in some cases. I use Mac as my development environment.

One day, I was having an issue with a Python library called “Memory Profiler” that tracks the usage of RAM in the function to detect the memory leak I have. So, I did the traditional way of test, install the wanted resources in a folder with the function handler and upload the zipped file to Lambda. The problem I got that Lambda can’t read the files in this library. I opened a case with the support and I got the answer. This library is Binary based and it needs to be compiled to be usable in Linux. I installed it with Mac, which is different OS.

So now, what is the benefit of the previous words?

AWS Lambda is running on Linux image that have all the dependencies to run your code.

But I’m using Windows or I’m developing my functions on Mac? What should I do?

Fortunately, there are two ways to solve this issue:

1- Starting an ec2 with “amzn-ami-hvm-2017.03.1.20170812-x86_64-gp2"and get the libraries you want.

2- Start a Docker container and do the same process locally (which we will explore now).

How to start?

We will go through the second scenario. But! The same steps are applied to the first scenario.

1- Download Docker:

You need to have Docker in your machine so you can enable it to install the image that simulate lambda environment.

You can get it here.

NOTE: make sure you’ve opened the installed Docker app.

Then, open a terminal and paste this command:

docker pull lambci/lambda:build-python3.6

NOTE: choose the run-time you use, I use Python3.6

After it finished, paste this command:

docker run -v “$PWD”:/var/task -it lambci/lambda:build-python3.6 bash

It will open a bash terminal that has the specified run-time and you’re ready to go.

Cool!!, we’re in AWS Lambda simulated environment.

Pay attention to the next screenshot. I’ll describe what I did below.

Steps as following:

1- I run Docker container to get inside the simulation environment.

2- I’ve made a new directory, which by the way, it’s pointed to your Desktop directory.

3- Enter the new directory.

4- Install the wanted libraries.

- -t in the command refers to where to install files.

- ./ refers to current directory.

5- Done! Now type exit.

The final step you need to do is to zip the folder and upload it to AWS Lambda.

Try to do this step for all of your lambda functions as it guarantees that the installed libraries are compiled to work with AWS Lambda.

There are a lot of repos in GitHub that have pre-compiled libraries for AWS Lambda. You need just to start looking for them, It might save your time.

Mohammed Lutfalla

Written by

An indiviual who looks way far to the future. Tech is the core of my soul and football is what makes me alive.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade