How to deploy MQTT broker using eclipse-mosquitto on amazon ECS

In this blog we are going to see how we deploy a MQTT message 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 as shown

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

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

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

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 

How to add files in EFS for persistent storage in your 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) and EFS will automatically persist your changes by storing it in its file system

How to modify the configuration file for persistent storage

After you have successfully mounted EFS on your container ( Step-4 – Step-7 ) and ( Step-8 – Step-13 ) follow ( Step-14 – Step-16 ) and then follow the following steps

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.

Leave a Reply

Your email address will not be published. Required fields are marked *