---
title: Deploy HiveMQ MQTT Broker with Amazon Elastic Container Service (ECS) Anywhere
description: "A guide to deploying HiveMQ MQTT broker using Amazon ECS Anywhere so you can harness the full potential of IoT applications across diverse environments. "
date: 2024-09-04
published: 2024-09-04
author: Anthony Olazabal
tags:
  - AWS
  - HiveMQ Broker
source: https://www.hivemq.com/blog/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere
---

# Deploy HiveMQ MQTT Broker with Amazon Elastic Container Service (ECS) Anywhere

Deploying a [HiveMQ Enterprise broker](/products/mqtt-broker) with Amazon Elastic Container Service (ECS) Anywhere opens up powerful possibilities for managing and scaling IoT infrastructures. With the growing demand for reliable and scalable [MQTT brokers](/blog/mqtt-brokers-beginners-guide), HiveMQ's enterprise-grade solution offers the robust performance and advanced features necessary for today's IoT ecosystems. By leveraging Amazon ECS Anywhere, you can deploy and manage your HiveMQ brokers beyond the cloud, extending into on-premises environments and edge locations with the same ease and consistency as cloud-native deployments.

In this guide, we will walk you through the step-by-step process of deploying HiveMQ Enterprise MQTT broker using Amazon ECS Anywhere, enabling you to harness the full potential of your IoT applications across diverse environments. Whether you're new to ECS Anywhere or a seasoned cloud professional, this tutorial will provide you with practical insights and best practices to ensure a seamless and efficient deployment. Let's dive in!

## Architecture Overview of Deploying MQTT Broker with Amazon ECS Anywhere 

