After you’ve finished developing your Yew Rust application, it’s time to make it available to your users. It is expected to be placed on a server someplace, either on a Cloud service provider or an on-premises server, for this purpose.
This blog will go over how to launch your Yew Rust application on AWS EC2. We will utilize GitHub Actions to automate the deployment process by configuring a pipeline to generate artifacts that will be deployed to an EC2 instance. We are going to use Nginx as a web server to serve our static files.
In this blog, we will cover:
EC2 (Elastic Compute Cloud) is an AWS service that allows users to get access to secure computing resources over the Internet. These computing resources which include networking resources, RAM, Storage, and Operating systems can be scaled on demand. It allows the flexibility to install various packages that suit a client’s software needs.
Nginx is a lightweight web server that can also be used as a reverse proxy server depending on the user’s needs. In this case, we are going to use it to serve our build artifacts. The artifacts will include index.html, a CSS file, a WASM file, and a JS script.
- Decreases website load time, ensuring a smoother user experience and minimizing high latency issues.
- Enhances overall performance by effectively directing traffic to web servers, leading to a more enjoyable browsing experience for users.
- Provides a cost-effective and high-performance load-balancing solution.
- Offers scalability and the ability to handle multiple queries simultaneously.
- Enables seamless real-time upgrades without any interruptions or downtime.
The code used in this repository is found at https://github.com/workfall/workfall-yew-app
Creating an EC2 Instance on AWS
On the AWS cloud console, log in to your account and search for EC2. Then click on the launch instance button.
In the next screen, you can give the instance a name, and select an operating system; Ubuntu 22 LTS in this case.
Next, we can select an instance type depending on your memory and processor needs. We shall pick a free tier option for now.
Next, we can make firewall configs. It is advisable to carefully configure ingress rules so that your instance is not open to attack.
You can allow all ingress traffic in HTTP and HTTPS. But for SSH, you can allow ingress traffic from only your IP address, we can dynamically allow GitHub Actions ingress and later on revoke it.
Setting IAM permissions
The best direction to take for IAM permissions is to create an IAM user group that has a role that has access to a GitHub Actions Identity provider and then create a new user and add them to the user group. To add an Identity provider you can do it under identity providers in IAM.
GitHub Actions Role:
Under Trust relationships add GitHub Actions Identity provider.
GitHub Actions workflows
In your project root folder add the main.yml file to the path .github/workflows
In this file, we shall build a release version with GitHub Actions and then deploy the artifacts to the EC2 instance created before.
Adding secrets to your GitHub repository
In your repository, go to settings > Secrets and variables and add the secrets that will be required by GitHub Actions. Namely AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, EC2_HOST and EC2_USER.
Login to your EC2 instance via SSH tunnel and install Nginx using the command sudo apt install nginx. To test if nginx is working, navigate to your public IP or use the command systemctl status nginx
To edit the Nginx configuration, use the command sudo nano /etc/nginx/sites-enabled/default. Edit the file to match the configuration shown below and then save and restart the Nginx server using the command systemctl restart nginx.
GitHub Actions will copy the artifacts to the /dist folder which we are pointing to nginx for it to serve.
Since you will be deploying code from the home directory, give Nginx permissions by running the following commands in order.
sudo chown -R your_instance_user:www-data /home/your_instance_user/dist
sudo chmod -R 750 /home/your_instance_user/dist
sudo nginx -t
sudo systemctl restart nginx
You should able to see your application deployed live on AWS EC2.
Note: Make sure to edit the URL to use HTTP instead of HTTPS in case you haven’t installed an SSL certificate.
In this blog, we covered deploying a Yew Rust application to AWS EC2 using GitHub Actions and Nginx. Most of the AWS concepts on Identity providers are covered in previous blogs. The links will be attached below.
In the future, we shall tackle security when it comes to architecting your applications on AWS. For example, how to grant and revoke permissions to third-party services like GitHub Actions without making your instances vulnerable to attacks. We will come up with more such use cases in our upcoming blogs.
If you are a Rust & EC2 Lover and want to explore more about the above topics, here are a few of our blogs for your reference:
- How to Build a Rust WebAssembly Frontend App with Yew Framework?
- AWS is in love with RUST and you should be too!
- 15 AWSome years of Amazon EC2 and still going strong!
Stay tuned to get all the updates about our upcoming blogs on the cloud and the latest technologies.
Keep Exploring -> Keep Learning -> Keep Mastering
At Workfall, we strive to provide the best tech and pay opportunities to kickass coders around the world. If you’re looking to work with global clients, build cutting-edge products, and make big bucks doing so, give it a shot at workfall.com/partner today!