AWS CDK Location Service (Alpha)
The AWS CDK Location Service Alpha module provides L2 (Level 2) constructs for interacting with AWS Location Service. This allows developers to define Location Service resources like Trackers, Geofence Collections, Maps, and Place Indexes using Python. As an 'alpha' module, its APIs are subject to change and it's generally not recommended for production use. It is released in lockstep with the main `aws-cdk-lib` package.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk.aws_location_alpha'
cause The `aws-cdk-aws-location-alpha` Python package is not installed, or there's a version mismatch with `aws-cdk-lib`.fixRun `pip install aws-cdk-aws-location-alpha`. Ensure `aws-cdk-aws-location-alpha` and `aws-cdk-lib` share the same major.minor.patch version prefix. -
AttributeError: module 'aws_cdk.aws_location' has no attribute 'Tracker'
cause Attempting to import L2 constructs (like `Tracker`) from the non-alpha `aws_cdk.aws_location` module, while they only exist in the `_alpha` module.fixChange your import statement to `import aws_cdk.aws_location_alpha as location` and then use `location.Tracker(...)`. -
jsii.errors.JavaScriptError: TypeError: Class constructor Location cannot be invoked without 'new'
cause This often indicates a deep incompatibility issue, frequently caused by a mismatch between the `aws-cdk-lib` and `aws-cdk-aws-location-alpha` package versions, or an outdated `jsii` runtime.fixEnsure `aws-cdk-lib` and `aws-cdk-aws-location-alpha` are installed with matching major.minor.patch versions. You may need to update `jsii` or reinstall your `node_modules` if you are working in a JSII-enabled environment. -
Error: There are no 'context' values defined for this stack.
cause CDK expects an AWS environment (account and region) to synthesize or deploy the stack, but it was not provided in the `App` or via environment variables.fixPass an `env` object to your Stack, e.g., `env=Environment(account=os.environ.get('CDK_DEFAULT_ACCOUNT'), region=os.environ.get('CDK_DEFAULT_REGION'))`. Ensure `CDK_DEFAULT_ACCOUNT` and `CDK_DEFAULT_REGION` are set in your shell environment.
Warnings
- breaking As an 'alpha' module (`a0` suffix in version, `_alpha` in module name), APIs are subject to breaking changes without major version bumps. This module is not recommended for production workloads.
- gotcha Alpha modules (`aws-cdk-aws-location-alpha`) must be strictly version-locked with the core `aws-cdk-lib` package. Mismatched versions can lead to `ModuleNotFoundError`, `AttributeError`, or `TypeError` due to underlying JSII compatibility issues.
- gotcha Deploying AWS CDK applications requires the AWS CDK CLI (`aws-cdk`) to be installed globally and configured with valid AWS credentials and a default region/account.
Install
-
pip install aws-cdk-aws-location-alpha -
npm install -g aws-cdk
Imports
- aws_location_alpha
import aws_cdk.aws_location as location
import aws_cdk.aws_location_alpha as location
- Stack
from aws_cdk import Stack
Quickstart
import os
from aws_cdk import (
Stack,
App,
Environment,
)
import aws_cdk.aws_location_alpha as location
from constructs import Construct
class MyLocationStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# Create an AWS Location Tracker
tracker = location.Tracker(
self, "MySimpleTracker",
tracker_name="MyExampleTracker",
description="A simple tracker for demonstration"
)
# Optional: Create a Geofence Collection
geofence_collection = location.GeofenceCollection(
self, "MySimpleGeofenceCollection",
geofence_collection_name="MyExampleGeofenceCollection",
description="A simple geofence collection"
)
app = App()
MyLocationStack(app, "MyLocationServiceStack",
env=Environment(
account=os.environ.get("CDK_DEFAULT_ACCOUNT"),
region=os.environ.get("CDK_DEFAULT_REGION"),
)
)
app.synth()