
HOW TO DEPLOY ECLIPSE MQTT BROKER USING DOCKER ON AMAZON ECS
In this blog we are going to see how we deploy a MQTT broker which uses eclipse-mosquitto to Amazon ECS, The steps to follow are:
Step-0: Log in to the AWS Management console and navigate to ECS.
If you have a cluster already created you can skip to Step-4
Creating a cluster in Amazon ECS
Step-1: Under ECS select create cluster where you want to deploy your MQTT Broker

Step-3: In cluster configuration choose an EC2 instance key pair (since we would need to ssh into our EC2 instance later on) and an EC2 instance type and hit create

Creating an EFS for mount in ECS task definition for MQTT Broker configurations and logs
Step-4: Go to services and select EFS and select Create file system
Step-5: Select the VPC in which you created your ECS cluster and create and assign a security group which allows NFS traffic from ip of your ECS instance(It is the ec2 instance that got provisioned when your cluster got created)

Step-6: Leave everything else as default and hit create
Step-7: Once the file system is created copy the file system id into your clipboard, This will be used in Step-9

Creating a task definition for your MQTT Broker
Step-8: Go to ECS and select task definition and under that select create new task definition and select ec2 launch type
Step-9: Scroll down to volumes under container definition select add volumes and in file system id paste the EFS file system id we copied in Step-7

Step-10: Scroll up to Container definitions and select add container to add you mqtt broker container definition

Step-11: Configure the container and add the following in the image field and port mappings as shown below

Step-12: Scroll down to Storage and logging and under mount points select the EFS mount point we created in Step-11 and mount it to /mosquitto directory

Step-13: Add the container , Create task definition and launch the task definition for the mqtt broker
How to add files in EFS for persistent storage in your MQTT Broker container
After you follow Step-4 – Step-7 and Step-8 – Step-13 and your container is running successfully, To add files do the following
Step-14: ssh into your ECS instance using the key pair you created in Step-3
Step-15: Run docker ps command and copy the container name as shown

Step-16: Run docker shell using the command “docker exec -it <container_name_here> sh”
Step-17: Modify the files of your choice inside the directory where EFS is mounted (in this case under /mosquitto).
How to modify the configuration file for persistent storage
Step-18: Open mosquitto.conf (which stores the configuration of eclipse-mosquitto) using the command vi /mosquitto/config/mosquitto.conf
Step-19: Add the lines given below at the end of the mosquitto.conf file and save
persistence true
persistence_location /mosquitto/data/
listener 1883
listener 9001
protocol websockets
Step-20: restart the container to see configuration changes to take effect and your eclipse MQTT Broker should now be running