Automatically Sending ROS2 Data to IoT Core with Greengrass
This post is about connecting ROS2 robots to the cloud with a quick and simple Greengrass setup. I'll guide you through building a sample component that can be hosted on a server connected to your robot fleet, which will connect the robots to AWS IoT Core without modifying the robot software in any way. Setup requires installing only a few prerequisites. Let's see how it works!
This post is also available in video form, if you want to follow along:
Methods of Connecting to IoT Core
ROS2 Conversion by Robot
A reliable way of connecting robots to the cloud is to provision a Thing for each robot. In AWS, this means registering a Thing in your AWS account and downloading a certificate for each robot, which is then used for authentication and authorization. I have a video on how to set this up, including sample code that you can run through, available here:
I also have another video which extends this setup by connecting all the robots to a single server with AWS IoT Greengrass installed on it, acting as a single connection point to the public internet. This is a hub-and-spoke model where all robots connect to the Greengrass server and the server connects to the cloud. It has the advantage of being easier to manage remotely, offering the ability to download more Greengrass components to the server, and managing a single connection to AWS IoT Core, which reduces costs. I have a video on setting this up as well:
With this method, the individual robots are responsible for converting any messages destined for the cloud into JSON blocks that could be sent as MQTT messages, which requires coding manually and setting up each robot individually with its own certificate and conversion nodes.
ROS2 Conversion by Greengrass
In this method, the Greengrass server has a component which operates by subscribing to ROS2 topics and converting the messages to MQTT directly. This has two major advantages:
- The robots do not need certificates, conversion nodes, or any custom code to convert topics.
- The Greengrass component can be configured from a central location with the topics it should subscribe to.
In addition, the Greengrass server provisions its own AWS IoT certificate, which means that all the server setup requires is installing Greengrass and Docker for running the components. Everything else can be configured from the cloud!
This method comes with its own downsides, including:
- The Greengrass component does no verification on the device publishing messages on its subscribed topics. That is, if an attacker published messages on the ROS2 network, the Greengrass component would treat them as if they came from a real robot. That means this solution is best used in a closed, protected system, or where the ROS2 network is secured.
- The component only has public messages available to it. If you use custom messages in your ROS2 applications, you will need to build these into the Greengrass component or provide them separately so the component can convert ROS2 messages to JSON correctly.
If these downsides don't sound too detrimental to use in your system, then let's continue! We'll start by building the Greengrass component and publishing it to our AWS account.
Building the Greengrass Component
For this sample, we will build a Greengrass component that runs a Docker Compose application. This is the same setup as in my Deploying Docker Compose with Greengrass post, and uses the same code as a base. We will use the Docker Compose application to run a ROS2-based Docker container that can be configured to subscribe to any ROS2 topic, and it will dynamically import the relevant message class, subscribe to the topic, convert incoming messages to JSON, and send them over local pub/sub to other Greengrass components. By deploying additional components that include an MQTT broker, we can then automatically forward those messages into the cloud.
Overall, this means we have a system where changing the configuration of our component on the Greengrass server will result in all messages from connected robots being converted and sent to the cloud automatically.
In order to build the Greengrass components, we need a system with a few prerequisites installed. I used a fresh EC2 instance to build the components and run the demo system.
System Setup
Any platform that can run Docker and Greengrass will work for this demo. I used Ubuntu 24.04, so feel free to translate the commands to your preferred system. The version of Ubuntu does not matter here, as we can use Docker to run any version of ROS2.
First, before installing any prerequisites, make sure your system is up to date:
sudo apt update && sudo apt upgrade -y
Then, we can proceed to install the build requirements.
AWS CLI
We can follow the user guide for installing the AWS CLI, and the simplest thing to do is execute these commands:
sudo apt install -y unzip
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
We also need to provide AWS permissions to the system. In my case, I assigned an IAM role with admin permissions to my EC2 instance.
Check that the installation is working and has valid credentials by running the following command:
aws sts get-caller-identity
Docker
The simplest way to install Docker is using the Get Docker script, as follows:
curl https://get.docker.com/ | sh
Once installed, make sure your user has permission to run containers as follows:
sudo usermod -aG docker $USER
# To activate in current shell:
newgrp docker
Check that the installation worked by getting responses from the following commands:
docker --version
docker compose --help