Perfsys — AWS Consulting Partner
Amazon Location Service concept showing AWS cloud linked to maps, geofencing, and tracking icons over a futuristic city skyline
AWS ConsultingLocation Service

How to Use Amazon Location Service for Geofencing and Real-Time Tracking

Published on

5 min read
pattern

Introduction

Amazon Location Service is a fully managed AWS service that helps developers add maps, geocoding, routing, and geofencing to modern applications.

In this tutorial, you will learn how to use Amazon Location Service with AWS Lambda, EventBridge, and SNS to build a real-time geofencing system that sends notifications when a device enters a defined area.

We will walk through a practical example using trackers, geofence collections, and serverless architecture on AWS.

What Is Amazon Location Service?

Amazon Location Service is a location-based service that developers can use to add geospatial data and location functionality to applications.

Customers can visualize data on a map, recommend routes, use geocoding to convert plain text addresses into geographic coordinates, use reverse geocoding to convert latitude and longitude coordinates into addresses, and monitor and track assets such as fleets of vehicles.

Let's try to implement a test use case where you get notification when somebody enters the specific area using Amazon Location Service.

Step 1: Create a Geofence in Amazon Location Service

Geofence is a set of coordinates where we indicate a zone around the delivery address.

For this purpose we use geojson.io.

Also you can use the Amazon Location interactive tool.

map screenshot

Save output as a json file.

In the Amazon Location console, open the Geofence collections page and create a new one.

In the created geofence collection add our coordinates.

Amazon Location console screenshot

Step 2: Create a Tracker in Amazon Location Service

Now we will create a tracker that gets location updates.

In the Amazon Location console, open the Trackers and create our tracker with default parameters.

Then link this tracker to our geofence collection.

Amazon Location console screenshot part 2

Step 3: Set up notification

To get notification when a driver enters the geofence we have to use Amazon Simple Notification Service (SNS).

In the Amazon SNS console, open Topics and create standart topic.

In the topic create a subscription with Email protocol and enter your email address.

After creation of subscription you will get a confirmation message by email.

Amazon Location console screenshot part 3

Step 4: Create a Lambda function

In this step we create a lambda function that sends notification when the delivery driver enters the area.

import json
import boto3

sns_client = boto3.client('sns')

def lambda_handler(event, context):
    print(event)

    topicArn = 'arn:aws:sns:us-east-1:acct-num:delivery-topic'
    subject = 'Geofence notification'
    message = 'Your delivery is within the area, you should receive it soon!'

    response = sns_client.publish(
        TopicArn=topicArn,
        Subject=subject,
        Message=message,
        MessageAttributes={"email" : { "DataType":"String", "StringValue":"mruletkin6@gmail.com"}})
    return {
        'statusCode': 200,
        'body': json.dumps('Success!')
    }
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "sns:Publish",
            "Resource": "arn:aws:sns:us-east-2:*:delivery-topic"
        }
    ]
}
Build Smarter Location-Aware Applications with AWS
Build Smarter Location-Aware Applications with AWS

Our AWS-certified engineers can help you design, integrate, and optimize geofencing, real-time tracking, and location-based solutions using AWS Location Service.

Book a Free Consultation
See Our Case Studies

Step 5: Create EventBridge rule

EventBridge rule will get geofence events from Amazon Location tracker and routes that data to SNS topic.

Create it in this way:

  1. In the AWS EventBridge console, open rules and click on create rule;
  2. On the first step, enter the name and leave other parameters default;
  3. On the second step, configure the event pattern.
  4. Choose AWS Service as event source, Amazon Location Service as AWS service, Location Geofence Event as Event type and ENTER under specific type;
  5. On the third step, choose Lambda Function as a target and define the name of the function;
  6. Other steps leave without changing and create the rule.
Amazon Location console screenshot part 4

Step 6: Set up a Lambda function

Create another one function that will publish the driver's location to Amazon Location tracker.

There are a few ways to receive geofences:

  1. From a mobile device. You can call this function from a mobile application that transmits a location update.
  2. Simulate position updates. You can create a Lambda function to publish synthetic device position updates.

We will use the second way.

from datetime import datetime
import json
import os

import boto3

client = boto3.client("location")

def lambda_handler(event, context):

    updates = []
    updates.append({
        'DeviceId': event['deviceid'],
        'SampleTime': datetime.now().isoformat(),
        'Position': [event['long'], event['lat']]
    })

    client.batch_update_device_position(TrackerName=TRACKER_NAME, Updates=updates)

    return {
        'statusCode': 200,
        'body': json.dumps('Success')
    }

Configure Permissions of this lambda by adding a policy that allows upload position update data.

The value of Resource must be arn of tracker.

{
  "Version": "2012-10-17",
  "Statement": [
    {
    "Sid": "UpdateDevicePositions",
    "Effect": "Allow",
    "Action": ["geo:BatchUpdateDevicePosition"], "Resource":
    "arn:aws:geo:us-east-1:*:tracker/delivery-tracker"
    }
  ]
}

