Using a lambda function to interact with a deployed ML model on Sagemaker
When you deploy an ML model on SageMaker you have to communicate with it. A possibility is to use AWS Lambda function to do so. A Lambda function is a serverless computing platform integrated into the AWS catalog of services. This service allows a person to create functions and has the code executed without the need to provide servers. You only pay for the resources you use.
The procedure detailed in this post is focused on ML use cases using Python as a coding language. The procedure can vary depending on the coding language and services used. To create a Lambda function you have to go to the AWS Lambda service. Once done, you have a button to create a Lambda function. During the creation procedure you must choose the runtime environment and the entry point (Handler).
Lambda entry point
The entry point is the handler of the Lambda function and it is the function called by the Lambda. By default the handler is called lambda_handler but you are free to change it to another name. Inside this handler you have to put the different steps executed by the Lambda function.
As mentioned above, a lambda function is running on a specific environment. A list of all possible runtime environments is provided on the AWS website here. An important point is the layer under this environment. Indeed, it is a specific operating system that is running. This means when you use compiled libraries and these must be compiled on the same operating system (Amazon Linux (2)). This can complicate the setup of a Lambda function, a little.
When you select your Python environment, the number all libraries installed by default is rather limited. For instance pandas or NumPy are not installed. To use libraries that are not installed by default, you need to import a compiled version of them inside the Lambda function.
Export Python libraries
On the official documentation some methodologies are proposed but they are not exhaustive and rather unclear. This part is commonly discussed on different forums and for each a different technique is used to export and automate the exportation of the compiled libraries. Among them we can list:
- Using docker with Amazon Linux 2 or a Linux distribution
- Using Jenkins
- Using AWS SAM
- Using EC2 with Amazon linux 2
- Using WLS if you are on Windows
- Using Cloud9
As we can see, the number of methodologies to export compiled libraries is rather diverse and varies in terms of difficulty to set. In my opinion, it is a sign that some work needs to be done by AWS to improve this process.
Nonetheless, among these various methodologies we did try some of them. Using EC2 to compile Python libraries is the methodology we tried. This methodology is rather fast to set. We created an EC2 instance with the Amazon Linux 2 set. After that, we installed the appropriate version of Python used by our Lambda function. Consequently, we created a virtual environment and installed libraries needed into a zip file. Once the zip file was generated we added our Lambda handler into that zip and upload it on an S3. After that, you went into function code section and selected upload a file from Amazon S3 as code entry type. Once done, the Lambda is set and ready to be used. Note: Each time a modification is applied to the code, you must redo the upload action of the package. Without that, your changes will not be taken into account.
The second approach used was the Windows Subsystem for Linux (WSL). The procedure is exactly the same as the EC2 except that instead of using an EC2 instance on AWS, you can simply use the Linux subsystem installed on your Windows distribution.
We also tried the AWS SAM tool which seems to work, but it is more complicated to configure (at least on Windows) with some IDE and virtual environment problems due to wrong paths of the Python interpreter.