Skip to content

alun1111/riverlevels

Repository files navigation

Deploying a Python AWS Lambda function (badly)

Setting up PIP and python locally

Use venv on your python project

python3 -m venv /path/to/new/virtual/env

🐱‍👤 Don't forget to activate it by running .\Scripts\activate!!

Install PIP dependencies

pip install <package>

Generate requirements.txt

pip freeze > requirements.txt

Activate venv and restore from requirements

. ./bin/activate
pip install -r requirements.txt -v

Test lambda with sam

sam lambda invoke

Thrashing around without a clue in the AWS ecosphere

I downloaded the template.yaml from the Lambda console as I had already created the function and hoped this would make things easier.

Then I went to the following places and after a brief careless read downloaded the AWS SAM CLI tool.

AWS SAM docco site

SAM github repo

🐱‍👤 The SAM tool is written in Python and uses the Click library for it's console integration

Tried running the sam build command but get a unspecific error. Running again with the --debug flag, revealed the version of python referenced in my path didn't match the version in the template file.

Then I reorganised my repo to look like the correct AWS deployment tree, which didn't do anything but I figured it needed doing.

After some faffing around with python (windows was hijacking python on the path to open the windows store!!) i got the buld to work fine.

However then I kept getting a error where the wrong role ARN was being used by sam deploy --guided! Gah! Then I noticed that there was an extension for vscode, so gave that a go but no dice.

🐱‍👤 It turned out it was using my default credentials, which i had set to a user with minimal permissions... doh!

What did work

After much messing around, it turned out a flat directory layout is just fine. The SAM tool searches for a requirements.txt and resolves the dependencies. It also uses the template.yaml file to grab lambda settings. In this case i exported from an existing lambda function.

Another cool thing is the SAM tool can deploy locally if you have a docker daemon running, which I don't. But it sounds like a good way to do it.

How to build and deploy

Run sam build and then sam deploy --guided accepting all defaults. I'm not sure if you need the virtualenv running or not, but you definately need the requirements.txt.

If the template file has already been created use sam deploy -t template.yaml

Whatever triggers the lambda must send a riverid in JSON, for example:

{ "riverid": "12345-SG" }

Currently I am using a Cloudwatch schedule rule to run the lambda for three different stations on the river Almond.

Almondell (14869-SG)
Whitburn (14867-SG)
Cragiehall (14881-SG)

About

AWS Lambda function to retrieve River Almond water levels from SEPA's CSV based api

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages