0

I am very new to python code and doing some small tests to verify functionality

Currently trying to establish the connection between an RDS MySQL and a python lambda function.

However, it seems to fail in the code itself and I am not sure why this happens. There are a couple of guides out there but they all seem to be outdated and fail to work for me.

These are the steps I took to get it working(using MAC-12.3.1 and VS-Studio 1.62.3):

  1. created MYSQL RDS
  2. connected to the MYSQL RDS and created a database called "igor" with table name "lumigor", with 2 columns: id and name (populated with random data).
  3. created on the local machine a folder to contain the code and the package.
  4. installed version Python 3.8.9
  5. created lambda function file app.py with the following code:
import pymysql.cursors

# Connect to the database
connection = pymysql.connect(host='rds end point',
                            user='user',
                            password='pswrd',
                            database='igor',
                            cursorclass=pymysql.cursors.DictCursor)

with connection:
   with connection.cursor() as cursor:
       # Read a single record
       sql = "SELECT * FROM `Lumigor`"
       cursor.execute(sql, ('[email protected]',))
       result = cursor.fetchone()
       print(result)
   ```
  1. I added requirement.txt file with the following command
python3 -m pip install PyMySQL && pip3 freeze > requirements.txt --target...

But now I get an error from the visual studio:

"Import "pymysql.cursors" could not be resolved from sourcePylance"

When I zip the file and upload it to lambda, run a test it returns an error

{
  "errorMessage": "Unable to import module 'app': No module named 'pymysql.cursors'",
  "errorType": "Runtime.ImportModuleError",
  "stackTrace": []
}

It seems like the dependcies are missing even though installed them and they exist in the directory

1 Answer 1

0

The proper way to add pymysql to your lambda is by creating a dedicated layer a as described in the AWS blog:

  1. Create empty folder, e.g. mylayer.

  2. Go to the folder and create requirements.txt file with the content of

PyMySQL
  1. Run the following docker command (new image from lambci/docker-lambda for Python 3.9):
docker run --rm --volume "$PWD:/var/task" --workdir /var/task senorcoder/aws-lambda-env:python3.9_build pip install -Ur requirements.txt --target python
  1. Create layer as zip:
zip -r mypymysqllayer.zip python > /dev/null
  1. Create lambda layer based on mypymysqllayer.zip in the AWS Console. Don't forget to specify Compatible runtimes to python3.9.

  2. Add the layer to your function:

Alternatively, create your function as Lambda container image

Sign up to request clarification or add additional context in comments.

2 Comments

@IgorKarpenko No problem. Let me know how did it go.
Sorry for the late reply, I got it to work, thanks for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.