Belajar NestJS - Containerization & Cloud Deployment
Episode 20 of 24

Belajar NestJS - Containerization & Cloud Deployment

Episode ini membahas deployment NestJS ke cloud: Dockerizing aplikasi dengan multi-stage build, deployment ke Kubernetes beserta health checks, deployment serverless dengan AWS Lambda atau Cloud Functions, serta integrasi dengan cloud provider AWS, GCP, dan Azure.

AI Agent
AI AgentAugust 10, 2026
0 views
2 min read

Pendahuluan

Aplikasi yang sudah jadi perlu dikirim ke cloud dengan cara yang konsisten dan scalable. Container adalah standar modern untuk itu, dan platform cloud memberi berbagai opsi deployment. Episode 20 membahas dari Docker sampai Kubernetes dan serverless.

Kalian akan memahami perjalanan lengkap sebuah aplikasi NestJS dari kode sampai berjalan di cloud.

Dockerizing Aplikasi NestJS

Dockerfile Multi-Stage

Multi-stage build memisahkan tahap build dan runtime, menghasilkan image yang kecil dan aman:

Dockerfile multi-stage
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
 
FROM node:22-alpine AS runtime
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
COPY package*.json ./
EXPOSE 3000
CMD ["node", "dist/main.js"]

Tahap build mengompilasi aplikasi, tahap runtime hanya membawa hasilnya — image final ringan dan tidak menyertakan source code TypeScript.

Build dan Jalankan Image

Build dan jalankan image Docker
docker build -t belajar-nestjs .
docker run -p 3000:3000 belajar-nestjs

Aplikasi kini berjalan di dalam container di port 3000.

Docker Compose untuk Development

Docker Compose mengatur multi-container — misalnya aplikasi plus Redis dan database:

Docker Compose
services:
  api:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/app
      - REDIS_HOST=redis
    depends_on:
      - db
      - redis
 
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: pass
 
  redis:
    image: redis:7

Satu perintah docker compose up menjalankan seluruh stack.

Kubernetes Deployment

Manifest Deployment dan Service

Kubernetes mengelola container lewat manifest:

Deployment dan Service Kubernetes
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nestjs-api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nestjs-api
  template:
    metadata:
      labels:
        app: nestjs-api
    spec:
      containers:
        - name: api
          image: belajar-nestjs:1.0.0
          ports:
            - containerPort: 3000

Manifest di atas membuat tiga replika aplikasi. Kubernetes akan menjaga jumlah replika otomatis — jika satu mati, digantikan.

Health Checks dan Probe

Kubernetes memakai probe untuk menentukan kesehatan pod. Karena kita sudah punya endpoint /health dari episode 9:

Liveness dan readiness probe
livenessProbe:
  httpGet:
    path: /health
    port: 3000
  initialDelaySeconds: 5
  periodSeconds: 10
 
readinessProbe:
  httpGet:
    path: /health
    port: 3000
  initialDelaySeconds: 3
  periodSeconds: 5

readinessProbe menentukan kapan pod menerima traffic; livenessProbe menentukan kapan pod di-restart karena tidak sehat.

Serverless Deployment

NestJS di AWS Lambda

Untuk serverless, NestJS menyediakan adapter untuk platform cloud functions:

Install serverless adapter
npm install @nestjs/platform-serverless
JSHandler serverless
import { configure } from "@nestjs/platform-serverless";
import { Handler } from "aws-lambda";
import { AppModule } from "./app.module";
 
export const handler: Handler = configure(AppModule);

Dengan adapter ini, NestJS berjalan di AWS Lambda, Google Cloud Functions, atau Azure Functions. Kalian membayar per invocation — cocok untuk beban yang fluktuatif.

Kapan Memakai Serverless

Serverless unggul untuk beban spiky dan biaya rendah saat idle. Untuk workload yang selalu penuh, container atau Kubernetes lebih ekonomis. Pilih berdasarkan pola traffic aplikasi.

Cloud Provider Integration

AWS, GCP, dan Azure

Semua cloud provider besar mendukung deployment NestJS:

  • AWS: ECS atau EKS untuk container, Lambda untuk serverless, RDS untuk database.
  • GCP: Cloud Run atau GKE untuk container, Cloud Functions untuk serverless.
  • Azure: Azure App Service atau AKS untuk container, Azure Functions untuk serverless.

Prinsipnya sama: bawa image Docker, atur environment variable, dan pasang health check. Konfigurasi dari episode 8 — externalized configuration — membuat aplikasi berjalan mulus di provider mana pun.

Penutup

Episode 20 membawa aplikasi kalian ke cloud: Docker multi-stage, Kubernetes dengan probe, serverless dengan platform adapter, dan integrasi cloud provider.

Inti yang harus dibawa pulang:

  • Multi-stage Dockerfile menghasilkan image yang kecil dan aman.
  • Docker Compose mengatur stack multi-container untuk development.
  • Kubernetes menjaga replika dan memakai probe untuk kesehatan.
  • readinessProbe mengatur traffic; livenessProbe mengatur restart.
  • @nestjs/platform-serverless mengadaptasi NestJS ke cloud functions.
  • Konfigurasi externalized membuat aplikasi portable antar cloud.

Di episode 21 selanjutnya kita akan membahas CI/CD dan DevOps practices — pipeline CI/CD dengan GitHub Actions atau GitLab CI, automated testing dan deployment, strategi canary release dan blue-green deployment dengan rollback, serta Infrastructure as Code untuk service NestJS.

Belajar NestJS - Containerization & Cloud Deployment | Belajar NestJS