iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
⛵

Deploying Actix web to AWS Lightsail Containers

に公開
2021/03/21

Introduction

I built a web application using Actix web, and since it was also for technical study, I wanted to try a deployment destination I hadn't used before. Given that I use AWS in my daily work, I decided to adopt AWS Lightsail Containers, which was announced at the end of last year.

Deploying to AWS Lightsail Containers was extremely simple. Also, in the process of deploying, I was able to learn how to create Docker images for Rust. I decided to write this article to summarize those steps.

Creating a Docker Image for Actix web

The application I developed uses React for the frontend, and it is published by placing the built files in the public folder of Actix web. Therefore, while the Dockerfile below uses a multi-stage build, the instructions from FROM rust:1.49 onwards are essentially what pertains to Actix web.

Dockerfile
# Image for React build
FROM node:14.15.4-alpine3.10 as client_builder

ARG REACT_APP_API_URL
ARG REACT_APP_GYAZO_AUTH_URL
ARG REACT_APP_GA_UNIVERSAL_ID

WORKDIR /client
COPY ./client/package*.json .
RUN yarn install

ADD ./client .
RUN yarn build

# Image for Actix web build
FROM rust:1.49

# Expose the port for accessing Actix web
EXPOSE 8080

# Add the Actix web project folder to the image
WORKDIR /server
ADD ./server .

# Run `cargo install` within the project folder to generate the build
RUN cargo install --path .

# Remove files that are no longer needed
RUN ls | grep -v -E 'templates' | xargs rm -r

# Add the content built in the React build image to the Actix web build image
COPY --from=client_builder /client/build ./build
RUN mkdir tmp

# Execute the build generated by the `cargo install` command to start Actix web
# The command name below follows [package.name] in Cargo.toml
CMD ["bloggimg-server"]

Also, to make it easier to manage options during the Docker build, I used Docker Compose. I recommend using it even when building a single Docker image, as it allows you to immediately respond when you want to add and link containers later.

docker-compose.yml
# Specify the path of the Actix web project in context
# Set the ARGS values you want to use during the Docker build in args via environment variables
# Specify the <image name:tag name> in image (assuming deployment to Docker Hub this time)
# Specify the dotenv file you want to use during development/verification in env_file
# Write port mapping settings in ports

version: '3.8'
services:
  app:
    build:
      context: ./
      args:
        - REACT_APP_API_URL=${REACT_APP_API_URL}
        - REACT_APP_GYAZO_AUTH_URL=${REACT_APP_GYAZO_AUTH_URL}
        - REACT_APP_GA_UNIVERSAL_ID=${REACT_APP_GA_UNIVERSAL_ID}
    image: n1kaera/bloggimg:v1.0.0
    env_file:
      - ./server/.env
    ports:
      - 8080:8080

Modify the above according to your own Actix web project and verify the operation by running docker-compose up. Once verified, execute docker-compose build to build the Docker image. After a successful build, the next step is to push the image to Docker Hub.

Pushing the Built Image to Docker Hub

This time, we will use Docker Hub to manage the images used with AWS Lightsail Containers. Before pushing to Docker Hub, complete your login by running the command docker login --username=<Docker Hub username>.

Then, push the Docker image to Docker Hub using the docker-compose push command.

Screenshot 2021-01-23 20.37.39.png
Check the Docker Hub page to confirm that the Docker image was pushed correctly

Once you have confirmed that the Docker image push was successful, the remaining work will be done in the AWS Console.

Creating a Lightsail Container Service from the AWS Console

After logging into the AWS Console, select the Lightsail service to go to the Lightsail top page. Once there, select the Containers tab and create a Container Service using the Create container services button.

Screenshot 2021-01-23 18.55.16.png
After logging into the AWS Console, navigate to the Lightsail page and select the Containers tab

Screenshot 2021-01-23 18.57.06.png
Click the Create container services button that appears when you select the Containers tab

On the screen you are directed to after clicking the Create container services button, select the region, capacity (the Micro tier is free for the first 3 months), etc., and enter a name. Since we have already finished the preparations for deployment, we will configure the deployment settings while creating the Container Service.

Deployment settings can be configured from the Set up your first deployment section.

Screenshot 2021-01-23 19.07.53.png
Click on Set up deployment to display the deployment configuration items

Screenshot 2021-01-23 19.12.14.png
Enter the required configuration items for deploying using a Docker Hub image

Screenshot 2021-01-23 19.16.16.png
Enter the information for the container health check

Screenshot 2021-01-23 19.19.17.png
Once all information is entered, click the Create container services button

Screenshot 2021-01-23 19.22.48.png
You can check the deployment status from Deployment versions at the bottom of the screen after the transition

Screenshot 2021-01-23 19.39.55.png
If deployment is successful, the Deployment versions status will become Active

Once deployment is complete, a Public domain should have been issued, so check if you can access and use the web application properly. The Public domain can be found on the top page of the corresponding Container Service.

Screenshot 2021-01-23 19.48.23.png
Verify operation via the Public domain on the AWS Lightsail Containers top page

Screenshot 2021-01-23 19.51.27.png
Conduct a thorough operation check to confirm that the deployment was successful

That completes the basic setup. If you want to redeploy with a new Docker image, you can do so by clicking the Modify your deployment link in the Deployments tab.

(Bonus) Accessing the Container Service via a Custom Domain

With AWS Lightsail Containers, associating a custom domain and enabling HTTPS can be set up easily. After selecting the Custom domains tab, click the Create certificate link at the bottom of the screen to display the configuration page.

Screenshot 2021-01-23 20.07.22.png
Click the Custom domain tab, then click the Create certificate link

Screenshot 2021-01-23 20.10.22.png
After entering various configuration items, click the Create button

Screenshot 2021-01-23 20.16.02.png
You will be asked to set up a CNAME record for domain verification, so perform the setup yourself

Screenshot 2021-01-23 20.20.26.png
After correctly setting up the CNAME record, the status will become Valid after a short while

Once the above is confirmed, set the Public domain value as the CNAME record for the domain you configured in Create certificate. Once the settings are reflected, you will be able to access the Container Service via HTTPS by navigating to your custom domain.

Screenshot 2021-01-23 20.27.26.png
Confirm that the service running on the Container Service is accessible via the Custom domains

Conclusion

I have briefly summarized the steps to deploy an Actix web project using AWS Lightsail Containers. While it is convenient, I currently feel that for personal development use, a Lightsail Instance might be better in terms of price and performance.

However, I felt that it could be effectively utilized in situations where you can leverage its benefits, such as having a Tokyo region, equipped with a load balancer, and being a service that is easy to scale while offering a fixed price.

Reference Links

Discussion