Skip to main content
Chat with me

Deploy a Dockerized Express App on AWS EKS

00:03:25:80

Hey Dev Fam,

So, you've got another cool Express.js app huh? and now you're thinking, "How can I make it play nice with AWS EKS?" Fear not, I’ve got you covered with a laid-back, step-by-step guide.

Intro 👋

Amazon Elastic Kubernetes Service (EKS) provides a scalable and managed Kubernetes environment that simplifies the deployment and operation of containerized applications. In this article, I’ll walk through the process of deploying a Dockerized Express.js application on AWS EKS. This guide assumes you have a basic understanding of Docker, Kubernetes, and AWS services.

Prerequisites 📋

  1. AWS Account: Ensure you have an AWS account and an IAM user with the necessary permissions to create and manage EKS clusters, EC2, CloudFormation and VPC.
  2. Docker: Have Docker installed on your local machine to containerize the Express.js app.
  3. AWS CLI and EKSCTL: Install the AWS CLI and EKSCTL to interact with AWS services and Kubernetes clusters.

Step 1: Dockerize the Express.js App 🐳

First off, let's get this party started by containerizing your Express.js app. Create a Dockerfile in your project directory:

dockerfile
# Use an official Node.js runtime as a parent image
FROM node:18-alpine

# Set the working directory to /app
WORKDIR /app

# Copy only necessary files to the working directory
COPY package.json yarn.lock ./
COPY src ./src


# Install app dependencies
RUN yarn install --production

# Expose the port where our app meant to run
EXPOSE 5000

# Defining the environment variables
ENV NODE_ENV=production

# Use a non-root user for better security
USER monkey

# Specify the command to run on startup
CMD ["node", "src/index.js"]

Build the Docker image:

bash
docker build -t ghcr.io/your-gh-username/package-name:latest .

Step 2: Push the Docker Image to GitHub Packages 🚀

Github has a fully managed container registry that makes it easy to store, manage, and deploy Docker container images.

  1. Heads up to the Github settings and generate a classic personal access token with full access to the packages. You can follow this guid.

  2. Authorize Docker to your GitHub Account:

bash
docker login --username your-username --password your-gh-access-token ghcr.io
  1. Push the Docker image:
bash
docker push ghcr.io/your-gh-username/package-name:latest
  1. Open up the Packages tab on Github and make that package public. Otherwise you’ll need to define an access token in deployment config.

Step 3: Create an Amazon EKS Cluster 🤩

Assuming you have correctly configured the awscli with a correct IAM user and installed eksctl.

  1. Run the following command in your terminal with appropriate options. More info about the eksctl options can be found here.
bash
eksctl create cluster --name my-first-cluster --region us-east-2

Hang tight, It can take some time as it needs to setup VPC, CloudFormation etc.

Step 4: Configure kubectl for the EKS Cluster ⚙️

After the cluster is created, configure kubectl to use it:

bash
aws eks update-kubeconfig --name my-first-cluster --region us-east-2

Step 5: Deploy the App 🕺

Before deployment, you’ll need to create 2 config files to setup Kubernetes.

  1. Create a Kubernetes deployment YAML file (e.g., deployment.yaml):
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-express-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-express-app
  template:
    metadata:
      labels:
        app: my-express-app
    spec:
      containers:
        - name: my-express-app
          image: ghcr.io/your-gh-username/package-name:latest
          ports:
            - containerPort: 5000
  1. Create a service.yaml file that will manage the ports etc:
yaml
apiVersion: v1
kind: Service
metadata:
  name: my-express-service
spec:
  selector:
    app: my-express-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 5000
  type: LoadBalancer
  1. Apply the deployment:
bash
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

Step 6: Access the App 🤘

Once the deployment is complete, copy the external IP address of the LoadBalancer service:

bash
kubectl get svc express-app

Open a browser, punch in that IP, and behold your Express.js app, now grooving on AWS EKS with GitHub Packages!

Bonus 😌

You can use the following command to delete the cluster and linked services completely:

bash
eksctl delete cluster --name my-first-cluster

Following this guide will help you successfully run your Express.js app on a scalable and managed Kubernetes environment provided by AWS EKS.

Rock on! 🚀✨