In this post, we are going to walk through a simple but elegant serverless solution to build a location tracking system. Of course, most social media accounts already track your movements throughout the day. However, if you have turned those accounts off to protect your privacy, but still wish to be able to keep track of and record your location data over time, you can easily build your very own location tracking app.
In the past, implementing services such as these could be costly and required spinning up and managing servers. Now you can leverage the power of serverless technology like AWS Lambda along with Amazon API Gateways to build scalable and management-free web services.
So, let’s dive into the details of how we can build a serverless location tracking system.
The architecture for such a system is rather simple. Here are the main components:
- Mobile Device: The mobile device that collects data and sends the information to a web service.
- Web Service: We built a standard REST API web service using Amazon Lambda and Amazon API Gateway.
- Data Lake: We collect the data in an Amazon S3 bucket.
- Data Mining: We use Amazon Athena queries to mine the data in the S3 Bucket to create partial and daily rollups. It speeds up the query process.
- Website: Our website then communicates with the Web Service to get the data and display various aspects of the location information.
Step 1: Building a Collection Web Service using AWS Lambda
We started with an iOS app called Overland (available on the App Store). Overland collects data about locations and it sends the data in configurable batches in JSON format. JSON is great because it’s lightweight and compact. The app also buffers and stores GPS data when it is offline (that is, no wifi or cell network available) in order to prevent data loss. The collected information is sent in batches when the app finds a wifi or cell network connection again. Overland is open-source and has a well-documented HTTP API which makes it an easy app to integrate.
For our web service, even though we could have built it directly using Amazon Lambda, we decided to use Zappa. It’s a toolkit that allows the deployment of standard Python web applications as web services on top of AWS Lambda and Amazon API Gateway. It generates CloudFormation templates, creates API gateway endpoints, maps those requests, and executes them on Lambda functions. Additionally, Zappa keeps the Lambda functions alive on AWS by using no-op executions. Thus, it helps avoid the “cold starts” problem that causes delays in starting AWS Lambda functions.
The location data flows from the iOS app to the web service every 5 minutes. We use Amazon S3 for data storage. We also partition the data into year, month, and day to make it easier to query.
Step 2: Preparing for Amazon Athena with AWS Glue
We use AWS Glue, a managed ETL service, that goes through the normalized JSON-formatted location data and figures out the structure. AWS Glue crawls through the data and rationalizes it into an SQL table. Then, we can easily use Amazon Athena, a serverless query service, to get the information we need. The purpose of using AWS Glue and Amazon Athena is to speed up the query process.
Step 3: Creating Data Rollups with Scheduled Functions
Even though Amazon Athena is fast, it still takes time to execute a query and it’s wasteful to execute the same query over and over again, when I can instead execute it once, in advance, and then save the results for later.
In order to increase efficiency and make it faster, we run cron jobs to call functions on a schedule and create data rollups.
Step 4: Website Integration
So you can see that serverless technology makes it extremely easy to create lightweight web services like the location tracking system above. These services can be created and deployed almost instantly. View my complete presentation here, and if you want to learn more about how serverless solutions can help your organization, talk to one of our specialists today.