![Building an Amazon Location Service Resources with AWS CDK and AWS CloudFormation](https://assets.community.aws/a/2eXjHNAVDikIq1uIYiQvLu7v2Wl/05-a.webp)
Building an Amazon Location Service Resources with AWS CDK and AWS CloudFormation
I will show you how to build Amazon Location Service, which allows you to build location-based applications within your AWS environment using AWS CDK and AWS CloudFormation.
Published Apr 2, 2024
Today, I will show you how to build Amazon Location Service, which allows you to build location-based applications within your AWS environment using AWS Cloud Development Kit (AWS CDK) and AWS CloudFormation. I will also show examples of the recently popular CDK Migrate and AWS CloudFormation IaC generator.
AWS CDK and AWS CloudFormation allow you to code and manage your infrastructure and automate and streamline the development process. AWS CDK uses a developer-friendly programming language to facilitate component reuse. AWS CloudFormation, on the other hand, offers stable resource management and the ability to create templates using JSON and YAML. You can enjoy the benefits of building a coded and reproducible infrastructure by leveraging these tools.
For Amazon Location Service, I will use the Amazon Location SDK and API key functionality released last year to build three functions: mapping, geocoding, and routing. Also, for the "Launch of CloudFormation Support for API Keys and Resource Management" announced this year, I verified the implementation with AWS CDK and AWS CloudFormation.
![](https://assets.community.aws/a/2eXmMGd4WsiYDZOSXxa6B4sCD2L/01-d.webp)
An example is presented in the following flow. You can choose either AWS CDK or AWS CloudFormation to build your resources.
1. Advance Preparation
2. Building an Amazon Location Service Resource: AWS CloudFormation
3. Building an Amazon Location Service Resource: AWS CDK
4. Checking an Amazon Location Service Resource
5. Building an Amazon Location Service Application
6. Introduction to CDK Migrate
7. Introduction to AWS CloudFormation IaC Generator
8. Summary
Install the AWS CDK environment. Installation is not required if you are using AWS CloudFormation.
Verified version at the time of writing
- node v20.0.0
- npm v9.6.4
Install the package
Check the version
First, build Amazon Location Service resources with AWS CloudFormation.
The environment we created is available on GitHub. Please fork or download and use it in your environment.
Deploy the Amazon Location Service resource with AWS CloudFormation using the created template.
AWS Management Console → AWS CloudFormation → "Create Stack".
![](https://assets.community.aws/a/2eXmPM9weQCzWDGPPYp7tNHAMqJ/02-c.webp)
Click on "With New Resources."
![](https://assets.community.aws/a/2eXmRCLAB7AUvpgff9ejM01vpQk/02-c.webp)
For the prerequisite, select "Template is ready." To specify a template, select "Upload a template file" and upload the file → Click "Next".
![](https://assets.community.aws/a/2eXmSzAlfbcrfcOVOgONA6tQNzp/02-c.webp)
Set an arbitrary stack name, API key name, map name, geocoding name, and routing name → Click "Next".
![](https://assets.community.aws/a/2eXmUOhB64eHoynwE8ky71Pfi8G/02-c.webp)
Set the stack options as default this time → Click "Next."
![](https://assets.community.aws/a/2eXmVv8ttcsXOYboB742UNzpT23/02-c.webp)
Confirm settings → Click "Submit".
![](https://assets.community.aws/a/2eXmXQkPJMMDEDrjukDwcl2sjCq/02-c.webp)
After a few moments, you will see that the stack has been created.
![](https://assets.community.aws/a/2eXmYyn1Q0FYaPdNB2VETHTSVh0/02-c.webp)
Next, build Amazon Location Service resources with AWS CDK.
The environment we created is available on GitHub. Please fork or download and use it in your environment.
Deploy Amazon Location Service resources with AWS CDK using the project you have created.
Move the directory
Install the package
Run the following command only the first time before deploying. Run it again when you change the region.
![](https://assets.community.aws/a/2eXmc8BaKDP7uCuZLUfeWoSh0LW/03-c.webp)
Deploy the project
![](https://assets.community.aws/a/2eXmdkFYISpv5tMPlNv2FImC6LH/03-c.webp)
Check if the AWS CDK or AWS CloudFormation deployment is reflected.
AWS Management Console → Amazon Location Service → each resource.
![](https://assets.community.aws/a/2eXmfZbQRn2XQT16ZsqpySurUkO/04-l.webp)
Map, geocoding, and routing settings are reflected. Copy the map name, geocoding name, and routing name for use in the next application build.
![](https://assets.community.aws/a/2eXmhIeXnIEMkCrc90zwmeUgSxr/04-l.webp)
The API key settings are reflected. Copy the region name and API key value for the next application build. When publishing externally, the API key referrer setting is also required.
![](https://assets.community.aws/a/2eXmifHtuYA4ZUZXGLhyDDcPANd/04-l.webp)
At this point, you have completed building the Amazon Location Service environment with AWS CDK or AWS CloudFormation. Next, we will build the Amazon Location Service application.
Use an existing starter to build the Amazon Location Service front-end environment. This starter is configured to make simple use of the map library. You can fork, download, and install it in your environment.
![](https://assets.community.aws/a/2eXml9FMtpGsddJOdyUOTKxPqnj/05-m.webp)
Validated version at the time of writing
- node v20.0.0
- npm v9.6.4
Set the region, API key, and map name of your deployed environment in the env file.
Move the directory
Install the package
Start local server
![](https://assets.community.aws/a/2eXmxbwQR6eYwt563pPfKNxvz7v/05-a.webp)
Next, install the necessary libraries for the Amazon Location SDK. The installation will make it easier to authenticate the API and combine it with MapLibre GL JS.
Install the AWS SDK. "client-location" is an SDK that allows you to manipulate the Amazon Location Service.
Install "amazon-location-utilities-auth-helper," a library that facilitates authentication with Amazon Location Service API keys and Cognito.
Install "amazon-location-utilities-datatypes," a library that converts Amazon Location Service responses to GeoJSON format.
I contributed to "amazon-location-utilities-datatypes" to add an optional feature because it was sometimes difficult to use in combination with MapLibre GL JS!
Finally, I will show how to build the mapping, geocoding, and routing functionality of the Amazon Location Service using API keys.
Set the region, API key, map name, geocoding name, and routing name of the deployed environment in an env file.
Start local server
You will see the map, geocoding, and routing functions of Amazon Location Service.
![](https://assets.community.aws/a/2eXn17Q9FfrL5Ectw3sI9XuunPA/05-a.webp)
At this point, we have completed building the Amazon Location Service application. Next, I will introduce CDK Migrate and IaC Generator.
CDK Migrate can be used to automatically convert AWS CloudFormation templates into AWS CDK projects. This project was also created using CDK Migrate, and most of the code was available. However, this project is a simple configuration, so there may be issues with more complex configurations.
The CDK migrate command converts the AWS CloudFormation template into an AWS CDK project.
![](https://assets.community.aws/a/2eXn4A1jV39A8LH9D8ZHbeDGzxM/06-c.webp)
The AWS CloudFormation IaC generator allows you to create AWS CloudFormation templates and import existing resources. This comes in handy when creating and templating resources in the AWS Management Console. The CloudFormation template we created this time was helpful in some respects but had to be created anew.
AWS Management Console → AWS CloudFormation → Click "IaC Generator" → Click "Start a new scan" → After the scan is complete, click "Create template".
![](https://assets.community.aws/a/2eXn5uwOAfD76vKWlHh2Ht4SWgg/07-I.webp)
Select "Start from a new template" → Set a template name → Click "Next".
![](https://assets.community.aws/a/2eXn7phsiSqFyHi6w3GI4c2nsDE/07-I.webp)
Select the resource you want to make into a template from the scanned resources. In this case, select a resource related to Amazon Location Service → Click "Next".
![](https://assets.community.aws/a/2eXn9I3M4FQGsPeCUGWhLN8qL8I/07-I.webp)
Click "Next.
![](https://assets.community.aws/a/2eXnBOE7rxchFOq8xEdo2IL4eeX/07-I.webp)
Confirm the settings → Click "Create template".
![](https://assets.community.aws/a/2eXnD8yI47H4pikZfaFqf6mukNS/07-I.webp)
A template for the specified resource will be created.
![](https://assets.community.aws/a/2eXnF0phkATMK0tOtjjPWGVceTL/07-I.webp)
CloudFormation support for API keys and resource management in Amazon Location Service is now available, giving you more options for automating and templating your environment builds. I look forward to future updates!
Also, the AWS CDK only provides the Place Index for Amazon Location Service L2 constructs in alpha as of March 2024. So, you will need to define it in the L1 construct.
I hope this example will be helpful to those building location-based applications on AWS!
Unofficially, I distribute monthly updates of Amazon Location Service.
References