Building Rust Applications For AWS Graviton
Learn how to migrate a Rust application from x86 based Amazon EC2 Instances to ARM64-based Graviton EC2 Instances in order to achieve both higher application sustainability and lower costs
- How to build a Rust application for AWS Graviton
- How to port an existing Rust application to AWS Graviton
About | |
---|---|
✅ AWS Level | 200 - Intermediate |
⏱ Time to complete | 30 minutes |
💰 Cost to complete | Free when using the AWS Free Tier or USD 2.62 |
🧩 Prerequisites | - AWS Account - Amazon DynamoDB Table |
💻 Code Sample | Code sample used in tutorial on GitHub |
📢 Feedback | Any feedback, issues, or just a 👍 / 👎 ? |
⏰ Last Updated | 2023-07-20 |
- An IAM Role with the name
rust-link-shortener-ec2-role
- An IAM Policy that will allow DynamoDB actions
- A DynamoDB table with the name
url_shortener
rust-link-shortener-ec2-role
IAM role during setup. Without this role yoru instance will not be able to access DynamoDB. The first instance will be of c5.xlarge
instance-type, and the second will be of c6g.xlarge
instance-type. Once they are running, connect to each instance and install Rust using the following command:1
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
build-essential
package to obtain the necessary compilers and linkers for any of the modules we will be installing later.1
2
sudo apt update
sudo apt install build-essential
1
git clone https://github.com/build-on-aws/building-rust-applications-for-aws-graviton
rust-link-shortener
directory containing all of the appropriate code.c5.xlarge
instance, navigate to the building-rust-applications-for-aws-graviton
directory and run the following command to build the application:1
cargo build --release
1
2
3
4
5
Finished release [optimized] target(s) in 2m 41s
real 2m41.674s
user 10m6.078s
sys 0m25.774s
target/release
directory and the rust-link-shortener
binary will be there ready for launch. To launch it run command ./rust-link-shortener
. The application is configured to run on port 8000 and listen on all interfaces for the purposes of this demo.c6g.xlarge
instance, navigate to the building-rust-applications-for-aws-graviton
directory and run the following command to build the application:1
cargo build --release
1
2
3
4
5
Finished release [optimized] target(s) in 3m 27s
real 3m27.946s
user 12m57.304s
sys 0m26.632s
target/release
directory and the rust-link-shortener
binary will be there ready for launch. To launch it run command ./rust-link-shortener
. The application is configured to run on port 8000 and listen on all interface for the purposes of this demo.1
curl -X POST -d "https://aws.amazon.com/ec2/graviton/" http://10.3.76.37:8000/shorten_url -H 'Content-Type: application/json'
1
https://myservice.localhost/rlbnDueu
rlbnDueu
is our application’s identifier for our URL.get_full_url
API, as shown in the following command.1
curl -X GET -d "rlbnDueu" http://10.3.76.37:8000/get_full_url -H 'Content-Type: application/json'
1
Your full URL is https://aws.amazon.com/ec2/graviton/
c6g.xlarge
and a c5.xlarge
instance we will be performing a load test to verify that the application built for Graviton is working as expected. We discuss various load testing methodologies in the Graviton Technical Guide on GitHub and recommend using a framework like wrk2. wrk2
is a version of wrk
that is modified to produce a constant throughput load and report accurate latency details across various percentiles. I decided to go ahead and use wrk2
to test the shorten_url
function of our application and compare the total requests per second served as well as the average latency at each percentile during our load test. I've kept the load tests simple in this guide to illustrate that testing is important. I'll be running my load tests from a c5.18xlarge instance in the same availability zone as our test instances. I'm using a c5.18xlarge instance because I know it will be able to load test our instances thoroughly because it is a much larger instance size than any of the exampe instances.1
2
3
git clone git@github.com:kinvolk/wrk2.git
cd wrk2
make
shorten_url
function uses the POST method and requires some data, we need a lua config file to pass to wrk2. My post.lua
file has the following content:1
2
3
wrk.method = "POST"
wrk.headers["content-type"] = "application/json"
wrk.body = "https://aws.amazon.com/ec2/graviton/"
-c
parameter controls how many connections are made during the load test. The -d
test specifies how long the test should run. The -L
parameter enables reporting of latency statistics across various percentiles. The -R
parameter specifies how many requests per second the load test should run with. The -s
parameter allows us to specify our Lua script we defined above.1
2
3
4
5
C5 Instance
./wrk -c120 -t60 -d 5m -L -R 13000 -s ./post.lua http://10.3.71.236:8000/shorten_url
C6g Instance
./wrk -c120 -t60 -d 5m -L -R 13000 -s ./post.lua http://10.3.69.199:8000/shorten_url
Latency Percentiles | C5.xlarge | C6g.xlarge |
---|---|---|
50 | 5.16ms | 5.26ms |
75 | 5.72ms | 5.85ms |
90 | 6.51ms | 6.65ms |
99 | 17.82ms | 16.94ms |
99.9 | 49.76ms | 39.23ms |
99.99 | 79.93ms | 66.05ms |
99.999 | 134.27ms | 193.66ms |
100 | 167.55ms | 333.05ms |
-R
parameter to 14000.1
2
3
4
5
C5 Instance
./wrk -c120 -t60 -d 5m -L -R 14000 -s ./post.lua http://10.3.71.236:8000/shorten_url
C6g Instance
./wrk -c120 -t60 -d 5m -L -R 14000 -s ./post.lua http://10.3.69.199:8000/shorten_url
Latency Percentiles | C5.xlarge | C6g.xlarge |
---|---|---|
50 | 5.47ms | 5.48ms |
75 | 6.04ms | 6.16ms |
90 | 6.80ms | 7.18ms |
99 | 17.15ms | 20.66ms |
99.9 | 36.48ms | 42.94ms |
99.99 | 56.90ms | 205.82ms |
99.999 | 143.74ms | 709.12ms |
100 | 168.45ms | 966.66ms |
-R
parameter to 15000.1
2
3
4
5
C5 Instance
./wrk -c120 -t60 -d 5m -L -R 15000 -s ./post.lua http://10.3.71.236:8000/shorten_url
C6g Instance
./wrk -c120 -t60 -d 5m -L -R 15000 -s ./post.lua http://10.3.69.199:8000/shorten_url
Latency Percentiles | C5.xlarge | C6g.xlarge |
---|---|---|
50 | 5.44s | 5.47ms |
75 | 8.00s | 6.11ms |
90 | 9.47s | 7.08ms |
99 | 11.20s | 29.20ms |
99.9 | 12.71s | 65.47ms |
99.99 | 13.82s | 107.39ms |
99.999 | 13.89s | 447.74ms |
100 | 13.90s | 571.90ms |
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.