Together, let’s end cross-compiling.
The price performance of Graviton2 has drawn many projects to move to Arm-based EC2 instances. Historically, most software developed for the Arm architecture was done on x86 machines and cross-compiled for Arm. This was because the target system was a mobile device, microcontroller, or some other embedded system. Today, Graviton2 has excellent performance, and more developers are working natively on Arm.
With the availability of Graviton2 there is far less reason to cross-compile. Cloud instances provide convenient access from anywhere, eliminating the need for specialized local hardware. Do you have tools that are not supported on the Arm architecture? The Graviton Challenge is a motivating program to get them ported. If you don’t have the ability to port them yourself, ask the tool provider for Arm support.
A common question for developers is how to setup the code, compile, run, and debug loop that happens on a daily basis. Developer preferences and opinions vary widely, but I will share a couple of my favorites for Graviton2.
I use ssh, vim, and bash to do many things, but sometimes an IDE or remote desktop is needed.
Visual Studio Code
VS Code is popular and one of my favorites. It has great support for Arm across Windows, Linux, and macOS.
There are multiple ways to run or connect VS Code to an AWS EC2 instance powered by Graviton2. I recently tried out the blog, “Building an ARM64 Rust development environment using AWS Graviton2 and AWS CDK”. It is a great way to learn about the AWS CDK and other services like AWS Secrets Manager and AWS Global Accelerator. I appreciate the security and performance of the solution, but the complexity and price may be high for some developers. I do recommend giving it a try, it’s a great learning experience.
If you do the blog steps watch out for the double quotes. Copying from a browser to AWS CloudShell didn't work cleanly for me, some other type of double quotes show up on the command line. I also recommend to change the VPC_CIDR. Without this change I had VPC errors.
export VPC_CIDR="10.0.0.0/16"
The blog uses code-server, a project which puts VS Code in a browser.
Instead of running code-server in a Docker container, let’s see how to use code-server straight away on a Graviton2 instance.
Create a new EC2 instance with Graviton2. I'm using Ubuntu 20.04, but other Linux distributions are possible. This exercise can be done on a t4g.micro, which is included in Free Tier and AWS is providing 750 free hours per month for everybody until December 31, 2021.
Configure the security group to allow SSH on port 22 and only from your IP address. Use the private key (.pem file) for access. Numerous tutorials exist on how to create an EC2 instance, setup the security group, and ssh using the private key. For Ubuntu the username is ubuntu. Note the public IP address of the EC2 instance and use it for the ssh command below.
To connect from a machine with ssh installed, just the private key file is needed. Substitute your key file name and public IP address. After logging in, confirm the instance is the Arm architecture.
$ ssh -i mykey.pem ubuntu@<My public IP>
$ uname -m
aarch64
Update and install gcc so we can run a small example.
$ sudo apt update ; sudo apt upgrade -y
$ sudo apt install gcc neofetch -y
Install and run code-server. A cool feature is the ability to proxy code-server and forget about TLS, authentication, and port forwarding. Use the –-link flag and look for the printed URL. This is great because no other changes to the security group are required.
$ curl -fsSL https://code-server.dev/install.sh | sh
$ code-server --link
Open the printed URL in a browser. If you are not signed into GitHub you will be asked for your credentials. After confirming your credentials, VS Code is ready to go on Graviton2. The screenshot shows a simple command sequence to get a project from GitHub, compile, and run it.
Here are the steps if you want to paste them.
$ git clone https://github.com/jasonrandrews/hello-arm.git
$ cd hello-arm/c-hello-world
$ gcc "-DARCH=\"`uname -a`\"" hello.c -o hello
$ ./hello
Sometimes a Linux desktop is needed for running graphical applications. While there are numerous ways to access a remote desktop, none of them are great. Complexity, performance, and security are a few factors. The need to install client software is also a drawback. I would love to hear what others use in the comments.
Remote desktop with NoMachine
The solution I use most often is NoMachine. It works well on Arm systems like Graviton2 and has good performance. It does require client software but clients are available for every machine I need. It can also use the private key (.pem file) directly so there is no need to create a password for the user account in the EC2 instance.
Using the EC2 instance above some extra software is required. For a minimal install I would recommend xfce4. For a full Linux desktop I would recommend ubuntu-desktop.
For xfce4 install the extra software.
$ sudo DEBIAN_FRONTEND=noninteractive apt install xfce4 xfce4-terminal firefox -y
Install NoMachine on the EC2 instance
Unfortunately, I cannot find a URL to get NoMachine for Arm using curl. On x86 the command below works, but I'm not sure how to get the Arm version. Ironic that a desktop is needed to get software to setup a remote desktop. For development projects I put the .deb file in AWS S3 and copy it to new EC2 instances, but for this article it will need to be manual. (Don't run this one, but I would love to know the URL for the Arm .deb file.)
$ curl -fSL "https://www.nomachine.com/free/linux/64/deb" -o nomachine.deb
Instead download NoMachine from your local machine and scp it to the EC2 instance. There are many Arm versions, but I recommend the Raspberry Pi 4 64-bit .deb file as the best one for Ubuntu on Graviton2.
$ scp -i ~/mykey.pem nomachine_7.6.2_3_arm64.deb ubuntu@<public IP>:~/
Now, go back on the EC2 instance and install NoMachine.
$ sudo dpkg -i ./nomachine_7.6.2_3_arm64.deb
To be able to connect using only the private key, copy the public key for NoMachine to use.
$ mkdir -p ~/.nx/config
$ cp ~/.ssh/authorized_keys ~/.nx/config/authorized.crt
One last trick is to coax NoMachine to create a virtual display with flexible resolution. Without this it will try to connect to the physical display and the resolution is too low and cannot be increased.
$ sudo systemctl set-default multi-user
Install a NoMachine Client
Download the NoMachine client for whatever machine you are using; Windows, Mac, Linux, or Android.
To connect with NoMachine on the default port, add port 4000 on the security group. NoMachine can use the same private key (.pem) file used for ssh. The user interface is a little different depending on the platform and version, but look for "Use key-based authentication with a key you provide". This key is the .pem file.
Remember the user name is ubuntu for the EC2 instance.
Summary
Let’s end cross compiling. As a developer, VS Code and remote desktop can get a lot of things done. Although the benefits of Graviton2 come from the price performance and the freedom to work natively on the Arm architecture, sometimes things like IDE and remote desktop can get in the way. Hopefully the tips in this article enable more developers to have a smooth experience with AWS Graviton2.
Top comments (0)