Deploying RASA on AWS EC2 and using the model stored on S3.

Simran Kaur Kahlon
Gray Matrix
Published in
3 min readAug 8, 2020
Image Source — rasa.com

Rasa is an open-source machine learning framework for automated text and voice-based conversations. It is used to build chatbots that can be integrated on the Web, WhatsApp, and any other platform.

  1. It's quick and easy to do Rasa setup on EC2, the below steps could help you. I am assuming you already have an EC2 created. If not please follow the steps given in the below link :

https://docs.aws.amazon.com/efs/latest/ug/gs-step-one-create-ec2-resources.html

2. I am using ubuntu 18.04 (t2.micro). Once we have the EC2 setup done and have installed python and pip, we can create a python virtual environment by using the following command:

python3 -m venv <environment name>

If you don’t have venv installed you can install so by following the below link:

https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

3. After creating the virtual environment we need to activate it as:

source <environment name>/bin/activate

4. I have listed all rasa requirements in “requirements.txt” file and will run it as:

pip3 install -r requirements.txt

5. So now, VM is in place with all the needed packages and libraries. We can clone our bot and train the model on the VM as:

rasa train

Make sure that your environment is still active.

6. Once the model is trained, we can run the Core, Action, and NLG servers with the following commands. I prefer to add them to a CRON on VM reboot as,

crontab -e

  • Core Command

@reboot cd /home/ubuntu/demo_bot && /home/ubuntu/demo_bot/venv/bin/python3 -m rasa run -m models — endpoints endpoints.yml -p 5006 — enable-api — credentials credentials.yml — log-file /home/ubuntu/demo_bot/logs/rasa_core-`/bin/date +\%Y-\%m-\%d-\%H-\%M-\%H-\%M-\%S`.log — debug

  • Action Command

@reboot cd /home/ubuntu/demo_bot && /home/ubuntu/demo_bot/venv/bin/python3 -m rasa run actions — actions actions -p 5055 — debug >> /home/ubuntu/demo_bot/logs/rasa_action-`/bin/date +\%Y-\%m-\%d-\%H-\%M-\%S`.log 2>&1

  • NLG Command

@reboot cd /home/ubuntu/demo_bot && /home/ubuntu/demo_bot/venv/bin/python3 nlg_server.py -d domain.yml &

venv is my virtual environment.

Make sure you create a logs folder in your project directory, to dump your core and action output for debugging purposes.

Using AWS Cloud Storage in RASA.

Rasa supports using S3 to save your models.

To use models stored on S3 you need to do the following:

  1. You will need the boto3 module, which can be installed as -

pip install boto

2. Next, you need to set the following environment variables :

AWS_SECRET_ACCESS_KEY

AWS_ACCESS_KEY_ID

AWS_DEFAULT_REGION

BUCKET_NAME

AWS_ENDPOINT_URL

3. On Linux, Mac you can set them as :

export AWS_ACCESS_KEY_ID=XXXXXXXXXXXXX

Likewise, you can set the other variables.

4. Now, you need to start your rasa server with the “remote-storage” option set to “aws”, and specify the model name stored in S3 as follows:

rasa run -m model-name — enable-api — log-file out.log — remote-storage aws

Rasa server will pick up the model specified from the S3 bucket.

5. You can also create the following script to run at reboot, which picks the model from S3.

import os

from env import *

## Command to run core server

server_command = “export AWS_ACCESS_KEY_ID=” + AWS_ACCESS_KEY_ID + “ && export AWS_SECRET_ACCESS_KEY=” + AWS_SECRET_ACCESS_KEY + “ && export AWS_DEFAULT_REGION=” + AWS_DEFAULT_REGION + “ && export BUCKET_NAME=” + BUCKET_NAME +” && export AWS_ENDPOINT_URL=”+AWS_ENDPOINT_URL +” && python3 -m rasa run -m demo_bot.tar.gz — endpoints endpoints.yml -p 5006 — enable-api — credentials credentials.yml — log-file logs/rasa_core-`/bin/date +\%Y-\%m-\%d-\%H-\%M-\%H-\%M-\%S`.log — debug — remote-storage aws — debug — cors \”*\””

os.system(server_command)

6. Run the above script on reboot as,

@reboot cd /home/ubuntu/demo_bot/ && /home/ubuntu/demo_bot/venv/bin/python3 core_script.py

That's it, and your bot is up and running on EC2.

Please get in touch in case of any queries.

Thanks.

--

--

Simran Kaur Kahlon
Gray Matrix

JS/ Laravel / AWS / Chatbot Developer #AWS Solution Architect Associate #AWS Developer Associate