Jiaxin-P2-Microservice-Rust

Key Objectives of Project

In project 2, the purpose is to build a functional Web Microservice in Rust based on Kubernetes or other similar platforms (Kubernetes Deployments provide a higher-level abstraction for managing the deployment and scaling of applications in a cluster, making it easier to automate and manage the process). I create a simple actix Microservice for movie buffs which can be used for randomly choosing a good classic movie at leisure based on the list of the world top 10 best movies.

This actix Microservice has multiple routes:

A. type: "/" that returns a message : "Hello, random best movies around the world!"

B. type: "/movie" that returns a random best movie in the list of the world top 10 best movies

C. type: "/version" that returns the version of the service

Structure Diagram

Demo Video Link

Preparation

1. Containerization: Setup virtual environment

A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them.

  • Type: python3 -m venv env and source env/bin/activate

2. Instal Rust

  • Type: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh and then source "$HOME/.cargo/env"

3. create new project

  • Type: cargo new (project name) (my Eg: cargo new src)
  • Create main.rs and lib.rs for the src project
  • Cargo build: it is a command in the Rust programming language that is used to compile a Rust project. It compiles the project's source code and its dependencies, and produces an executable binary file. The cargo build command can be run from the root directory of the project.
  • Set up Cargo.toml to determine the dependencies and build configuration of the project.

Screen Shot 2023-02-09 at 1 42 11 PM

  • Set up Dockerfile for APP webdocker

Screen Shot 2023-02-09 at 1 45 39 PM

  • Create a Makefile: it is a special file that lists a set of rules for compiling a project. These rules include targets, which can be an action make needs to take or the files/objects make will need to build, and the commands that need to be run in order to build that target.

Screen Shot 2023-02-09 at 1 51 30 PM

Run Microservice

1. Use make format and make lint to fix code format and check code errors

Screen Shot 2023-02-09 at 2 06 34 PM

2. Run web microservice

  • After creating main.rs and lib.rs, in terminal, directly type: cargo run (Press CTRL+C to quit)

Screen Shot 2023-02-09 at 2 07 26 PM

Screen Shot 2023-02-09 at 2 01 26 PM

  • B. type: "/movie" that returns a random best movie in the list of the world top 10 best movies

Screen Shot 2023-02-09 at 2 02 33 PM

  • C. type: "/version" that returns the version of the service

Screen Shot 2023-02-09 at 2 04 38 PM

Use automatic deployment platforms to deploy the project

A. Via AWS APP Runner

  1. Go to github and then copy the http link for cloning my repo of the project 2

Screen Shot 2023-02-09 at 4 32 23 PM

  1. Go to AWS Cloud9, then click "Create environment"

Screen Shot 2023-02-09 at 4 35 43 PM

  • In the terminal, copy the clone link and type : git clone https://github.com/nogibjj/Jiaxin-P2-Microservice-Rust.git
  • Install Rust again

Screen Shot 2023-02-09 at 4 48 15 PM

  1. Go to AWS Amazon Elastic Container Registry, click "get start"
  • Then follow below steps to create a private repository

Screen Shot 2023-02-09 at 4 50 15 PM

Screen Shot 2023-02-09 at 4 52 05 PM

Screen Shot 2023-02-09 at 4 52 22 PM

  • Then go to "images", click "View push commands" to copy the code, then run each of them one by one at AWS Cloud9 later.

Screen Shot 2023-02-09 at 4 54 10 PM

Screen Shot 2023-02-09 at 4 56 40 PM

  • Tips: Sometimes you maybe occur an error when doing docker build, just type: curl -s https://gist.githubusercontent.com/wongcyrus/a4e726b961260395efa7811cab0b4516/raw/6a045f51acb2338bb2149024a28621db2abfcaab/resize.sh | bash /dev/stdin 60 to fix it

Screen Shot 2023-02-08 at 5 00 21 PM

  • Copy above code in terminal at AWS Cloud9

Screen Shot 2023-02-09 at 4 58 06 PM

Screen Shot 2023-02-09 at 5 00 07 PM

  1. Go to AWS APP Runner
  • Click "create service" to set up configuration by following below steps:

Screen Shot 2023-02-09 at 5 10 25 PM

Screen Shot 2023-02-09 at 5 16 48 PM

Screen Shot 2023-02-09 at 5 17 40 PM

  • Then begin to depoly the project after click "Deploy", make sure the event status are "Succeeded" at Deployment logs, otherwise you need to fix the errors according to the report from the event log.

Screen Shot 2023-02-09 at 5 18 47 PM

Screen Shot 2023-02-09 at 5 22 31 PM