AWS Lambda + Python: the easy way (using serverless)

3 min read Original article ↗

Robert Olsthoorn

If you’ve landed here, you’ve realized that AWS Lambda is a great, fun way to have production code up and running. However; when using Python, it quickly becomes obvious that the process of zipping the package, uploading to s3 and navigating the AWS interface is annoying at best.

The following tutorial will take you through an easy deployment, all from the command line:

  1. Installing relevant packages locally
  2. Setting up AWS
  3. Updating your local code
  4. Deployment and execution
  5. Tips/Tricks
  6. (Optional) Setting up Github actions

Press enter or click to view image in full size

Lambda + Python

Install relevant packages

  1. Install serverless
npm install -g serverless

2. Create your repo/package for aws-lambda

serverless create --template aws-python3 \ --name sample_project --path sample_project

3. Create your virtualenv (to keep requirements isolated in python). And activate it.

virtualenv env --python=python3 & source env/bin/activate

4. (Optional) Install any requirements you need for your project

pip install numpy & pip freeze > requirements.txt

5. Install Docker (necessary to set up native packages on AWS lambda)

Set up AWS

In order to push to AWS Lambda from the command line, you need to have an access id and access key and params

  1. Create AWS account (if you haven’t already)
  2. Create a new access key, or find ones you’ve already made before. Keys can be accessed here. https://console.aws.amazon.com/iam/home?#/security_credentials

Press enter or click to view image in full size

AWS login screen. Open the Access Keys option

3. Store the access key id and access key on your local environment (necessary for serverless)

export AWS_ACCESS_KEY_ID=<YOUR_KEY_ID> AWS_SECRET_ACCESS_KEY <YOUR SECRET_ID>

4. (Optional) Store the variables as an alias, that way you can make sure you always push to the right AWS environment

alias aws_creds="export AWS_ACCESS_KEY_ID=<YOUR_KEY_ID> AWS_SECRET_ACCESS_KEY <YOUR SECRET_ID>"

Update Python code on your machine

Remember how you ran create serverless , now enter into the directorycd sample_project . There are two files of importance handler.py and serverless.yml

Get Robert Olsthoorn’s stories in your inbox

Join Medium for free to get updates from this writer.

Remember me for faster sign in

handler.py is where your python code lives. Update it appropriately.

If the package size is large and AWS complains, uncomment the top.

To run on your own machine

python handler.py

serverless.yml is where you configure serverless for uploading.

For a full-visibility of options visit: https://gist.github.com/Rolstenhouse/1987fc5e6268e8768af8fa7f311da53f

Deploy and debug

  1. Run the deployment script, this will take a few minutes.
serverless deploy

Once it finishes, check in AWS lambda that it updated appropriately.

Press enter or click to view image in full size

There’s a chance that you have to check a different AWS region. Mine uploaded to us-east-2

2. Invoke and call the function from your command line

serverless invoke -f sample-scraper --log

You did it! Tips/tricks

  1. To change the frequency, update the cron field in the serverless.yml file.
  2. If the file size is too large, zip the requirements. Change the zip:true in serverless.yml
  3. Trying to store a file locally? Lambda lets you write to the /tmp directory. Once you write it there, you can use s3 to then offload it to another place.