Unable to Import Module “Lambda_function”: No Module Named “Pymysql”

  • Home /
  • Blog Posts /
  • Unable to import module “lambda_function”: No module named “pymysql”

If you receive the following error message when trying to run pymysql on AWS Lambda:

Unable to import module "lambda_function": No module named "pymysql" then you can fix this by running a Custom Lambda Layer.

To do this, you can boot up an EC2 instance (or a Linux instance anywhere else) and run the following set of commands:

#create module directory
mkdir -p temp/python
cd temp/python

#install pymysql module
pip install pymysql -t .
cd ..

#create a zip file using installed module
zip -r9 ../pymysql.zip .

#create the lambda layer
aws lambda publish-layer-version --layer-name pymysql \
--description "pymysql for mysql access" \
--zip-file fileb://../pymysql.zip \
--compatible-runtimes python3.8

Once this is done, you will be able to head back to AWS Lambda, click on Layers and view your custom layer waiting there for you to use!

Now you can associate your lambda function to use this custom lambda layer.