Unable to import module “lambda_function”: No module named “pymysql”

0 min read 149 words

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.

Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags