forked from rolling-scopes-school/nodejs-aws-cart-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rolling-scopes-school#9: Application deployment via Elastic Beanstalk…
… (Docker platform) (#2) * 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 * rolling-scopes-school#8: Integrate wiht AWS RDS (Postgres) (#1) * 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: 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
- Loading branch information
1 parent
57c4190
commit 77f4309
Showing
18 changed files
with
225 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.git/ | ||
# Contains AWS CDK stack declaration, not used during docker build | ||
cdk/ | ||
# Contains compiled project files, will be compiled during docker build | ||
dist/ | ||
node_modules/ | ||
# Contains Postman collection for REST API testing, not used during docker build | ||
postman/ | ||
# Contains tests, not run during docker build | ||
test/ | ||
.dockerignore | ||
.DS_Store | ||
.env* | ||
.eslintrc.js | ||
.gitignore | ||
.prettierrc | ||
docker-compose.yml | ||
Dockerfile | ||
nest-cli.json | ||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
option_settings: | ||
aws:elasticbeanstalk:cloudwatch:logs: | ||
StreamLogs: true | ||
DeleteOnTerminate: true | ||
RetentionInDays: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Ignore everything | ||
* | ||
# Except | ||
!.ebextensions | ||
!migrations | ||
!prisma | ||
!src | ||
!.dockerignore | ||
!Dockerfile | ||
!package* | ||
!tsconfig* | ||
!webpack.config* | ||
|
||
cdk/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
DATABASE_USERNAME=admin | ||
DATABASE_PASSWORD=123456 | ||
DATABASE_URL=postgresql://admin:123456@localhost:5432/admin?schema=public | ||
DATABASE_URL=postgresql://admin:123456@localhost:5432/admin?schema=public | ||
SERVER_PORT=4000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# syntax=docker/dockerfile:1 | ||
FROM node:20-alpine AS base | ||
|
||
FROM base AS install-dev | ||
WORKDIR /tmp/dev | ||
COPY package.json package-lock.json ./ | ||
RUN npm ci | ||
|
||
FROM base AS install-prod | ||
WORKDIR /tmp/prod | ||
COPY package.json package-lock.json ./ | ||
RUN npm ci --omit dev | ||
|
||
FROM install-dev AS prisma | ||
COPY prisma ./prisma | ||
COPY migrations ./migrations | ||
RUN npm run prisma:generate | ||
|
||
FROM prisma AS build | ||
ENV NODE_ENV=production | ||
COPY tsconfig* webpack.config.js ./ | ||
COPY src ./src | ||
RUN npm run build | ||
|
||
FROM base | ||
WORKDIR /app | ||
COPY --from=install-prod /tmp/prod/node_modules/@prisma ./node_modules/@prisma/ | ||
COPY --from=install-prod /tmp/prod/node_modules/prisma ./node_modules/prisma/ | ||
COPY --from=install-prod /tmp/prod/node_modules/.bin ./node_modules/.bin/ | ||
|
||
COPY --from=prisma /tmp/dev/node_modules/.prisma ./node_modules/.prisma/ | ||
COPY --from=prisma /tmp/dev/migrations ./migrations/ | ||
COPY --from=prisma /tmp/dev/prisma ./prisma/ | ||
|
||
COPY --from=build /tmp/dev/dist/main.js ./dist/ | ||
COPY --from=build /tmp/dev/dist/seed.js ./dist/ | ||
COPY --from=build /tmp/dev/package.json ./ | ||
EXPOSE 4000 | ||
CMD ["dist/main.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
DATABASE_USERNAME= | ||
DATABASE_PASSWORD= | ||
DATABASE_PASSWORD= | ||
CART_SERVICE_URL= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { CfnOutput } from 'aws-cdk-lib' | ||
import { Cors } from 'aws-cdk-lib/aws-apigateway' | ||
import { CorsHttpMethod, HttpApi, HttpMethod } from 'aws-cdk-lib/aws-apigatewayv2' | ||
import { HttpUrlIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations' | ||
import { Construct } from 'constructs' | ||
|
||
interface CartServiceHttpApiProps { | ||
serverUrl: string | ||
} | ||
|
||
export class CartServiceHttpApi extends Construct { | ||
constructor(scope: Construct, id: string, props: CartServiceHttpApiProps) { | ||
super(scope, id) | ||
|
||
const integration = new HttpUrlIntegration( | ||
'CartServiceHttpUrlIntegration', | ||
`${props.serverUrl}/{proxy}` | ||
) | ||
|
||
const api = new HttpApi(this, 'CartServiceHttpApi', { | ||
description: 'HTTP API to proxy requests to EC2 instance of Elastic Beanstalk application', | ||
corsPreflight: { | ||
allowOrigins: Cors.ALL_ORIGINS, | ||
allowHeaders: Cors.DEFAULT_HEADERS, | ||
allowMethods: [ | ||
CorsHttpMethod.GET, | ||
CorsHttpMethod.OPTIONS, | ||
CorsHttpMethod.POST, | ||
CorsHttpMethod.PUT | ||
] | ||
} | ||
}) | ||
api.addRoutes({ | ||
path: '/{proxy+}', | ||
methods: [HttpMethod.ANY], | ||
integration: integration | ||
}) | ||
|
||
new CfnOutput(this, 'CartServiceHttpApiUrl', { value: api.apiEndpoint }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.