Weatherfarm
Serverless API for a farm weather and alerts mobile app
Sole committer for the two months of my involvement; authentication, user preferences, error handling, push-alert fixes, and much of the integration tests
Weatherfarm is a weather network for farmers in western Canada, run by Glacier FarmMedia, with a mobile app for forecasts, station observations, radar, and push alerts. This is the serverless API behind that app, built by Apply Digital.
Tech
The API is about two dozen Python functions on AWS Lambda behind API Gateway, deployed with the Serverless Framework. DynamoDB (through PynamoDB) stores alerts, devices, preferences, and subscriptions, and SNS delivers push notifications. Forecasts, observations, and radar imagery come from a third-party weather data provider. Environment Canada’s public alert feeds are polled every ten minutes and turned into push notifications for subscribed devices. Integration tests are written with Tavern, and deployment runs from Bitbucket Pipelines.
Where I came in
I joined in November 2018, nine months into the project. The original developers’ last commits were in early October, and from then until January I was the only person committing to the repository: about ninety commits in eight weeks. The weather, radar, and alert endpoints already existed, so my work was mostly about identity, preferences, and robustness.
Authentication
Until then, endpoints took the user’s ID as a URL or query-string parameter, with API Gateway API keys as the gate. I added JWT bearer-token authentication as a Python decorator applied to every HTTP-triggered function. It checks the signature, expiry, not-before time, and subject, returns a distinct 401 error code for each failure, and hands the verified user ID to the handler. The tokens are issued elsewhere, so the API only holds the shared verification key.
Once that was in, the user ID parameters and then the API keys were redundant, and I removed them. Alerts, subscriptions, and devices were all re-keyed on the user ID from the token.
One thing worth knowing if you use serverless-offline: it suggested that Lambda adds the verified claims to the request context, but real Lambda does not, so the decorator does that itself.
Push notifications and devices
Each device has its own SNS endpoint. I changed the devices table to be keyed by device ID alone, so a device has exactly one registration, and logging in as a different user on it simply hands it to the new user. I also added a logout endpoint which deregisters the device’s endpoint.
The Environment Canada alert pipeline needed repair too. I fixed the queries which find the devices to notify, and added logging and an early exit for alerts which nobody has subscribed to.
Preferences and “my field”
The app’s “my field” screen shows growing degree days, crop heat units, and rainfall from selected weather stations, relative to the date a farmer seeded. I added the preferences endpoints which store the seeding date, as a month and day in a custom DynamoDB attribute type which validates on assignment. I also wrote the date logic which works out whether a field is currently between seeding and harvest, handling year boundaries and time zones, with doctests covering the awkward cases. The endpoint now uses hourly observations in the station’s own time offset.
There was a good little bug in there too:
on Mondays the weekly rainfall total was the whole 32-day window’s total,
because Python’s weekday() is zero on Monday and that zero was used as a negative slice
length.
Errors and tests
Handlers had been building their own error responses.
I standardized them on one helper with a consistent scheme of error codes
(auth:bad-bearer-token:expired, for example),
and documented each endpoint’s parameters and possible errors in its function description in
the Serverless config.
I wrote nearly half of the integration tests which survive, including the authentication edge cases: expired tokens, tokens which aren’t valid yet, and tokens with a missing or non-numeric subject.