Solution for error: No module named 'PIL or AWS Lambda Python PIL cannot import name ‘_imaging’” Creating a custom layer for lambda function for PIL

Ram KumarRam Kumar
4 min read

What is aws lambda custome layer:

In the context of AWS Lambda, a custom layer refers to a package of code or libraries that can be attached to a Lambda function to provide additional functionality. Custom layers allow you to separate reusable code from your Lambda function code, making it easier to manage, update, and share common functionality across multiple Lambda functions.

You can create a custom layer by packaging your code, libraries, or dependencies into a .zip file or a .tar.gz file. You can also include runtime-specific folders such as python, nodejs, or java to specify the runtime for which the layer is intended

Please follow the below steps to create a custom layer for the AWS lambda function, you can choose any language which the Lambda function support but here I am taking an example of Pyhton3.8

  1. Login to the cloud9 environment, if you need any help creating the cloud9 environment, please refer to my blog (Create-cloud9-environment) where I explained every step, it will take 3-4 minutes to spin up a could9 environment.

  2. check what Python version is installed already to your cloud9 environment using the command: python -V

  3. If your version is not 3.8 then upgrade the Python version to 3.8 using the commands mentioned in the GitHub file Upgrade_python_to_3_8_version.sh

    or execute the below commands one by one, few commands will take some time, please bear with it.

    sudo yum update -y

    sudo yum groupinstall "Development Tools" -y

    sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel -y

    cd /tmp wget https://www.python.org/ftp/python/3.8.12/Python-3.8.12.tgz

    tar xzf Python-3.8.12.tgz

    cd Python-3.8.12 ./configure --enable-optimizations

    make -j 4

    sudo make altinstall

    python3.8 -V

    sudo alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.8 1

    sudo alternatives --set python3 /usr/local/bin/python3.8

  4. run python3 -V to verify the version, it should print 3.8, if your python version is 3.8 then follow the next step to cerate the layer.

  5. In the terminal execute the command mkdir python to create a directory named python then ls -l command to list the created directory python, you can keep any name of the directory, but I m keeping it python as creating layer for python runtime

  6. Then run the below-mentioned commands in the sequence to create to install the pillow library which is required for PIL and create the zip of installed libraries ls 0l

    python3 -m pip install pillow -t python/ --upgrade

    zip -r layer.zip python

    ls -l

  7. After creating the zip file publish the layer to use, Using the below command, use the name of your convenience for the layer as I am using my-layer, provide the zip file name which you have created, for mine its layer.zip and region which one you are using, for me it's us-east-1

    aws lambda publish-layer-version --layer-name my-layer --zip-file fileb://layer.zip --compatible-runtimes python3.8 --region us-east-1

    The above command will publish the layer and give the output likes below: there will be a number instead of account-id as I have intentionally removed it from the output

    {

    "LayerVersionArn": "arn:aws:lambda:us-east-1:<Account id>:layer:my-layer:1",

    "Description": "",

    "CreatedDate": "2023-04-14T17:10:06.845+0000",

    "LayerArn": "arn:aws:lambda:us-east-1: <Account id>:5:layer:my-layer",

    "Content": {

    "CodeSize": 3682840,

    "CodeSha256": "OIjon2fIcvWK/PdXt/jWh1+zzvLz8xxL4p/AS85ix8M=",

    "Location": "https://prod-04-2014-layers.s3.us-east-

    "Version": 1,

    "CompatibleRuntimes": [

    "python3.8"

    ]

    }

  8. You can check if any layer is associated with your lambda function using the below command, please replace the function name as i used testfuction, its should give you an output null if there is no layer associated with your lambda function

    aws lambda get-function-configuration --function-name testfunction --query 'Layers[*].Arn' --output json

  9. Update the lambda function and associate the layer to the lambda function using below command

    aws lambda update-function-configuration --function-name testfunction --layers arn:aws:lambda:us-east-1:<aws account id>:layer:my-layer:1

    output will be like:

    {

    "Layers": [

    {

    "CodeSize": 3682840,

    "Arn": "arn:aws:lambda:us-east-1: :<aws account id>::layer:my-layer:1"

    }

    ],

    "LastUpdateStatus": "InProgress",

    "FunctionName": "testfunction",

    "LastModified": "2023-04-14T17:27:08.000+0000",

    "RevisionId": "8eb50a68-1785-4df3-b9ea-f149a09e6a98",

    "LastUpdateStatusReason": "The function is being created.",

    "MemorySize": 128,

    "State": "Active",

    "Version": "$LATEST",

    "Role": "arn:aws:iam:: :<aws account id>::role/service-role/janitest-role-9wipbk1m",

    "Timeout": 3,

    "Handler": "lambda_function.lambda_handler",

    "Runtime": "python3.8",

    "TracingConfig": {

    "Mode": "PassThrough"

    },

    "CodeSha256": "jHjR/M5w3SGq9+HQVpzFVLESykUGNWfK8958wFG6PDU=",

    "Description": "",

    "LastUpdateStatusReasonCode": "Creating",

    "CodeSize": 1427,

    "FunctionArn": "arn:aws:lambda:us-east-1:<aws account id>::function:testfunction",

    "PackageType": "Zip"

    }

  10. Verify the layers of your lambda function using the below command:

    aws lambda get-function-configuration --function-name testfunction --query 'Layers[*].Arn' --output json

    Output:

    [

    "arn:aws:lambda:us-east-1:<Account_id>:layer:my-layer:1"

    ]

It will solve the error: No module named 'PIL or AWS Lambda Python PIL cannot import name ‘_imaging’

You can choose any language like node4.js, java, python , as I used this example for python because I am using python as my Lambda function code language

PLEASE DO like , Share and comment if this blog helps you and any manner, and please provide feedback in comments so I can create the best content which can help the community

3
Subscribe to my newsletter

Read articles from Ram Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ram Kumar
Ram Kumar

I am a seasoned DevOps Engineer/Consultant with a passion for helping organizations streamline their software delivery processes. With expertise in tools like GIT, Jenkins, Docker, Kubernetes, AWS, GCP and terraform. I specialize in designing and implementing automated build, test, and deployment pipelines to accelerate time-to-market and improve quality. I have extensive experienced on various Linux flavor's , Shell Scripting, Oracle Andin, SQL, PL/SQL. In my posts, I'll be sharing insights on DevOps best practices, infrastructure automation, and optimizing software delivery workflows. Stay tuned for valuable tips and tricks to enhance your DevOps practices and deliver reliable software to your customers. Let's connect and embark on a journey towards efficient and reliable software delivery! Feel free to reach out for any DevOps-related questions or discussions. Looking forward to connecting with fellow DevOps enthusiasts! #DevOpsConsultant #Automation #SoftwareDelivery #DevOpS Engineer