Skip to content

BYOC on AWS ​

Recommended: ECS on Fargate behind a Network Load Balancer that terminates TLS with an ACM certificate. For a small setup, a single EC2 instance with Docker also works. Read the BYOC overview first.

Option A: ECS on Fargate + Network Load Balancer ​

Store the secret ​

bash
aws secretsmanager create-secret --name connectmq/broker-secret --secret-string '<your-secret>'

Register a task definition ​

task-def.json (use "cpuArchitecture": "ARM64" for Graviton):

json
{
  "family": "connectmq-broker",
  "requiresCompatibilities": ["FARGATE"],
  "networkMode": "awsvpc",
  "cpu": "1024",
  "memory": "2048",
  "runtimePlatform": { "operatingSystemFamily": "LINUX", "cpuArchitecture": "X86_64" },
  "executionRoleArn": "arn:aws:iam::<ACCOUNT_ID>:role/ecsTaskExecutionRole",
  "containerDefinitions": [{
    "name": "broker",
    "image": "connectmqcom/broker:1.0.0",
    "essential": true,
    "portMappings": [
      { "containerPort": 1883, "protocol": "tcp" },
      { "containerPort": 8083, "protocol": "tcp" }
    ],
    "environment": [
      { "name": "BROKER_ID", "value": "my-broker-01" },
      { "name": "PUBLIC_BROKER_ENABLED", "value": "false" }
    ],
    "secrets": [
      { "name": "BROKER_SECRET_KEY",
        "valueFrom": "arn:aws:secretsmanager:<REGION>:<ACCOUNT_ID>:secret:connectmq/broker-secret" }
    ],
    "logConfiguration": {
      "logDriver": "awslogs",
      "options": {
        "awslogs-group": "/ecs/connectmq-broker",
        "awslogs-region": "<REGION>",
        "awslogs-stream-prefix": "broker",
        "awslogs-create-group": "true"
      }
    }
  }]
}
bash
aws ecs create-cluster --cluster-name connectmq
aws ecs register-task-definition --cli-input-json file://task-def.json

The execution role needs secretsmanager:GetSecretValue on the secret.

Create a Network Load Balancer ​

Use ip target groups; the NLB terminates TLS with an ACM certificate:

NLB listenerProtocolTarget group (container port)
1883 (optional, private only)TCPtg-mqtt → 1883
8883TLS (ACM certificate)tg-mqtt → 1883
8084TLS (ACM certificate)tg-ws → 8083
bash
aws elbv2 create-target-group --name tg-mqtt --protocol TCP --port 1883 \
  --target-type ip --vpc-id <VPC_ID> --health-check-protocol TCP
# keep the real client IP (quotas and bans depend on it)
aws elbv2 modify-target-group-attributes --target-group-arn <TG_MQTT_ARN> \
  --attributes Key=preserve_client_ip.enabled,Value=true

Do the same for tg-ws on port 8083.

Create the service with exactly one task ​

bash
aws ecs create-service --cluster connectmq --service-name connectmq-broker \
  --task-definition connectmq-broker --desired-count 1 --launch-type FARGATE \
  --deployment-configuration minimumHealthyPercent=0,maximumPercent=100 \
  --network-configuration "awsvpcConfiguration={subnets=[<SUBNET_A>,<SUBNET_B>],securityGroups=[<SG_ID>],assignPublicIp=ENABLED}" \
  --load-balancers "targetGroupArn=<TG_MQTT_ARN>,containerName=broker,containerPort=1883" \
                   "targetGroupArn=<TG_WS_ARN>,containerName=broker,containerPort=8083"

minimumHealthyPercent=0 makes sure two tasks with the same Broker ID never run at the same time during a deployment.

Security group: allow inbound 1883/8083 from the internet (client IPs are preserved, so traffic arrives from client addresses) or only from your device CIDRs, and outbound 443. In private subnets, add a NAT gateway so tasks can reach brokerapi.connectmq.com.

DNS ​

Create a Route 53 alias mqtt.example.com → NLB. Clients connect to mqtts://mqtt.example.com:8883 or wss://mqtt.example.com:8084/mqtt.

Option B: a single EC2 instance ​

Launch Amazon Linux 2023 (x86 or Graviton), install Docker and follow Docker / Docker Compose. Attach an Elastic IP and open ports 1883/8883/8083/8084 in the security group.

bash
sudo dnf install -y docker && sudo systemctl enable --now docker

Not supported on AWS ​

App Runner and Lightsail container services (HTTP only), and the Application Load Balancer (layer 7).

Next: Verify, update & troubleshoot.

Fully managed MQTT broker for connected devices.