Test position update

Now we can test our use case. In Lambda console invoke the function and pass several test events.

This test event will put the сoordinates outside the geofence:

{
  "deviceid": "Person1",
  "long": 35.06816625595093,
  "lat": 48.457483847716304
}
map with location pin screenshot

This one will trigger the ENTER event of the geofence and the EventBridge rule we created in the previous step will detect the event and trigger SNS notification:

{
  "deviceid": "Person1",
  "long": 35.07041931152344,
  "lat": 48.458195352987275
}
second test graph

So here we've got our push notification:

email

FAQ

Amazon Location Service is a managed service that allows developers to add maps, geocoding, tracking, and geofencing to applications without managing complex geospatial infrastructure. It's commonly used for delivery tracking, fleet management, logistics, and real-time location-based notifications.

Geofencing in Amazon Location Service works by defining a virtual boundary (geofence) around a real-world location. When a device or asset with a tracker enters or exits this boundary, an event is triggered. This event can be processed through Amazon EventBridge or AWS Lambda to send alerts, automate workflows, or update applications in real time.

Yes. AWS Lambda can be integrated with AWS Location Service to automate actions when geofence events occur. For example, you can trigger a Lambda function to send a push notification or publish a message to Amazon SNS whenever a device enters or exits a defined geofence.

Unlike third-party APIs, Amazon Location Service offers native integration with the AWS ecosystem, strong data privacy controls, and cost optimization through usage-based pricing. It allows companies to build secure and scalable geolocation solutions while keeping sensitive location data within AWS infrastructure.

Conclusion

In this article we walked through the steps of creating applications for getting notifications using Amazon Location SDK for Python (boto3).

Use case we implemented showed us a small part of Amazon Location Service functionality.

To summarize it's an important service that enables customers to create complex decisions using holistic AWS infrastructure that allows them to optimize the development process and costs.

Anastasiia Tokareva
Anastasiia Tokareva

Ex-Software Engineer at Perfsys focused on building reliable, well-tested features across the product's frontend and backend.

Related Articles

View All Articles

Explore Our Case Studies

View all Case Studies
  • Heroku to AWS Migration: How We Moved a Live Fitness App Backend in 7 Weeks

    7

    weeks to production on AWS

    11

    blockers found in Phase 1

    0

    user-facing incidents

    AWS MigrationFitnessGermany

    Heroku to AWS Migration: How We Moved a Live Fitness App Backend in 7 Weeks

    Perfsys moved a German fitness brand's app backend from an agency's Heroku setup to Amazon ECS Fargate in 7 weeks, with no user-facing incidents and no data loss.

  • AWS Consulting Case Study: How to Plan a Heroku to AWS Migration Without Live Access

    3

    weeks to a finished plan

    11

    migration blockers found

    6

    cutover phases with rollbacks

    AWS ConsultingMigration PlanningDiscoveryGermany

    AWS Consulting Case Study: How to Plan a Heroku to AWS Migration Without Live Access

    Perfsys reviewed a German fitness brand's agency-run Heroku backend with no live access, found 11 migration blockers in about 3 weeks and delivered a plan the client could act on.

  • AWS Infrastructure for a Fintech Startup: Live in 6 Weeks

    5 days

    to a live environment

    6 weeks

    to controlled launch

    0

    failed releases

    AWS InfrastructureTerraformFintechUnited Kingdom

    AWS Infrastructure for a Fintech Startup: Live in 6 Weeks

    Perfsys took OlivPay, a fintech pay-for-me platform, from an empty AWS account to a live environment in 5 days and to a controlled production launch in 6 weeks. The result: zero failed production releases, private merchant ID documents and a one-click release pipeline.

  • Building an AI Report-Generation App That Cut Report Time by 98% on AWS Bedrock

    30

    days to build

    98%

    faster report generation

    0

    generation failures after deployment

    AWS BedrockAI AppmacOS

    Building an AI Report-Generation App That Cut Report Time by 98% on AWS Bedrock

    Perfsys built a custom macOS app for an edtech consultant using AWS Bedrock — cutting report generation time from half a day to under 5 minutes, with zero generation failures.

  • How a US Mortgage Technology Startup Used Amazon Neptune to Unify Loan Origination Data for 30+ B2B Customers

    30+

    B2B customers served

    ~1 Billion

    Relationships in graph

    6 months

    Migration timeline

    Amazon NeptuneGraph DatabaseFintechUnited States

    How a US Mortgage Technology Startup Used Amazon Neptune to Unify Loan Origination Data for 30+ B2B Customers

    How a US mortgage startup used Amazon Neptune to absorb loan origination data from 30+ B2B customers across 5 industries, scaling to a billion-edge graph without per-customer schema redesign.

AWS Experts, On-Demand

Need to move fast? Our cloud team is ready to scale, secure, and optimize your systems. Get serverless expertise, 24/7 support, and seamless CI/CD pipelines when you need it most.

Please accept cookies to load the booking widget.