Guides
10 min

How to run a Thalex bot on AWS

We'll show you step-by-step how to get an AWS EC2 virtual machine up & running and deploy a bot.

Thalex X
Published on
Aug 14, 2024
Written by
Hendrik Ghys

Introduction

In this guide we focus on using cloud-based virtual machines from AWS EC2 and show:

  1. How we can instantiate a cloud based virtual machine
  2. How to connect to remotely to an instance
  3. How to deploy a bot on an instance
  4. How to keep the bot running


Step 0 - AWS Account

To create an EC2 instance, you'll need an AWS account.

We expect you can create an AWS account on your own.

The VM we'll be using is in Ireland (as Thalex is in Ireland) and is free for a while, then it costs $0.0116/hr at the time of writing.

server-choice.png

If your computation demand grows in the future, you can always launch a faster instance instead.

You can check up-to-date prices here


Step 1 - Create an SSH Key

After creating an account, click on “Sign In to the Console”.

sign-in.png

You'll now need to create a key pair for your EC2 instance:

Click on "Services" top-left

Then "Compute" -> "EC2"

Look for "Network & Security" to find "Key Pairs"

Click "Create key pair"

services-compute.png
key-pairs.png

You can name your key to whatever you like.

key-create.png

We'll be using "my-ssh-key" for the sake of this tutorial.

Download the ssh key if prompted.

You'll need it to securely connect to the instance from your local machine.


Step 2 - Launch an instance

With the ssh key created, we can proceed to creating an instance:

  • Click on "Instances" under the Instances dropdown (lhs)
  • Click on "Launch instances"
  • Choose "t2.micro" for the instance type
  • Choose "Ubuntu" as the OS
  • Select "Ubuntu Server 24.04 LTS" for the Amazon Machine Image

launch-instances.png
instance-image.png

Scrolling down:

  • For ssh key, choose the one you created in step 1
  • Make sure "Allow SSH traffic from" is checked
  • The source of allowed traffic should contain your IP address
  • You can toggle "My IP" (safer) or "Anywhere" (might be easier)

ssh-ip.png

You should be ready to launch now.

Scroll to the bottom and click "Launch Instance".

summary-instance.png

This should only take a few minutes...


Step 3 - SSH into the instance

To do useful things with our instance we need to be able to securely connect to it from our local machine.

For this, we need two things:

  1. The ssh key we created in step 1
  2. The public address (‘DNS’) of the EC2 instance

We can find the public DNS in the summary of the instance:

  • Bring up the summary by clicking the checkbox next to the instance name
  • Alternatively, click on “Instances” and click on your instance under “Instance ID”

Copy the “Public IPv4 DNS” within the summary:

instances.png
instance-summary.png

Fire up your favourite terminal and issue the following command to set access rights for ssh:

chmod 400 /path/to/your/ssh-key.pem/from/step-1

Let's ssh into the instance by typing into your terminal:

ssh -i /path/to/your/ssh-key.pem/from/step-1 ubuntu@instance-public-IPv4-dns

It is also possible to find the above-mentioned ssh-command in copy-pastable format:

On the AWS "Instances" page, select the running instance.

Click "Connect" and then select the "SSH client" tab.

Connect-instance.png

Troubleshooting tips:

  • Typos get punished. Use the aws-suggested ssh line if you encounter errors
  • If your ssh key is in a different folder, you'll need to specify the relative path to the key (e.g. "../aws/my-ssh-key.pem")
  • The public IPv4 DNS of the instance changes every time you restart the instance

If you're on Windows:

  • Many of the above commands won't work in command prompt / powershell
  • Check out Cmder | Console Emulator to get a better terminal experience

Step 4 - Install & launch tmux

Tmux is a terminal multiplexer.

  • It supports creating & managing multiple terminal sessions from a single window.
  • tmux can keep ssh sessions running on the instance, even if we disconnect locally
  • This is crucial to be able to keep our bots running 24/7

There's a good chance ubuntu will come with it preinstalled.

Just to be sure, you can run:

sudo apt install -y tmux
tmux 
tmuxwin # for windows

Step 5 - Clone the thalex_py repository

