Skip to content

Commit 652a95e

Browse files
committed
address PR review: author lookup, 201 on create, authorId index, typecheck script, README improvements
1 parent c1acc0d commit 652a95e

4 files changed

Lines changed: 30 additions & 3 deletions

File tree

deployment-platforms/rest-express-docker-aws-ec2/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ sudo service docker start
132132
sudo usermod -aG docker ec2-user
133133
```
134134

135+
The EC2 instance needs permission to pull images from ECR. Choose one option:
136+
137+
- **Option 1 (recommended):** Attach an IAM instance role with the `AmazonEC2ContainerRegistryReadOnly` policy. No credentials are stored on the instance.
138+
- **Option 2:** Run `aws configure` on the instance and enter an IAM access key that has ECR read permissions.
139+
140+
This is required by the `deploy.yml` step that runs `aws ecr get-login-password` on the instance before pulling the Docker image.
141+
135142
Make sure port `3000` (or your chosen `CONTAINER_PORT`) is open in the instance's security group.
136143

137144
### 2. Configure GitHub secrets and variables
@@ -160,4 +167,15 @@ In your repository, go to **Settings → Secrets and variables → Actions** and
160167

161168
### 3. How deployment works
162169

163-
Copy [`.github/workflows/deploy.yml`](./.github/workflows/deploy.yml) to `.github/workflows/` at the root of **your own repository**. Pushing to `main` then triggers the workflow. It 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, runs `prisma migrate deploy` against your production database using a one-off container, 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.
170+
Copy [`.github/workflows/deploy.yml`](./.github/workflows/deploy.yml) to `.github/workflows/` at the root of **your own repository**. Pushing to `main` or `latest` triggers the workflow, which performs the following steps:
171+
172+
1. Authenticates with AWS using the configured IAM credentials.
173+
2. Builds a Docker image using Buildx with GitHub Actions layer caching for faster rebuilds.
174+
3. Pushes the image to ECR tagged with both the commit SHA and `latest`.
175+
4. SSHs into your EC2 instance.
176+
5. Runs `prisma migrate deploy` against your production database in a one-off container.
177+
6. Pulls the new image.
178+
7. Stops and removes the old container if one is running.
179+
8. Starts the new container with `DATABASE_URL` injected at runtime.
180+
9. Waits 5 seconds and verifies the container is running — prints logs and exits non-zero on failure.
181+
10. Prunes old images to keep the EC2 disk clean.

deployment-platforms/rest-express-docker-aws-ec2/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"license": "MIT",
55
"scripts": {
66
"build": "prisma generate && tsc",
7-
"dev": "prisma generate && tsx src/index.ts",
7+
"typecheck": "tsc --noEmit",
8+
"dev": "prisma generate && npm run typecheck && tsx src/index.ts",
89
"start": "node dist/index.js"
910
},
1011
"dependencies": {

deployment-platforms/rest-express-docker-aws-ec2/prisma/migrations/20240101000000_init/migration.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
2323

2424
-- AddForeignKey
2525
ALTER TABLE "Post" ADD CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
26+
27+
-- CreateIndex
28+
CREATE INDEX "Post_authorId_idx" ON "Post"("authorId");

deployment-platforms/rest-express-docker-aws-ec2/src/routes/post.routes.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,19 @@ postRouter.post('/post', async (req: Request, res: Response) => {
4343
res.status(400).json({ error: 'title and authorEmail are required' })
4444
return
4545
}
46+
const author = await prisma.user.findUnique({ where: { email: authorEmail } })
47+
if (!author) {
48+
res.status(404).json({ error: `No user found for email: ${authorEmail}` })
49+
return
50+
}
4651
const post = await prisma.post.create({
4752
data: {
4853
title,
4954
content,
5055
author: { connect: { email: authorEmail } },
5156
},
5257
})
53-
res.json(post)
58+
res.status(201).json(post)
5459
})
5560

5661
// PUT /publish/:id — publish a post

0 commit comments

Comments
 (0)