Step-by-Step guide to host a simple Rest API in AWS | Docker | AWS App Runner

What is AWS App Runner?

AWS App Runner helps developers deploy containerized web applications and API. We can deploy our application directly using our source code or a docker image. We can easily set up the automatic deployments, and also we can do it manually. Furthermore, we also have an Autoscaling mechanism for traffic management.

Use case

Whenever there is a need to host an application quickly without hosting quickly and if any small app has below 10 APIs, I use it for free since my requests are too low.

What can be done in AWS App Runner?

  1. Frontend and backend web applications.

  2. Microservices and APIs.

  3. Rapid production deployments.

Prerequisites

  • AWS Account

  • Docker

  • Node.js

How we are going to design the API using AWS!

Step 1: Create REST API using NodeJS Application

  1. Create a new folder and navigate the terminal to this folder

  2. Use the following command to initiate the rest_api %[npm init -y]

    image.png

  3. Install the dependencies [npm i express -s && npm i cors -s && npm i dotenv -s && npm i express -s && npm i compression -s && npm i express-json-errors -s && npm i express-validation -s && npm i express-validator -s && npm i async -s]

  4. Create [server.js]and proceed with the following code.

  5. And write whatever API you need to like (Get, Post, Put, Delete).

const express = require('express');
const cors = require('cors');
const config = require('./config/app.js');
const compression = require('compression');
const routes = require('./routes');
const middlewareErrorParser = require('./middleware/ErrorParser');
const middlewarePathLogger = require('./middleware/PathLogger');
const app = express();
app.use(express.json({ type: "application/json" }));
app.use(express.urlencoded({ extended: false }));
app.use(cors());
app.use(compression());
if (config.debug) {
    app.use(middlewarePathLogger);
}
app.use('/', routes);
app.use(middlewareErrorParser);
app.listen(config.port, () => {
    console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});

module.exports = app;

Step 2: Dockerize your application

  1. Install Docker by clicking Docker desktop.

  2. In the root folder of the application, create 'Dockerfile' and paste the below code in it.

     FROM node:latest
    
     RUN mkdir -p /usr/src/app
     WORKDIR /usr/src/app
     COPY . /usr/src/app
    
     EXPOSE 3000
     RUN npm install nodemon -g
     RUN npm install
     CMD ["npm", "start"]
    
  3. Now build your docker by using the following command

    docker build -t 'your-image-name'

  4. Now your image is ready.

Step 3: Push your Docker image to AWS ECR

  1. Once the docker image is ready, needs to push to AWS ECR.

  2. Create an Elastic Container Registry using AWS CLI commands as follows.

  3. aws ecr create-repository --repository-name your-repo-name.

  4. Now authenticate your docker to the ECR which is created

    1. aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin yourAaccountId.dkr.ecr.your-region.amazonaws.com
  5. Once logged in, create a tag to your docker image command as follows

    1. docker tag your-image-name:latest your-account-id.dkr.ecr.your-region.amazonaws.com/your-repo-name:latest
  6. Now push your image to AWS Elastic container registry

    1. docker push your-account-id.dkr.ecr.your-region.amazonaws.com/your-repo-name:latest

Step 4: Create an AWS App Runner Service using ECR image

  1. Open the AWS management console and navigate to AWS App Runner

  2. Under services, click on Create Service as below

  3. Select Container registry and Amazon ECR private images and then select the image that is pushed

  4. In the below Role section, select Create a new rule and Create a new role.

  5. Configure the settings for your service, including the service name, instance size, and environment variables if needed.

  6. Review your settings and click "Create & deploy".

  7. AWS App Runner will create a new service and deploy your containerized API.

Conclusion

Once your service is deployed, AWS App Runner will provide you with a URL to access your REST API. You can find this URL in the AWS App Runner console.

Congratulations! You have successfully hosted a simple REST API in AWS using Docker, AWS ECR and AWS App Runner. Your API is now accessible through the provided URL.

0
Subscribe to my newsletter

Read articles from Kumaravel Ponnusamy directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kumaravel Ponnusamy
Kumaravel Ponnusamy

๐ŸŒŸ Welcome to my world of endless curiosity and innovation! I'm Kumaravel Ponnusamy, a dedicated problem solver and lifelong learner on a quest for knowledge and impact. ๐Ÿ’ก ๐Ÿ‘จโ€๐Ÿ’ป By day, I navigate the digital landscape, leveraging my skills to craft creative solutions and drive positive change. ๐Ÿš€ ๐Ÿ“š When I'm not immersed in technology and problem-solving, you'll find me in great outdoors, or indulging in my passion. ๐ŸŒ Join me on this exciting journey as I explore the boundless possibilities of our ever-evolving world. Let's connect, collaborate, and make a difference together! ๐Ÿค #InnovationEnthusiast #ProblemSolver #LifelongLearner #PassionDriven