Step-by-Step guide to host a simple Rest API in AWS | Docker | AWS App Runner
Table of contents
- What is AWS App Runner?
- Use case
- What can be done in AWS App Runner?
- Prerequisites
- How we are going to design the API using AWS!
- Step 1: Create REST API using NodeJS Application
- Step 2: Dockerize your application
- Step 3: Push your Docker image to AWS ECR
- Step 4: Create an AWS App Runner Service using ECR image
- Conclusion
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?
Frontend and backend web applications.
Microservices and APIs.
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
Create a new folder and navigate the terminal to this folder
Use the following command to initiate the rest_api %[
npm init -y
]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
]Create [
server.js
]and proceed with the following code.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
Install Docker by clicking Docker desktop.
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"]
Now build your docker by using the following command
docker build -t 'your-image-name'
Now your image is ready.
Step 3: Push your Docker image to AWS ECR
Once the docker image is ready, needs to push to AWS ECR.
Create an Elastic Container Registry using AWS CLI commands as follows.
aws ecr create-repository --repository-name your-repo-name.
Now authenticate your docker to the ECR which is created
aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin
yourAaccountId.dkr.ecr.your-region.amazonaws.com
Once logged in, create a tag to your docker image command as follows
docker tag your-image-name:latest
your-account-id.dkr.ecr.your-region.amazonaws.com/your-repo-name:latest
Now push your image to AWS Elastic container registry
Step 4: Create an AWS App Runner Service using ECR image
Open the AWS management console and navigate to AWS App Runner
Under services, click on Create Service as below
Select Container registry and Amazon ECR private images and then select the image that is pushed
In the below Role section, select Create a new rule and Create a new role.
Configure the settings for your service, including the service name, instance size, and environment variables if needed.
Review your settings and click "Create & deploy".
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.
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