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.
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".
Click on "With New Resources."
For the prerequisite, select "Template is ready." To specify a template, select "Upload a template file" and upload the file → Click "Next".
Set an arbitrary stack name, API key name, map name, geocoding name, and routing name → Click "Next".
Set the stack options as default this time → Click "Next."
Confirm settings → Click "Submit".
After a few moments, you will see that the stack has been created.
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.
Deploy the project
Check if the AWS CDK or AWS CloudFormation deployment is reflected.
AWS Management Console → Amazon Location Service → each resource.
Map, geocoding, and routing settings are reflected. Copy the map name, geocoding name, and routing name for use in the next application build.
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.
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.
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
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.
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.
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".
Select "Start from a new template" → Set a template name → Click "Next".
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".
Click "Next.
Confirm the settings → Click "Create template".
A template for the specified resource will be created.
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