Adding a Python Package to AWS-Lambda via the Lambda Layer
Cloud infrastructure is currently the solution to many IT tasks required on a daily basis. The cloud offers a simple solution to increase capacity (scaling), choice of resources according to your needs, powerful instances, less maintenance, and all this can be obtained at a relatively affordable price. There are many business schemes of cloud services offered by many providers, which may make you confused about choosing the cloud. One of the very powerful and affordable cloud services is AWS-Lambda, also known as Function among other providers. This is a solution serverless application where in this service we will focus only on code rather than infrastructure etc. In the previous article, I discussed serverless on AWS-Lambda using Yii2 which is a PHP framework. PHP on Lambda uses a runtime so executed PHP code does not run natively on Lambda. For this reason, to save more resources, I personally prefer to use Lambda solution using a supported programming language in Lambda, one of the supported programming languages in Lambda is Python.
Python is one of the most readable languages with many supported libraries. When using Lambda, the available libraries are limited to the default Python libraries and some AWS libraries. What if we need additional libraries in Lambda? If it is on the server we can easily run it pip install package. However, in Lambda we will use a feature called layers.
Contents
Requirements
To make this tutorial easier to follow, you should pay attention to certain things:
- You have an AWS account
- Your computer has been installed with docker and docker-compose
Create a Docker Lambda layer container
The first step, we will create a file with the name Dockerfile (place it in a special folder, for example LambdaLayer folder) and then fill the file as below.
FROM amazonlinux:2.0.20191016.0
RUN yum install -y python37 && \
yum install -y python3-pip && \
yum install -y zip && \
yum clean all
RUN python3.7 -m pip install --upgrade pip && \
python3.7 -m pip install virtualenv
The above command will use the amazonlinux docker image and install Python with the packages we need to create a python package that matches the lambda layer format.
Next, create and run the Dockerfile.
docker build -f "<filename>.Dockerfile" -t lambdalayer:latest .
docker run -it --name lambdalayer lambdalayer:latest bash
After the above command, we will enter bash from our container.
Install the package into the Lambda layer container
Next, we need to isolate the package we want to convert to lambda layer using virtuanenv which we previously installed. Run the command below where package is the virtualenv you want to use as lambdalayer support.
python3.7 -m venv package
You can create a new package without recreating the container by using a different package name. Next, navigate to virtualenv for packages with the activate command, then install the python package we need.
source package/bin/activate
pip install your-python-package -t ./python
deactivate
ZIP and upload the package to the AWS-Lambda layer
After that, you can zip the package into ./python folder with the following command.
zip -r python.zip ./python/
Next, please exit the container and then run the following command to get the python.zip file which we will upload to the lambda layer later.
docker cp lambdalayer:python.zip ./Desktop/
Please go to your AWS-Lambda and upload the file by pressing the Create Layer button and upload the zip file.
Once the download is successful, you can use external Python packages on your AWS-Lambda. After that the sky is the limit.
That’s all for this tutorial, I hope it’s useful and Happy Coding!
Source:
PakarPBN
A Private Blog Network (PBN) is a collection of websites that are controlled by a single individual or organization and used primarily to build backlinks to a “money site” in order to influence its ranking in search engines such as Google. The core idea behind a PBN is based on the importance of backlinks in Google’s ranking algorithm. Since Google views backlinks as signals of authority and trust, some website owners attempt to artificially create these signals through a controlled network of sites.
In a typical PBN setup, the owner acquires expired or aged domains that already have existing authority, backlinks, and history. These domains are rebuilt with new content and hosted separately, often using different IP addresses, hosting providers, themes, and ownership details to make them appear unrelated. Within the content published on these sites, links are strategically placed that point to the main website the owner wants to rank higher. By doing this, the owner attempts to pass link equity (also known as “link juice”) from the PBN sites to the target website.
The purpose of a PBN is to give the impression that the target website is naturally earning links from multiple independent sources. If done effectively, this can temporarily improve keyword rankings, increase organic visibility, and drive more traffic from search results.