AWS Lambda to MySQL and API Gateway

Posted under » Python » AWS on 24 May 2021

Why do you need Lambda to access RDS? Well there is no server involved and it is suited for high availability. Let's try it out.

Since no server is involved, you need to have all the python dependencies that the function requires.

If you add the package to the layer via pip from Windows/MacOS machine, it would not be compatible with AWS Lambda Python runtime. In that case, you need to download the wheel file for Linux (from PyPi), extract it, and the use that for your AWS Lambda layer. On top of that, you also need to find the wheel file for the package dependencies too. For example, if you want to use pandas, you also need to download: numpy, pytz, python-dateutil. Again, if you want to use python-dateutil, you would also need to download six packages.

All of that hassle can be very easily done in Linux environment. Installing packages via pip3 command will also pull the dependencies.

The directory tree should contain python/lib/python3.8/site-packages(site directories) and the bin folder.

One of the most important package when you want to connect to MySQL is the pymysql.

You should exclude "__pycache__"

Another thing is that, it will look for "def lambda_handler(event, context):" in the otherwise there will be an error.

Once you have created it, you can test it

You will have to choose an execution role. It is an AIM role attached to the lambda function

The basic role allows us to upload logs to AWS Cloudwatch logs.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS