Deploying Your Rust Apps in the Cloud
Tags: aws, cloud, telecoms, webdev
Published: 2022-03-04 05:13:48

## Introduction
In this article, I provide a step-by-step guide on how you can easily deploy your Rust application in the cloud with [Amazon AWS EC2](https://aws.amazon.com/ec2/). Depending on your system requirements, you can deploy your application in the cloud to save you the hassle of managing the hardware infrastructure yourself so you can focus on what matters - your application and the customers they serve.

## Prerequisites
I am assuming you already have an AWS account, if not please create one and login as root user. We will be using the AWS EC2 Free Tier _t3.micro_ compute, as our virtual server in the cloud.

 
## Step 1
After logging in as root to your AWS account, switch to the new view and nagivate to EC2 as shown in the screenshot below. Select `Services`, `Compute` and then click on `EC2`.
![](https://i.imgur.com/Y1pffNX.png)

## Step 2
Click on `Launch instances` as shown below.
![](https://i.imgur.com/40nqhzZ.png)


## Step 3
Under choose AMI, first check the `Free tier only` box on the left side of the window and then select any image. I selected `Ubuntu Server 20.04 LTS` image. You can experiment with any of the images.
![](https://i.imgur.com/kD3zs7B.png)


## Step 4
Make sure you select the t3.micro (it is the `free tier` instance type) and then click on `Review and Launch` then `Launch`. This will display a window asking you to select or create a key pair. Select the applicable option and then click on `Launch Instance`  
![](https://i.imgur.com/2b2UzFh.png)

## Step 5
Give your virtual server a name. Mine is named `server-01`. Wait for your server to change `Instance state` to `Running` before you connect to it.
![](https://i.imgur.com/SE9bQwx.png)


## Step 6
Now, you can connect to your virtual server using [PuTTy](https://www.putty.org/), Powershell, Bash (if you are using Linux), CMD or any client application that supports `SSH`. I am using PuTTy. To connect to the `compute` or virtual server, ssh to the `Public IPv4 DNS` address specified under the `Details` tab.
![](https://i.imgur.com/U4QAbAw.png)

## Step 7
The user name is `ubuntu`. The virtual server has access to the internet and you can install any package you need. Now, let's install the Rust compiler.
![](https://i.imgur.com/0yt7Dy8.png)

## Step 8
Install Rust and `buid-essential` (sudo apt install build-essential).
![](https://i.imgur.com/1i1tawo.png)

## Step 9
We will be deploying two types of application on the virtual server - a telecoms application (a [GTPv2-C](https://en.wikipedia.org/wiki/GPRS_Tunnelling_Protocol) server) and a web application based on the [warp](https://github.com/seanmonstar/warp) crate. GTPv2C protocol is used in the [3GPP](https://www.3gpp.org/) based 3G, 4G and 5G networks for signaling and it uses port 2123 and the [UDP](https://en.wikipedia.org/wiki/User_Datagram_Protocol) transport protocol. We will configure our Rust GTP server to listen on UDP port 2123 in compilance with the [3GPP TS 29.274](https://www.arib.or.jp/english/html/overview/doc/STD-T63v9_60/5_Appendix/Rel8/29/29274-8b0.pdf) (GTPv2-C) standard.


The web server will listen on TCP port 3030.

Configure the `Inbound rules` as shown below. AWS EC2 will apply these rules to our virtual server and it will allow our Rust applications to send and receive messages using these rules.
![](https://i.imgur.com/sBnezgP.png)


## Step 10

Create your web server application using `cargo new web_server` and edit with nano as shown below. The original code is available here:https://github.com/seanmonstar/warp and it has been slightly modified for this tutorial. Run the web server with `cargo run`.

After this step, you will have two Rust applications running in the cloud. You can use the `ps aux | grep target` to check the two processes running (`gtp` and `web_server`)

![](https://i.imgur.com/rP0uJb2.png)


## Step 11
Open another PuTTy session and connect to the virtual server. Clone the GTPv2-C server from  https://github.com/eshikafe/ngc and edit the `ngc/common/gtp/src/main.rs` so that the server listens and accepts conections from any IP address (i.e `0.0.0.0`) as shown below. Run the application with `cargo run`

![](https://i.imgur.com/bS1uYtr.png)

In the screen shot below, a simple `Python` script (the client) sends a message to the gtp server application using the `Public IP address` and port 2123. The script sends a GTPv2-C `Create Session Request` message from my Windows PC, through the internet, to the Rust GTPv2-C server application running in the cloud. This implies that you can deploy a telecom application (based on Rust) in the cloud to serve multiple devices concurrently and efficiently. The sample gtp application uses the [tokio](https://tokio.rs/) crate to achieve concurrency. Concurrency is a key requirement for a 4G/5G telecom server application.
![](https://i.imgur.com/3RaFT6J.png)


## Step 12
Lastly, open a web browser and paste the following URL in the address bar:`{Public IPv4 DNS}:3030/hello/Rustacean`. Replace `Public IPv4 DNS` with the domain assigned to your compute. You should see the message below.
![](https://i.imgur.com/qIlyhO4.png)

You can also use curl to test your web server as shown below.
![](https://i.imgur.com/SfZwGE3.png)

To deploy your applications securely in the cloud, you need to understand how the [AWS EC2 security group](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-security-groups.html) works. Please read the AWS EC2 security group documentation before you deploy your application as a production system in the cloud.

## Reference
[AWS Tutorial for Beginners - Full Crash Course | Neal Davis](https://www.youtube.com/watch?v=7mgBvbqUVgw&t=1267s)