Skip to content
Soroka Tech Blog

Very Nordic Problem - In search of great skiing track

serverless, devops, aws, tutorial, typescript4 min read

Banner Image

Finland is the happiest country on Earth. It has endless forests and lakes. During wintertime, most of the territory has snow. People are using this combo of cold conditions and excellent services. Cross-country skiing is becoming very popular. However, I noticed that the municipality should prepare the forest tracks properly using heavy machinery.

Us a ski track user, I would like to be notified when the tracks are fresh and cleaned. If there were maintenance in the morning, the ice on the trails would be an obstacle for comfortable skating-style skiing. In theory, I would like to avoid it.

Map

Map of the tracks called Outdoor Exercise Map shows the tracks' state. If I want to get the latest data about maintenance, there is the only way - go to the website and check. My motivation behind making the Ski Track Notifier is to stop doing these repetitive tasks. I decided to solve the issue with my favorite technologies: AWS, CDK, Serverless. All the code examples in this article focus on Typescript. The project code is on GitHub.

Architecture

The idea behind the application is to use as many managed services as possible. It will mean that the price will be only for the invocations. AWS Lambda is the primary business logic executor. The app calls it periodically to check the Helsinki region Service Map API.

The flow is going as follows. The EventBridge rule calls AWS Lambda every 30 minutes. The function checks the API and filters out the ski tracks which were not ready inside the time interval of 60 minutes. There is an SNS topic to gather the PublishCommands from Lambda. It has an e-mail subscription as a notification target. Here is the architecture of the solution.

Architecture

Tooling

I decided to make development and deployment as easy as possible. It is a small project which should be free for hosting and fast to put into production. This section will speak about the technologies used for the application. I would start from the cloud resources to the actual business logic.

Cloud Resources

Infrastructure as Code and CI/CD pipeline allow me to make my application available in minutes from the 'master' branch. I used CDK with CDK Pipelines for provisioning resources. Because I am working alone on this project, there is no need for a multi-branch setup. There are two CloudFormation stacks in the project. One is responsible for CI/CD pipeline made with the CodePipeline service. Another stack is for implementation. It consists of Lambda, SNS, permissions, and EventBridge Trigger Rule in a nutshell.

Stacks

Project Structure

I used the monorepo approach, where the implementation and the infrastructure instructions live in the same codebase. Beforehand there were two issues with this approach:

  • Two 'package.json' files in the source folder and the 'infra' one for dependencies created a dependency mess. It means that there will be two package-lock and two folders with node_modules.
  • Packaging lambda code dependencies meant having an extra step of the building and packing it. One could do it via lambda layer or external bundler like webpack.

Thanks to CDK team, now there is a construct called NodejsFunction. It solves precisely these problems by simplifying the build process and code structure. According to the creators, it uses an esbuild that should be 'extremely fast'. I tried it, and it blew my mind with simplicity. My function resource looks like this with just an entry point to generate a valid bundled js file with the tree-shaking. This approach will pack exactly this function without having a whole folder in the cloud.

1const getSkiTrackStateHandler = new aws_lambda_nodejs.NodejsFunction(this, 'MyFunction', {
2 entry: path.join(__dirname, '../../src/functions/get-ski-track-state.ts'),
3});

One will run only four commands to complete building a valid CloudFormation template. The instructions generated from this array:

1const buildCommands = [
2 'npm i',
3 'npm run build',
4 'npm run test',
5 'npx cdk synth'
6];

Business Logic I mainly used the TDD (Test-Driven Development) approach to develop the implementation. My idea was to decouple the API call made with Axios GET request from the actual filtering. Keeping this in mind helps to test it faster. I could create a fixture with the sample ski tracks. For the tests, I used jest.

The API returns a JSON object. Every ski track has a key called observations. It is an array of objects that should have property each.

1observations: [
2 {
3 unit: 54322,
4 id: 545294,
5 property: 'ski_trail_condition',
6 time: '2022-01-07T23:18:48.129560+0200',
7 expiration_time: null,
8 name: {
9 fi: 'Hyvä',
10 sv: 'Bra',
11 en: 'Good condition',
12 },
13 quality: 'good',
14 value: 'good',
15 primary: true,
16 },
17 {
18 unit: 54322,
19 id: 545293,
20 property: 'ski_trail_maintenance',
21 time: '2022-01-07T23:18:48.129560+0200',
22 expiration_time: null,
23 name: {
24 fi: 'Kunnostettu',
25 sv: 'Iståndsatt',
26 en: 'Maintenance finished',
27 },
28 quality: 'unknown',
29 value: 'maintenance_finished',
30 primary: false,
31 }
32]

I need to have ski_trail_condition with the good value. Also, there is ski_trail_maintenance. It should have value with the maintenance_finished. One more condition is to have it in the last hour. For the time comparisons, there is a library called luxon.

Example

Here is the screenshot of the letter sent to the e-mail. It has the track's name, parsed date, and address. Surprisingly, maintenance data comes with the delay to the API. That is why it is essential to have this ratio between running each lambda and checking the track update age.

E-mail

Future work

Of course, there are more insights about the tracks in the API. For example, 'valaistu' means the availability of artificial light on the tracks. Also, there is a distance shown in kilometers.

Now the application supports only one subscriber. In the future, it would be great to have multiple subscribers, the ability to choose municipality, and track choice of the main focus. Also, it would be nice to connect something like an Amazon Lex-powered bot for multiple channels.

One other idea is to send notifications in batches. The current implementation has the drawback of sending multiple e-mails if there are recent maintained tracks. Today, there is no separate check that the notification about a particular object has been sent to the subscriber.


It took a couple of evenings to create a small full-fetched application to notify myself about the changes found in the public API. With the power of serverless and event-driven architecture, one could deliver business value fast without worrying about servers, virtual machines, or containers.

Thanks for reading!

Follow me on Twitter

Connect on LinkedIn

© 2022 by Soroka Tech Blog. All rights reserved.
Theme by LekoArts