Running RASA bot with AWS-EC2

Samrudha Kelkar
tech-that-works
Published in
3 min readJan 23, 2019

--

RASA has been actively developing its NLP stack on top of spacy. If you follow their git then you can relate to it. No doubt the latest changes are awesome!! Rasa-core is now streamlined to run a rasa server, they have decoupled customization actions from the actual bot stack. Actions now can run in a separate server as you might want in production scenarios.

However, it took away developer-loved-control of the bot. Earlier we used to feel things working in front of us which now have been abstracted down. This was expected with the maturity of the platform.

In this blog, we will explore doing simple hosting of rasa core in AWS EC2. Then we will run an old-school flask application on Heroku to talk to the bot.

Step 1] Get your bot working locally

If you already have your rasa bot running, then cool. But if you don’t, do not worry. You can use an awesome starter pack available here. This will build your bot right away.

pip install -r requirements.txt
make train-nlu
make train-core
make cmdline

Try chatting with the bot to ensure it is responding as desired.

Step 2] Launch EC2

This is THE important step why this post exists.

Go to AWS console and click on launch your EC2 instance.

  • Choose EC2 > 16 GB (preferable is 32GB)

Rasa needs TensorFlow, spacy models that have a considerable footprint (~8GB) so I would suggest to use at least 32 GB memory.

  • While spinning EC2 instance, make sure you have exposed port that you want to use for rasa-core (in the below image see port 5000). You need to add a rule for the port number and make access to the public/custom server.

This will enable the outside world to hit your ec2 URL and interact with a bot.

  • Use file mentioned below for installing pip packages

bot needs many modules that do not work off the shelf. I would recommend using this requirements file which I got working after 3+ hours of trial-error with different pip packages and their versions.

pip install -r ec2_requirements.txt

In my opinion, EC2 instance with Ubuntu works best. I faced many challenges with the amazon Linux flavor of CentOS.

One more point to note is to create a virtual environment. You do not want to mess around in the ubuntu’s /user folder for the site-packages. It can create access issues if you want to change something in the package code.

conda create -n botenv python=3.6 anaconda

Note that I choose Python version as 3.6.

Python 3.7 is not supported by Tensorflow. So if you have downloaded the latest anaconda then that will surely have python version≥3.7.

Now run a bot server with the port that you configured. If you are using rasa starter pack then do the following steps

make train-nlu
make train-core
python -m rasa_core.run -d models/current/dialogue -u models/current/nlu --endpoints endpoints.yml --port <port-number>

Trying pinging <ec2url>:<port_number> with JSON { “message”: “hi”} to check working of the bot.

Step 3]

Create your web-based chat server with flask/Django or any other channel which will enable interacting with the rasa core.

Below is the small snippet of python code using requests module that you can use to talk to the bot residing in EC2.

endpoint_url = <ec2url”
port_number = <port_number>
url= endpoint_url + “:” + port_number + “//webhooks/rest/webhook?stream=true&token= HTTP/1.1”
message={“message”: request.form[“text”]}
response = requests.post(url, json=message)
print (“resp is “,response)
response = response.json()
print (response)

--

--