Navigate to a folder of choice on your local machine to clone the thalex_repo

If you clone via http:

git clone https://github.com/thalextech/thalex_py.git
cd thalex_py

Step 6 - Create API Keys

Log into Thalex on testnet:

  • Top-right check your subaccount. If you have multiple subaccounts, select the account you want to use for API trading
  • On the user icon right next to it, click on the "Account" dropdown
  • Navigate to "API"
  • Proceed to "Create API Key"
  • Give a name for the API Key e.g. "bot-runner-aws"
  • Select the "Allow Trading" permission and then save

testnet_select_api_dropdown.png
image.png

Make sure you save the private key, you won't be able to do it later.

For production, you'll need a separate API key. You can create it via the same process.


Step 7 - Add your Keys

Now we go back to the terminal that is running the remote session.

Copy the content of _keys.py, which is a template, to keys.py, which is where we'll insert our API key(s).

cp _keys.py keys.py

Now open keys.py with a text editor. We show an example using vim (with default keybindings) as this is directly available.

vim keys.py 

Once you are in the file, press

  • 'i' to edit the text file and paste the relevant private keys and their respective key ids
  • If you only want to use testnet, you can leave prod empty and vice-versa.
  • The esc button once you are finished editing
  • Save the file and exit the editor by pressing ':x' and pressing enter

As an alternative to, you can:

  • create the keys.py locally on your machine (i.e. not on AWS)
  • 'scp' (secure copy) the keys to the instance

For example, to copy keys.py from local to the /tmp directory on your instance:

scp -i ~/path/to/your/ssh-key.pem keys.py ubuntu@instance-public-IPv4-dns:/tmp

Back in the ssh session, you can move keys.py to the destination folder of choice.

Here we show the command if you move to a remote folder called "thalex_py":

mv /tmp/keys.py /thalex_py/

Or, to move keys.py to your home directory:

mv /tmp/keys.py $HOME

Step 8 - Install Requirements

Ubuntu doesn't come with everything we need. We'll need to install a few things.

We'll create a virtual python environment and install project dependencies.

sudo apt update
sudo apt install -y python3-pip python3.12-venv
python3 -m venv my_venv
source my_venv/bin/activate
pip install -e ./

To test everything was installed correctly, try running the instrument counter script.

The output should look something like this.

python examples/instrument_counter.py
        Perpetuals: 2
        Futures: 18
        Options: 452
        Rolls: 30

Make sure you're in the virtual python environment (venv) to run/execute the python scripts.

You should see "my_venv" in your terminal (where ".venv" is below):

After exiting and re-entering the terminal, don't forget to reactivate the python environment:

source my_venv/bin/activate

Step 9 - Fire Up Your Bot

By now you should be ready to try out example scripts from the thalex repo.

For example, let's fire up a bot that engages in synthetic futures arbitrage:

python examples/synth_arbitrage.py

Once you see the trading bot running:

  • Detach the tmux session by pressing Ctrl+b, followed by d
  • After detaching the session, it will continue to run on AWS
  • You no longer need to be connected to the instance locally
  • You can close the ssh session with Ctrl+d

If you don't detach, exiting the ssh session from your local machine will terminate the session.


Step 10 - Reconnect to the Bot

If you want to shut down, restart, or edit your bot, you have to attach back to the session.

Then type tmux attach in the terminal after being ssh'd in:

tmux attach

You can shut down the bot with Ctrl+C and do with it as you please.


Step 11 - Clean Up

Don't forget to pause, stop or terminate your EC2 instance to avoid unnecessary costs.

You can do this from the AWS "Instances" page:

stop-instance.png

That should be it. Hope this was helpful.







Disclaimer

Thalex may provide you with certain educational and software tools that you may use for your benefit, at your own risk, and for your own account. Thalex disclaims any responsibility for the use, reliance or utilization of the software, and you agree without limitation to hold Thalex harmless and wholly indemnified of your conscious use of the software, for whatever purposes you may have, related or unrelated with the activities on Thalex or any other service provider that caters for the use of the software, or similar software. Use of the software presupposes acceptance of these conditionals.

From the blog

The latest industry news, interviews, technologies, and resources.
View all posts