![Architecture Overview of Deploying MQTT Broker with Amazon ECS Anywhere](https://a.storyblok.com/f/243938/1978x1056/735665bbde/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-1.webp)

The global architecture is pretty simple. We try to avoid at maximum dependencies with external resources outside of the on-premises / edge site. So we put in place the following components:

- A [Minio S3](https://min.io/) storage (in this sample architecture it’s a single node, but you can build a local cluster shared between the ECS Instances)
- The HiveMQ Enterprise cluster with the [S3 Cluster discovery extension](https://www.hivemq.com/products/extensions/s3-cluster-discovery-extension/) that uses the Minio S3 storage as a repository to store the state and members of the cluster
- A custom image of HiveMQ Enterprise broker that includes our extensions and configurations
- [Traefik](https://traefik.io/traefik/) stateless containers to load balance the HTTP and MQTT traffic between nodes.
- [Docker desktop](https://www.docker.com/products/docker-desktop/) installed on your local machine to build a custom docker image

### Prerequisites

To start the configuration we assume that you have the following already installed:

- An [AWS Account](https://signin.aws.amazon.com/signin?redirect_uri=https%3A%2F%2Fportal.aws.amazon.com%2Fbilling%2Fsignup%2Fresume\&client_id=signup) with enough permissions to create ressources
- The [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
- An [Amazon Elastic Container Service](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-cluster-console-v2.html) Cluster already configured with at least two external instances. If needed, follow the instructions as described [here](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-anywhere-registration.html) in the Amazon ECS product documentation.
- Knowledge on AWS IAM management

## Configure Amazon IAM 

To anticipate the permission needs for Traefik to read information from the services deployed on the External Instances of ECS, we prepare a permission policy with the following properties:

![Configure Amazon IAM](https://a.storyblok.com/f/243938/1172x678/442361811d/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-2.webp)

Click ***Next***, in the next creation form page, name the policy, and add a description. Then click ***Create policy***.

For the last step on Amazon IAM, you create a role in the Console like below:

Create a new role selecting the *Elastic Container Service Task* as use case.

![Create a new role selecting the Elastic Container Service Task as use case](https://a.storyblok.com/f/243938/1167x813/235d863cc7/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-3.webp)

Then select the Permissions policy previously created “*TreafikECSReadAccess.*”

![Permissions policy](https://a.storyblok.com/f/243938/1533x445/9f800dc401/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-4.webp)

Once created, note down the ARN of the role to be able to update the Task deployment template later in the configuration.

For example, here we have the ARN: `arn:aws:iam::5623627372734:role/ECSTasksTraefik`

## Create Minio Deployment Task Template

On the AWS Console, go to Amazon Elastic Container Service in the *Task definitions* menu and click on “***Create new task definition with JSON.***”

![Creating new task definition on Amazon ECS Anywhere service](https://a.storyblok.com/f/243938/1861x291/c67d624932/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-5.webp)

Copy and paste the definitions to prepare the deployment task.

Change the value for the following properties to reflect your configuration:

- MINIO\_ROOT\_PASSWORD
- MINIO\_ROOT\_USER
- MINIO\_VOLUMES

## Deploy Minio

Now that our templates are ready, we can create the services on our ECS Cluster.

To do so, go to the AWS Console in Amazon Elastic Container Service, your cluster, and click on ***Create*** in the ***Services*** section.

![Deploy Minio on Amazon ECS Anywhere](https://a.storyblok.com/f/243938/1861x657/9cd88a5ac4/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-6.webp)

On the creation form, select the ***External*** launch type:

![Deploy Minio on Amazon ECS Anywhere](https://a.storyblok.com/f/243938/1093x532/2c996a3abe/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-7.webp)

In the deployment configuration section, name the task and select the task definition for Minio that we’ve previously created:

![Deploy Minio on Amazon ECS Anywhere](https://a.storyblok.com/f/243938/1093x734/365e68d979/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-8.webp)

Click ***Create*** and wait for the deployment to complete.

![Deploy Minio on Amazon ECS Anywhere](https://a.storyblok.com/f/243938/1281x42/522eccaf79/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-9.webp)

### Configure Minio

In order to use the S3 Bucket for our cluster, we need to set up the Minio instance.

Access your local console with `http://&lt;service Uri&gt;:9001`.

![Configure Minio](https://a.storyblok.com/f/243938/1828x893/046b20ad16/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-10.webp)

Log in with your defined credentials.

Once in, create a new bucket in the object browser.

![Create bucket on Minio Object Store](https://a.storyblok.com/f/243938/1002x385/392b28aa25/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-11.webp)

Take note of the Bucket Name.

The last step is to configure an access key in the dedicated menu “***Access Keys***.”

![Create Access Key on Minio Object Store](https://a.storyblok.com/f/243938/1002x556/bd0f0c9c41/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-12.webp)

Keep the Access Key and the Secret Key in your notes for the configuration of the S3 discovery extension.

## Build a HiveMQ Enterprise MQTT Broker Custom Image

In order to deploy the cluster seamlessly, we need to create a custom image of HiveMQ Enterprise MQTT broker to embed the S3 extension and the configuration files.

We will start by creating our configuration files:

config.xml

s3discovery.properties

In this file, you need to adjust the credentials-access-key-id, credentials-secret-access-key and the s3-endpoint to your S3 storage with the values defined in the Minio S3 configuration.

We will also embed in our image a “startup script" that will update our broker configuration file with the local IP address of the container defined as local cluster transport endpoint.

pre-entry.sh

We then use the following Dockerfile to build our custom image:

You can update the HiveMQ image version and S3 Discovery extension to align to the latest one when you build the image.

Build the image with the following command:

*Note: Following the build, you need to deploy the custom image in a repository that will be accessible from your ESC Instances. This could be for tests, docker hub, or other services like Amazon Container Registry in production.*

## Create HiveMQ Deployment Task Template

On the AWS Console, go to Amazon Elastic Container Service in the Task definitions menu and click on “Create new task definition with JSON”.

![Creating new task definition on Amazon ECS Anywhere service](https://a.storyblok.com/f/243938/1861x291/c67d624932/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-5.webp)

Copy and paste the definitions to prepare the deployment task.

Change the following values to reflect your configuration:

- Image link
- HIVEMQ\_CONTROL\_CENTER\_PASSWORD
- HIVEMQ\_CONTROL\_CENTER\_USER

In the docker labels, you will need to align the names with your desired Traefik configuration. In our case, we have Traefik configuration with a TCP entrypoint called MQTT and an HTTP entrypoint called WEB.

On the container deployment, we declare an HTTP service called hivemq-cc with the following properties:

> *"traefik.http.services.hivemq-cc.loadbalancer.server.port": "8080",* >
> *"traefik.http.routers.hivemq-cc.entrypoints": "web",*>
> *"traefik.http.routers.hivemq-cc.rule": "Host(`cc.hivemq.local`) && PathPrefix(`/`)",*>
> *"traefik.http.services.hivemq-cc.loadBalancer.sticky.cookie.name": "hivemqcc",*>
> *"traefik.http.services.hivemq-cc.loadBalancer.sticky.cookie": "true"*

We declare a second service called router-broker-mqtt relying on TCP with the following properties:

> *"traefik.tcp.routers.router-broker-mqtt.entrypoints": "mqtt",*>
> *"traefik.tcp.routers.router-broker-mqtt.service": "service-broker-mqtt",*>
> *"traefik.tcp.services.service-broker-mqtt.loadbalancer.server.port": "1883",*>
> *"traefik.tcp.routers.router-broker-mqtt.rule": "HostSNI(`*`)",*

## Deploy HiveMQ

Go to the AWS Console in Amazon Elastic Container Service, your cluster, and click on ***Create*** in the ***Services*** section.

![Deploy Minio on Amazon ECS Anywhere](https://a.storyblok.com/f/243938/1861x657/9cd88a5ac4/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-6.webp)

On the creation form, select the External launch type:

![Deploy Minio on Amazon ECS Anywhere](https://a.storyblok.com/f/243938/1093x532/2c996a3abe/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-7.webp)

In the deployment configuration section, name the task, select the task definition for HiveMQ, and define the number of tasks you want to deploy:

![Deploy HiveMQ](https://a.storyblok.com/f/243938/1093x750/11f57473ab/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-20.webp)

Click ***Create*** and wait for the deployment to complete.

![Deploy HiveMQ on Amazon ECS Anywhere](https://a.storyblok.com/f/243938/1320x37/663316aef8/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-13.webp)

If everything went well, you should be able to access the Control Center via `http://&lt;ip` of local ECS instance where hivemq is `deployed&gt;:8080` and see two nodes in your control center:

![Deploy HiveMQ on Amazon ECS Anywhere](https://a.storyblok.com/f/243938/1034x356/5b4c9c9029/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-14.webp)

Let’s move to the final step to deploy Traefik to balance the HTTP traffic for the Control Center and also the [MQTT](/blog/how-to-get-started-with-mqtt) traffic.

## Create Traefik Deployment Task Template

On the AWS Console, go to Amazon Elastic Container Service in the Task definitions menu and click on “Create new task definition with JSON”.

![Creating new task definition on Amazon ECS Anywhere service](https://a.storyblok.com/f/243938/1861x291/c67d624932/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-5.webp)

Copy and paste the definitions to prepare the deployment task.

Change the following values to reflect your configuration:

- \--providers.ecs.region
- taskRoleArn (This is where you paste the value from the IAM configuration done earlier)

*Note: The configured MQTT port is 1884 because the 1883 on the host (ECS External instance) is already used by the HiveMQ nodes.*

## Deploy Traefik

Go to the AWS Console in Amazon Elastic Container Service, your cluster, and click on Create in the Services section.

![Deploy Minio on Amazon ECS Anywhere](https://a.storyblok.com/f/243938/1861x657/9cd88a5ac4/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-6.webp)

On the creation form, select the External launch type:

![Deploy Minio on Amazon ECS Anywhere](https://a.storyblok.com/f/243938/1093x532/2c996a3abe/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-7.webp)

In the deployment configuration section, name the task, select the task definition for Traefik (LoadBalancer) and define the number of tasks you want to deploy:

![](https://a.storyblok.com/f/243938/1095x752/6f627f2ce9/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-15.webp)

Click Create and wait for the deployment to complete.

![Deploy Traefik](https://a.storyblok.com/f/243938/1140x39/4404ce3055/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-16.webp)

Once the task has been completed, you should be able to access the Traefik dashboard via `http://&lt;ip `of ECS instance where Traefik is `deployed&gt;:8888/dashboard/#`.

![Deploy Traefik](https://a.storyblok.com/f/243938/1448x726/b71e5b24f3/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-17.webp)

You can see that our services defined by docker labels are automatically detected.

The same happens for TCP services for MQTT connections.

![Deploy Traefik](https://a.storyblok.com/f/243938/1448x387/775f9f1fcb/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-18.webp)

Our backend is also dynamically detected:

![](https://a.storyblok.com/f/243938/956x583/2a6a2933fd/deploy-hivemq-mqtt-broker-with-amazon-ecs-anywhere-image-19.webp)

## Wrap Up

Deploying the HiveMQ Enterprise MQTT broker with Amazon Elastic Container Service Anywhere (ECS A) empowers your IoT infrastructure with unmatched flexibility, scalability, and control. By following the steps outlined in this guide, you've taken a significant leap toward optimizing your MQTT deployments across cloud, on-premises, and edge environments.

With ECS Anywhere, you can now manage your HiveMQ brokers consistently, regardless of location, ensuring robust performance and reliability for your IoT applications. This deployment approach not only simplifies operations but also enhances the ability to respond to evolving business needs with agility and precision.

As always in our labs, configurations are simplified to get straight to the point. You'll notice, for example, that Minio's deployment can be enhanced to automatically define a default bucket and access keys. You can also customize your HiveMQ image by integrating other extensions and configurations to connect even more services.

If you're starting your IoT/IIoT project with MQTT and Amazon Web Services, consider giving [HiveMQ](https://app.hivemq.com/signup?target=software) a try or request a demo.

[Request Demo](/contact)
