Skip to content

Commit f698871

Browse files
authored
rolling-scopes-school#9: Application deployment via Elastic Beanstalk (Docker platform) (#3)
* fix: add missing properties to a user's cart * feat: use esbuild to compile project * feat: wrap nest app as serverless function details: create aws cdk stack to deploy lambda handler * feat: add Prisma ORM to the project details: define cart and cart_items db schemas * feat: add postgres docker image for local testing * feat: add Product model to db schema * feat: add Prisma seed script to fill db with initial data * feat: add users table with relation to cart * fix: indicate correct entry point for cart lambda handler * refactor: rename prisma models properties * test: actualize e2e test * refactor: remove rows in cascade on removing relations * refactor: drop 'count' column in products table * feat: use Postgres to store data in cart service * tests: add integration test for Prisma service * feat: users service uses prisma service to store/retrieve data * fix: does not include cdk into source files * feat: cart controller uses basic auth for get/put methods details: add e2e tests for cart controller * feat: add possibility to create orders details: 1. create 'orders' table 2. update 'checkout' endpoint in cart controller 3. create order via prisma transaction 4. add integration tests for order creation 5. add e2e tests for order creation * feat: create rest api with proxy method to pass all the data to cart handler details: reference already existing db instance by its attributes * fix: include prisma linux query lib into cart lambda source code * refactor: drop 'products' table details: cart service should not save products data as its already present in 'products' service, so it will operate 'product_id'/'count' only * refactor: remove jwt/local auth strategies (use basic only) * refactor: bootstrap nest application in separate module details: clearly split server (listening to a port) and serverless implementations * feat: give final shape to input/output of cart controller/service details: cart creation/update/checkout implemented fully * refactor: remove 'calculateCartTotal' as there's no info about a product price * tests: update integration/e2e tests of cart service * refactor: squash prisma migrations * refactor: resolve dest of prisma query lib relatively to output dir * refactor: use webpack to build nest.js application details: esbuild does not respect 'experimentalDecorators' which are heavily used by nest.js for injection (see details at https://esbuild.github.io/content-types/#tsconfig-json) * refactor: remove PrismaModule import in AuthModule * feat: create rds db instance (postgres) via aws cdk * fix: correct supertest import in tests * feat: ignore 'cdk*' files and folders with prettier * feat: enable source maps for aws cdk build * feat: create network construct for managing connections between lambda and rds details: 1. network constructs creates security groups to establish connection between lambda and rds db instance 2. db instance endpoint host/port are used to create database url passed as end to lambda handler 3. lambda handler is deployed to one AZ only (the same availability zone is used by db instance) * refactor: take server port from env var * refactor: rename db env vars to conform with aws cdk * feat: add scripts to deploy/seed local/remote db instance with prisma * docs: add documentation for aws cdk stack * refactor: move aws cdk account id/region env vars to config * tests: actualize aws cdk unit tests * fix: correct formatting in cdk/README.md * fix: use .env.local or .env.remote depending where db changes are required * docs: add documentation for cart service * feat: add method to delete cart of a user * docs: add mentioning of .env file used for local development by default * tests: add Postman collection to test Cart service details: update README with info about Postman collection * feat: compile server and serverless implementations * feat: add Dockerfile to build server details: use Docker Compose to run cart-api comprising of postgres and nest.js app * refactor: create prisma:deploy and prisma:seed scripts to run with diff envs * feat: compile prisma seed script * docs: add comments to .dockerignore * feat: create Dockerfile to build image with cart api server * fix: export port as part of Elastic Beanstalk platform Docker requirements * feat: add cloudwatch streaming config for Elastic Beanstalk application * feat: add .ebignore as part of Elastic Beanstalk application config * feat: add npm scripts for Elastic Beanstalk application details: 1. env-cmd vars expansion is used to pass DATABASE_URL to env vars of EB application 2. Dockerfile is used to build image on EC2 instance (docker-compose.yml is ignored by .ebignore) * fix: include .ebextensions into Elastic Beanstalk deploy * feat: remove public access to RDS DB instance * refactor: remove internet <-> RDS security group * refactor: change eb instance type to t2.micro details: t2.micro falls under Free Tier 750h/month rule in eu-central-1 (Frankfurt) region * feat: add http api to proxy requests from HTTPS endpoint to EB HTTP endpoint * fix: add default headers to http proxy api to support CORS * docs: add information about docker/eb npm scripts
1 parent 0eb1826 commit f698871

18 files changed

Lines changed: 225 additions & 40 deletions

.dockerignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.git/
2+
# Contains AWS CDK stack declaration, not used during docker build
3+
cdk/
4+
# Contains compiled project files, will be compiled during docker build
5+
dist/
6+
node_modules/
7+
# Contains Postman collection for REST API testing, not used during docker build
8+
postman/
9+
# Contains tests, not run during docker build
10+
test/
11+
.dockerignore
12+
.DS_Store
13+
.env*
14+
.eslintrc.js
15+
.gitignore
16+
.prettierrc
17+
docker-compose.yml
18+
Dockerfile
19+
nest-cli.json
20+
README.md

.ebextensions/log-streaming.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
option_settings:
2+
aws:elasticbeanstalk:cloudwatch:logs:
3+
StreamLogs: true
4+
DeleteOnTerminate: true
5+
RetentionInDays: 1

.ebignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Ignore everything
2+
*
3+
# Except
4+
!.ebextensions
5+
!migrations
6+
!prisma
7+
!src
8+
!.dockerignore
9+
!Dockerfile
10+
!package*
11+
!tsconfig*
12+
!webpack.config*
13+
14+
cdk/
15+
node_modules/

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
DATABASE_USERNAME=admin
22
DATABASE_PASSWORD=123456
3-
DATABASE_URL=postgresql://admin:123456@localhost:5432/admin?schema=public
3+
DATABASE_URL=postgresql://admin:123456@localhost:5432/admin?schema=public
4+
SERVER_PORT=4000

Dockerfile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# syntax=docker/dockerfile:1
2+
FROM node:20-alpine AS base
3+
4+
FROM base AS install-dev
5+
WORKDIR /tmp/dev
6+
COPY package.json package-lock.json ./
7+
RUN npm ci
8+
9+
FROM base AS install-prod
10+
WORKDIR /tmp/prod
11+
COPY package.json package-lock.json ./
12+
RUN npm ci --omit dev
13+
14+
FROM install-dev AS prisma
15+
COPY prisma ./prisma
16+
COPY migrations ./migrations
17+
RUN npm run prisma:generate
18+
19+
FROM prisma AS build
20+
ENV NODE_ENV=production
21+
COPY tsconfig* webpack.config.js ./
22+
COPY src ./src
23+
RUN npm run build
24+
25+
FROM base
26+
WORKDIR /app
27+
COPY --from=install-prod /tmp/prod/node_modules/@prisma ./node_modules/@prisma/
28+
COPY --from=install-prod /tmp/prod/node_modules/prisma ./node_modules/prisma/
29+
COPY --from=install-prod /tmp/prod/node_modules/.bin ./node_modules/.bin/
30+
31+
COPY --from=prisma /tmp/dev/node_modules/.prisma ./node_modules/.prisma/
32+
COPY --from=prisma /tmp/dev/migrations ./migrations/
33+
COPY --from=prisma /tmp/dev/prisma ./prisma/
34+
35+
COPY --from=build /tmp/dev/dist/main.js ./dist/
36+
COPY --from=build /tmp/dev/dist/seed.js ./dist/
37+
COPY --from=build /tmp/dev/package.json ./
38+
EXPOSE 4000
39+
CMD ["dist/main.js"]

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,28 @@ Cart Service is responsible for creation carts/orders.
55
## NPM scripts
66

77
- `build` - builds cart handler;
8-
98
- `test:integration` - runs integration tests for Prisma service;
10-
119
- `test:e2e` - runs e2e tests;
12-
1310
- `prisma:generate` - generates Prisma client based on schema files stored in [prisma/schema](./prisma/schema/);
14-
1511
- `prisma:deploy:local` - creates databases based on migration files stored in [migrations](./migrations/);
16-
1712
- `prisma:deploy:remote` - same but uses `.env.remote` to connect to remote Postgres database;
18-
1913
- `prisma:seed:local` - populates Postgres database with initial data (see [seed.ts](./prisma/seed.ts));
20-
2114
- `prisma:seed:remote` - same but uses `.env.remote` to connect to remote Postgres database;
22-
2315
- `docker:up` - Starts DynamoDB local service (see [docker-compose.yml](./docker-compose.yml) for implementation details);
24-
25-
- `docker:down` - Stops DynamoDB local service.
16+
- `docker:down` - Stops DynamoDB local service;
17+
- `docker:build` - Builds `hazardsoft/cart-service` Docker image;
18+
- `docker:push` - Pushes `hazardsoft/cart-service` Docker image to Docker Hub;
19+
- `eb:init` - Initializes `hazardsoft-cart-api` Elastic Beanstalk application;
20+
- `eb:create` - Creates `prod` environment with `hazardsoft-cart-api-prod` subdomain for Elastic Beanstalk applciation;
21+
- `eb:deploy` - Deploys changes to `prod` environment of Elastic Beanstalk application;
22+
- `eb:destroy` - Destroys all environments of Elastic Beanstalk application.
2623

2724
## Environment
2825

2926
Copy/paste [.env.example](./.env.example) file and rename it to the following:
3027

31-
1. `.env.local`/`.env` - used to work with local instance of Postgres database;
32-
2. `.env.remote` - used to work with AWS RDS DB instance of Postgres database;
28+
1. `.env.local`/`.env` - contains credentials for local instance of Postgres database;
29+
2. `.env.remote` - contains credentials for AWS RDS DB instance of Postgres database.
3330

3431
### Populate Postgres database
3532

@@ -44,17 +41,21 @@ Prisma consumes `DATABASE_URL` env var in order to connect to the database.
4441

4542
Docker compose is used to spin up docker container with Postgres in order to run integration/e2e tests.
4643
To prepare database in docker container perform the next steps:
44+
4745
1. run `npm run docker:up` command to spin up docker container with Postges database;
4846
2. run `npm run prisma:deploy:local` command to create tables in docker container;
4947
3. run `npm run prisma:seed:local` command to fill Postgres database with initial data.
5048

5149
#### Run Tests
5250

5351
1. [db.integration-spec.ts](./test/db.integration-spec.ts) contains integration tests for [Prisma service](./src/db/prisma.service.ts).
54-
- `npm run test:integration` command to run integration tests;
52+
53+
- `npm run test:integration` command to run integration tests;
54+
5555
2. [test/cart.e2e-spec.ts](./test/cart.e2e-spec.ts) contains e2e tests for [Cart controller](./src/cart/cart.controller.ts).
56-
- `npm run test:e2e` command to run e2e tests.
56+
57+
- `npm run test:e2e` command to run e2e tests.
5758

5859
### Postman
5960

60-
Use [postman_collection.json](./postman/Cart%20Service.postman_collection.json) collection to test REST API endpoints with [Postman](https://www.postman.com)
61+
Use [postman_collection.json](./postman/Cart%20Service.postman_collection.json) collection to test REST API endpoints with [Postman](https://www.postman.com)

cdk/.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
DATABASE_USERNAME=
2-
DATABASE_PASSWORD=
2+
DATABASE_PASSWORD=
3+
CART_SERVICE_URL=

cdk/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ The following env var is passed into cart handler in order to provide access to
2222

2323
- `DATABASE_URL` - generated dynamically based on db instance endpoint host/port values + username/password passed via `.env` file.
2424

25+
### HTTP API
26+
27+
Elastic Beanstalk deploy provides HTTP endpoint, but to integrate it with FE it is necessary to have HTTPS endpoint.
28+
In order to provide HTTPS endpoint additional HTTP API is created to proxy requests to EC2 instance of EB application.
29+
The following env var is used to define EB endpoint:
30+
31+
- `CART_SERVICE_URL`
32+
2533
## NPM scripts
2634

2735
- `lint` - runs ESLint with fix option

cdk/src/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export const config = {
99
password: process.env.DATABASE_PASSWORD ?? ''
1010
}
1111
},
12+
servers: {
13+
cart: {
14+
url: process.env.CART_SERVICE_URL ?? ''
15+
}
16+
},
1217
handlers: {
1318
timeout: 10
1419
}

cdk/src/constructs/db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class CartServiceDatabase extends Construct {
5555
vpc: props.network.vpc,
5656
vpcSubnets: props.network.vpcSubnets,
5757
securityGroups: props.network.securityGroups,
58-
publiclyAccessible: true,
58+
publiclyAccessible: false,
5959
port: Number(Port.POSTGRES.toString()),
6060
performanceInsightRetention: PerformanceInsightRetention.DEFAULT,
6161
backupRetention: Duration.seconds(0),

0 commit comments

Comments
 (0)