Skip to content

Commit 79c27e5

Browse files
committed
feat: add rest-express-docker-aws-ec2 deployment example
1 parent 248f85b commit 79c27e5

15 files changed

Lines changed: 497 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
dist
3+
.env
4+
prisma/generated
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DATABASE_URL="postgresql://USER:PASSWORD@HOST:5432/DATABASE"
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: Deploy to AWS EC2
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
12+
env:
13+
AWS_REGION: ${{ vars.AWS_REGION }}
14+
ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }}
15+
CONTAINER_NAME: ${{ vars.CONTAINER_NAME }}
16+
CONTAINER_PORT: ${{ vars.CONTAINER_PORT }}
17+
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
22+
- name: Configure AWS credentials
23+
uses: aws-actions/configure-aws-credentials@v4
24+
with:
25+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
26+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
27+
aws-region: ${{ env.AWS_REGION }}
28+
29+
- name: Set up Docker Buildx
30+
uses: docker/setup-buildx-action@v3
31+
32+
- name: Login to Amazon ECR
33+
id: login-ecr
34+
uses: aws-actions/amazon-ecr-login@v2
35+
36+
- name: Build and push Docker image
37+
uses: docker/build-push-action@v6
38+
with:
39+
context: .
40+
push: true
41+
tags: |
42+
${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}
43+
${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:latest
44+
cache-from: type=gha
45+
cache-to: type=gha,mode=max
46+
47+
- name: Deploy to EC2
48+
uses: appleboy/ssh-action@8743aa11bfbda97acb45c151ae7a2e0b203f1914
49+
env:
50+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
51+
with:
52+
host: ${{ secrets.EC2_HOST }}
53+
username: ${{ secrets.EC2_USER }}
54+
key: ${{ secrets.EC2_SSH_KEY }}
55+
envs: ECR_REGISTRY,ECR_REPOSITORY,AWS_REGION,CONTAINER_NAME,CONTAINER_PORT
56+
script: |
57+
set -eu
58+
59+
# Auth
60+
aws ecr get-login-password --region "$AWS_REGION" | \
61+
docker login --username AWS --password-stdin "$ECR_REGISTRY"
62+
63+
# Pull latest image
64+
docker pull "$ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }}"
65+
66+
# Stop and remove existing container if running
67+
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
68+
docker stop "$CONTAINER_NAME"
69+
docker rm "$CONTAINER_NAME"
70+
fi
71+
72+
# Run new container
73+
docker run -d \
74+
--name "$CONTAINER_NAME" \
75+
--restart unless-stopped \
76+
-p "$CONTAINER_PORT:3000" \
77+
-e DATABASE_URL="${{ secrets.DATABASE_URL }}" \
78+
"$ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }}"
79+
80+
# Verify container is running
81+
sleep 5
82+
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
83+
echo "Container $CONTAINER_NAME is running"
84+
docker ps --filter "name=$CONTAINER_NAME" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
85+
else
86+
echo "Container $CONTAINER_NAME failed to start"
87+
docker logs "$CONTAINER_NAME" --tail 50
88+
exit 1
89+
fi
90+
91+
# Prune old images
92+
docker image prune -f
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
dist
3+
.env
4+
prisma/generated
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Stage 1: builder
2+
FROM node:20-alpine AS builder
3+
4+
WORKDIR /app
5+
6+
COPY package*.json ./
7+
COPY prisma ./prisma
8+
COPY prisma.config.ts ./
9+
RUN npm ci
10+
RUN npx prisma generate
11+
12+
COPY tsconfig.json ./
13+
COPY src ./src
14+
RUN npm run build
15+
16+
# Stage 2: runner
17+
FROM node:20-alpine AS runner
18+
19+
WORKDIR /app
20+
21+
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
22+
23+
COPY package*.json ./
24+
COPY prisma ./prisma
25+
COPY prisma.config.ts ./
26+
RUN npm ci --omit=dev
27+
28+
COPY --from=builder /app/dist ./dist
29+
COPY --from=builder /app/prisma/generated ./prisma/generated
30+
31+
USER appuser
32+
33+
EXPOSE 3000
34+
35+
CMD ["node", "dist/index.js"]
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# REST API with Express, Docker & AWS EC2
2+
3+
This example shows how to deploy a **Prisma REST API** (Express + TypeScript) to **AWS EC2** using **Docker** and **GitHub Actions**.
4+
5+
## Prerequisites
6+
7+
- [Docker](https://www.docker.com/) and Docker Compose (for the Docker path)
8+
- [Node.js](https://nodejs.org/) 20+ and a local PostgreSQL instance (for the non-Docker path)
9+
- An AWS account (for deployment only)
10+
11+
## Getting started
12+
13+
### 1. Clone the repository
14+
15+
```sh
16+
git clone https://github.com/prisma/prisma-examples.git --depth=1
17+
cd prisma-examples/deployment-platforms/rest-express-docker-aws-ec2
18+
```
19+
20+
### 2. Run the app
21+
22+
Choose one of the two local development paths below.
23+
24+
---
25+
26+
#### Option A — Docker Compose (recommended)
27+
28+
Copy the example env file (Docker Compose sets `DATABASE_URL` automatically, so no edits needed):
29+
30+
```sh
31+
cp .env.example .env
32+
```
33+
34+
Start the app and a local Postgres database:
35+
36+
```sh
37+
docker compose up --build
38+
```
39+
40+
The server is now running at `http://localhost:3000`. Migrations are applied automatically on startup.
41+
42+
---
43+
44+
#### Option B — Local Node.js + PostgreSQL
45+
46+
Create a `.env` file and set `DATABASE_URL` to your local database:
47+
48+
```sh
49+
cp .env.example .env
50+
# edit .env and set DATABASE_URL, e.g.:
51+
# DATABASE_URL="postgresql://prisma:prisma@localhost:5432/prisma"
52+
```
53+
54+
Install dependencies and generate the Prisma Client:
55+
56+
```sh
57+
npm install
58+
npx prisma migrate dev
59+
```
60+
61+
Start the development server:
62+
63+
```sh
64+
npm run dev
65+
```
66+
67+
The server is now running at `http://localhost:3000`.
68+
69+
---
70+
71+
### 3. Use the REST API
72+
73+
Create a user:
74+
75+
```sh
76+
curl -X POST http://localhost:3000/user \
77+
-H "Content-Type: application/json" \
78+
-d '{"email": "alice@prisma.io", "name": "Alice"}'
79+
```
80+
81+
Create a post:
82+
83+
```sh
84+
curl -X POST http://localhost:3000/post \
85+
-H "Content-Type: application/json" \
86+
-d '{"title": "Hello Prisma", "content": "My first post", "authorEmail": "alice@prisma.io"}'
87+
```
88+
89+
Publish a post:
90+
91+
```sh
92+
curl -X PUT http://localhost:3000/publish/1
93+
```
94+
95+
Fetch all published posts:
96+
97+
```sh
98+
curl http://localhost:3000/feed
99+
```
100+
101+
Fetch a single post:
102+
103+
```sh
104+
curl http://localhost:3000/post/1
105+
```
106+
107+
Delete a post:
108+
109+
```sh
110+
curl -X DELETE http://localhost:3000/post/1
111+
```
112+
113+
---
114+
115+
## Deploying to AWS EC2
116+
117+
### 1. Set up AWS prerequisites
118+
119+
**ECR repository** — create one if you haven't already:
120+
121+
```sh
122+
aws ecr create-repository --repository-name my-prisma-app --region us-east-1
123+
```
124+
125+
**EC2 instance** — launch an instance (Amazon Linux 2 or Ubuntu) and install Docker:
126+
127+
```sh
128+
# Amazon Linux 2
129+
sudo yum update -y
130+
sudo amazon-linux-extras install docker -y
131+
sudo service docker start
132+
sudo usermod -aG docker ec2-user
133+
```
134+
135+
Make sure port `3000` (or your chosen `CONTAINER_PORT`) is open in the instance's security group.
136+
137+
### 2. Configure GitHub secrets and variables
138+
139+
In your repository, go to **Settings → Secrets and variables → Actions** and add:
140+
141+
**Secrets** (sensitive values):
142+
143+
| Name | Description |
144+
|---|---|
145+
| `AWS_ACCESS_KEY_ID` | AWS IAM access key with ECR and EC2 permissions |
146+
| `AWS_SECRET_ACCESS_KEY` | Corresponding secret key |
147+
| `EC2_HOST` | Public IP or DNS of your EC2 instance |
148+
| `EC2_USER` | SSH username (e.g. `ec2-user` or `ubuntu`) |
149+
| `EC2_SSH_KEY` | Private SSH key used to connect to EC2 |
150+
| `DATABASE_URL` | PostgreSQL connection string for your production database |
151+
152+
**Variables** (non-sensitive values):
153+
154+
| Name | Example value |
155+
|---|---|
156+
| `AWS_REGION` | `us-east-1` |
157+
| `ECR_REPOSITORY` | `my-prisma-app` |
158+
| `CONTAINER_NAME` | `prisma-app` |
159+
| `CONTAINER_PORT` | `3000` |
160+
161+
### 3. How deployment works
162+
163+
Pushing to `main` triggers [`.github/workflows/deploy.yml`](./.github/workflows/deploy.yml). The workflow authenticates with AWS, builds a Docker image using Buildx (with GitHub Actions layer caching for faster rebuilds) and pushes it to ECR tagged with both the commit SHA and `latest`. It then SSHs into your EC2 instance, pulls the new image, gracefully stops and removes the old container if one exists, starts the new one with `DATABASE_URL` injected at runtime, waits 5 seconds and verifies the container is running — printing logs and exiting non-zero if it isn't. Finally it prunes old images to keep the EC2 disk clean.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
services:
2+
app:
3+
build: .
4+
ports:
5+
- "3000:3000"
6+
environment:
7+
DATABASE_URL: postgresql://prisma:prisma@postgres:5432/prisma
8+
depends_on:
9+
postgres:
10+
condition: service_healthy
11+
command: >
12+
sh -c "npx prisma migrate deploy && node dist/index.js"
13+
14+
postgres:
15+
image: postgres:16-alpine
16+
environment:
17+
POSTGRES_USER: prisma
18+
POSTGRES_PASSWORD: prisma
19+
POSTGRES_DB: prisma
20+
ports:
21+
- "5432:5432"
22+
healthcheck:
23+
test: ["CMD-SHELL", "pg_isready -U prisma"]
24+
interval: 5s
25+
timeout: 5s
26+
retries: 5
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "rest-express-docker-aws-ec2",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"scripts": {
6+
"build": "tsc",
7+
"dev": "tsx src/index.ts",
8+
"start": "node dist/index.js"
9+
},
10+
"dependencies": {
11+
"@prisma/adapter-pg": "7.0.0",
12+
"@prisma/client": "7.0.0",
13+
"dotenv": "^17.2.1",
14+
"express": "5.1.0",
15+
"pg": "^8.16.3"
16+
},
17+
"devDependencies": {
18+
"@types/express": "5.0.5",
19+
"@types/node": "22.18.12",
20+
"@types/pg": "^8.15.6",
21+
"prisma": "7.0.0",
22+
"tsx": "^4.20.6",
23+
"typescript": "5.8.2"
24+
}
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineConfig, env } from 'prisma/config'
2+
import 'dotenv/config'
3+
4+
export default defineConfig({
5+
schema: 'prisma/schema.prisma',
6+
migrations: {
7+
path: 'prisma/migrations',
8+
},
9+
datasource: {
10+
url: env('DATABASE_URL'),
11+
},
12+
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
generator client {
2+
provider = "prisma-client"
3+
output = "./generated"
4+
}
5+
6+
datasource db {
7+
provider = "postgresql"
8+
}
9+
10+
model User {
11+
id Int @id @default(autoincrement())
12+
email String @unique
13+
name String?
14+
posts Post[]
15+
}
16+
17+
model Post {
18+
id Int @id @default(autoincrement())
19+
title String
20+
content String?
21+
published Boolean @default(false)
22+
author User @relation(fields: [authorId], references: [id])
23+
authorId Int
24+
}

0 commit comments

Comments
 (0)