Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor infra and migrate to rds data api #25

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ jobs:
aws-access-key-id: AWS_ACCESS_KEY_ID
aws-secret-access-key: AWS_SECRET_ACCESS_KEY
aws-region: AWS_DEFAULT_REGION
- run:
name: Build infra
command: npm run build:infra
- run:
name: Diff
command: npx cdk diff --all > diff.txt && cat diff.txt
Expand All @@ -148,9 +145,6 @@ jobs:
aws-access-key-id: AWS_ACCESS_KEY_ID
aws-secret-access-key: AWS_SECRET_ACCESS_KEY
aws-region: AWS_DEFAULT_REGION
- run:
name: Build infra
command: npm run build:infra
- run:
name: Deploy
command: npx cdk deploy --all --require-approval never
Expand Down
14 changes: 10 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
NODE_ENV=development
APP_PORT=3000
APP_NAME="NestJS Boilerplate"
APP_NAME="Finance Boilerplate"
API_PREFIX=api
APP_FALLBACK_LANGUAGE=en
APP_HEADER_LANGUAGE=x-custom-lang
FRONTEND_DOMAIN=https://localhost:4200
BACKEND_DOMAIN=http://localhost:3000
SWAGGER_ENABLED=true
I18N_DIRECTORY=src/i18n
Expand All @@ -18,9 +19,14 @@ DATABASE_SYNCHRONIZE=false
DATABASE_MAX_CONNECTIONS=100
DATABASE_SSL_ENABLED=false
DATABASE_REJECT_UNAUTHORIZED=false
DATABASE_CA=
DATABASE_KEY=
DATABASE_CERT=

AUTH_JWT_SECRET=secret
AUTH_JWT_TOKEN_EXPIRES_IN=1d

MAIL_TEMPLATES_PATH="templates"
MAIL_FROM="[email protected]"

AWS_STORAGE_CREDENTIAL="profile"
AWS_STORAGE_REGION="us-east-1"

ORGANIZATION_GITHUB_ORGANIZATION_ID="dad87ad4-e90b-4f05-83d7-071757479c11"
32 changes: 32 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
NODE_ENV=production
APP_PORT=3000
APP_NAME="Finance Boilerplate"
API_PREFIX=api
APP_FALLBACK_LANGUAGE=en
APP_HEADER_LANGUAGE=x-custom-lang
FRONTEND_DOMAIN=https://localhost:4200
BACKEND_DOMAIN=http://localhost:3000
SWAGGER_ENABLED=true
I18N_DIRECTORY=i18n

DATABASE_TYPE=postgres
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=postgres
DATABASE_NAME=postgres
DATABASE_SYNCHRONIZE=false
DATABASE_MAX_CONNECTIONS=100
DATABASE_SSL_ENABLED=false
DATABASE_REJECT_UNAUTHORIZED=false

AUTH_JWT_SECRET=secret
AUTH_JWT_TOKEN_EXPIRES_IN=1d

MAIL_TEMPLATES_PATH="templates"
MAIL_FROM="[email protected]"

AWS_STORAGE_CREDENTIAL="profile"
AWS_STORAGE_REGION="us-east-1"

ORGANIZATION_GITHUB_ORGANIZATION_ID="dad87ad4-e90b-4f05-83d7-071757479c11"
63 changes: 1 addition & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,9 @@ Provide a robust backend solution leveraging NestJS with PostgreSQL on AWS infra

- [Installing and Running](docs/installing-and-running.md)
- [Working with database](docs/database.md)
- [Deploy on AWS](docs/deploy.md)
- [Auth](docs/auth.md)

## Features

- :file_folder: Database: This feature uses TypeORM, an ORM (Object-Relational Mapping) library that simplifies the process of working with databases in your application.

- :seedling: Seeding: This feature allows you to seed your database with initial data, which can be useful for testing and development purposes.

- :wrench: Config Service: This feature uses @nestjs/config, a module that helps you manage configuration in your NestJS application.

- :e-mail: Sign in and sign up via email: This feature allows users to sign in and sign up to your application using their email address.

- :key: Use Admin and User roles: This feature allows you to define different roles for users of your application, such as Admin and User, and control access to certain features based on these roles.

- :earth_americas: Internacionalization with I18N: This feature uses nestjs-i18n to support internationalization (I18N) in your application, allowing you to translate your application into different languages.

- :bar_chart: Swagger: This feature uses Swagger, a tool for generating API documentation, to document your application's API.

- :pill: E2E Tests: This feature includes end-to-end (E2E) tests, which test the full stack of your application to ensure it is working as expected.

- :whale: Docker and Docker Composer Files: This feature includes Docker and Docker Compose files, which allow you to containerize your application and run it in a Docker environment.

- :stopwatch: CircleCI: This feature uses CircleCI, a continuous integration and delivery platform, to set up pipelines that build, test, lint, run integration tests, diff, and deploy your code. The pipelines can also be put on hold if needed.

- :construction: Infraestructure as a code with CDK: This feature allows you to define your infrastructure as code using the AWS Cloud Development Kit (CDK), which makes it easier to automate the process of creating and managing your infrastructure.

- :computer: Typescript config in tsconfing.infra.json for CDK: This feature includes a Typescript config file (tsconfing.infra.json) that is used to configure the CDK for your application.

- :earth_americas: Default environments development and production: This feature includes default environments for development and production, which can be used to separate your code and infrastructure for different stages of the development process.

- :file_folder: TypeORM lambda environment: This feature allows you to use TypeORM in a lambda environment, allowing you to run your database operations in a serverless environment.

- :file_folder: Postgres environment: This feature allows you to set up a Postgres database environment, which can be used to store and manage data.

- :key: AWS IAM authentication for database and environment setup: This feature allows you to use AWS IAM (Identity and Access Management) to authenticate and authorize access to the database and environment.

- :loop: Multiple lambda services in the same repository: This feature allows you to store multiple lambda services, or small, independent units of code that are triggered by certain events, in the same repository. This can be useful for organizing your code and making it easier to maintain.

- :file_folder: Common folder for share code between different services: This feature allows you to create a common folder that can be used to share code between different services. This can be useful for reducing duplication and improving code reuse.

- :construction: Multiple infrastructure as a code layers: This feature allows you to define your infrastructure as code, meaning that you can use code to automate the process of creating and managing your infrastructure. The core layer is for database and storage, while the application layer is for lambda and clients.

- :package: ESBuild for compilate small lambda zip: This feature uses ESBuild, a super-fast JavaScript bundler and minifier, to compile small lambda zip files. This can help improve the performance and efficiency of your lambdas.

- :computer: Aurora Serverless V2 and security group: This feature allows you to use Aurora Serverless V2, a fully managed, autoscaling MySQL-compatible database, and a security group, which is a virtual firewall that controls inbound and outbound traffic to your database.

- :pill: Create lambda connection pool with RDS proxy: This feature allows you to create a connection pool, a group of reusable connections that can be used to connect to a database, and use an RDS (Relational Database Service) proxy to manage connections to the database. This can help improve the performance and reliability of your database.

- :rocket: Create lambda for run migrations in staging: This feature allows you to create a lambda function that can be used to run migrations, or changes to the database schema, in the staging environment. This can be useful for testing and debugging your database.

- :train2: API Gateway: This feature allows you to use API Gateway, a fully managed service that makes it easy to create, publish, maintain, monitor, and secure APIs, to connect your backend services to your applications.

- :twisted_rightwards_arrows: Lambda API Gateway proxy to NestJS: This feature allows you to use a lambda function as an API Gateway proxy to connect to NestJS, a modular, fast, and powerful server-side application framework built with TypeScript.

- :notebook: Base repository documentation: This feature provides documentation for the base repository, which can be used as a reference for understanding and using the code in the repository.

- :key: OIDC Service for secure tokens: This feature allows you to use an OIDC (OpenID Connect) service to generate secure tokens, which can be used to authenticate and authorize access to your application.

- :paperclip: You can use Compodoc or TSDoc to generate technical documentation for your methods, or you can manually write documentation in the docs/ folder and update the docs/summary.json file. Compodoc and TSDoc are tools that allow you to easily generate documentation for your codebase.

- :rocket: A contribution template guide has been created for sharing with your team or for contributing to this project.

- :shield: Set up SonarCloud project settings to improve your code security.

## Quick run

```bash
Expand Down
53 changes: 1 addition & 52 deletions cdk.context.json
Original file line number Diff line number Diff line change
@@ -1,52 +1 @@
{
"vpc-provider:account=767397837500:filter.isDefault=true:region=us-east-1:returnAsymmetricSubnets=true": {
"vpcId": "vpc-0a49111bfc7e90ad0",
"vpcCidrBlock": "172.31.0.0/16",
"ownerAccountId": "767397837500",
"availabilityZones": [],
"subnetGroups": [
{
"name": "Public",
"type": "Public",
"subnets": [
{
"subnetId": "subnet-05c72d8557ff1be9d",
"cidr": "172.31.0.0/20",
"availabilityZone": "us-east-1a",
"routeTableId": "rtb-07d8e51a395a7eea8"
},
{
"subnetId": "subnet-0d8d024d5a7e57ab7",
"cidr": "172.31.80.0/20",
"availabilityZone": "us-east-1b",
"routeTableId": "rtb-07d8e51a395a7eea8"
},
{
"subnetId": "subnet-0f7ff3dccf94de46d",
"cidr": "172.31.16.0/20",
"availabilityZone": "us-east-1c",
"routeTableId": "rtb-07d8e51a395a7eea8"
},
{
"subnetId": "subnet-08b89a94b49962951",
"cidr": "172.31.32.0/20",
"availabilityZone": "us-east-1d",
"routeTableId": "rtb-07d8e51a395a7eea8"
},
{
"subnetId": "subnet-03c7ec87f8ba43176",
"cidr": "172.31.48.0/20",
"availabilityZone": "us-east-1e",
"routeTableId": "rtb-07d8e51a395a7eea8"
},
{
"subnetId": "subnet-06f1d5990cb41ffa5",
"cidr": "172.31.64.0/20",
"availabilityZone": "us-east-1f",
"routeTableId": "rtb-07d8e51a395a7eea8"
}
]
}
]
}
}
{}
2 changes: 1 addition & 1 deletion cdk.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"app": "node dist/infra/index"
"app": "npx ts-node --prefer-ts-exts infra/index.ts"
}
78 changes: 33 additions & 45 deletions docs/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,50 +46,39 @@ You can configure your AWS credentials in two ways:

## Step 3: Define Your Infrastructure

1. Open the `infra/index.ts` file and update it with the following content:
1. Open the `infra/configs/production.ts` file and update it with the following content:
```typescript
import { AtlasInfraestructure, createStacks } from 'path-to-your-AtlasInfraestructure-module';

const infrastructure = new AtlasInfraestructure({
production: {
// Name of your application
applicationName: 'atlas',
// Stage name for the environment
stageName: 'production',
// Domain name for the environment
domainName: 'sandbox.slingui.com',
// API domain name for the environment
apiDomainName: 'api.sandbox.slingui.com',
// Public host zone ID for the environment (AWS Route 53 Hosted Zone ID)
idPublicHostZone: 'Z01545163ANT5OQYS99UY',
env: {
// AWS account ID for the environment
account: '767397837500',
// AWS region for the environment (e.g., 'us-east-1')
region: 'us-east-1',
},
layersStack: createStacks(),
},
development: {
// Name of your application
applicationName: 'atlas',
// Stage name for the environment
stageName: 'development',
// Domain name for the environment
domainName: 'sandbox.slingui.com',
// API domain name for the environment
apiDomainName: 'api.sandbox.slingui.com',
// Public host zone ID for the environment (AWS Route 53 Hosted Zone ID)
idPublicHostZone: 'Z01545163ANT5OQYS99UY',
env: {
// AWS account ID for the environment
account: '767397837500',
// AWS region for the environment (e.g., 'us-east-1')
region: 'us-east-1',
},
layersStack: createStacks(),
},
});
export const production: ApplicationProps = {
applicationName: 'FinanceBaas',
stageName: 'production',
env: {
account: '025066284119',
region: 'us-east-1',
},
githubOrganizationId: environment.parsed.ORGANIZATION_GITHUB_ORGANIZATION_ID,
layersStack: stacks,
applications: {
core: {
domainName: 'boilerplate.atlascli.io',
apiDomainName: 'api.boilerplate.atlascli.io',
idPublicHostZone: 'Z03396972KP6M49QCZJPD',
applicationEnvironment: {
NODE_ENV: environment.parsed.NODE_ENV || 'development',
APP_PORT: environment.parsed.APP_PORT || '3000',
APP_NAME: environment.parsed.APP_NAME || 'NestJS Boilerplate',
API_PREFIX: environment.parsed.API_PREFIX || 'api',
APP_FALLBACK_LANGUAGE: environment.parsed.APP_FALLBACK_LANGUAGE || 'en',
APP_HEADER_LANGUAGE: environment.parsed.APP_HEADER_LANGUAGE || 'x-custom-lang',
BACKEND_DOMAIN: environment.parsed.BACKEND_DOMAIN || 'http://localhost:3000',
FRONTEND_DOMAIN: environment.parsed.BACKEND_DOMAIN || 'http://localhost:4200',
SWAGGER_ENABLED: environment.parsed.SWAGGER_ENABLED || 'true',
I18N_DIRECTORY: environment.parsed.I18N_DIRECTORY || 'src/i18n',
AUTH_JWT_SECRET: environment.parsed.AUTH_JWT_SECRET || 'secret',
AUTH_JWT_TOKEN_EXPIRES_IN: environment.parsed.AUTH_JWT_TOKEN_EXPIRES_IN || '1d',
},
},
},
};

// To start this repository on AWS, you need to have a Hosted Zone on Route53 on AWS,
// this is important because we generate all the necessary certificates and publish
Expand All @@ -104,8 +93,7 @@ If you change the applicationName in infra/index.ts, you should update the SESSI
1. Specify the profile and deploy your stack:
```bash
npm run build
npm run build:infra
npx cdk deploy --all --profile AdministratorAccess-767397837500
npx cdk deploy --all --profile AdministratorAccess-XXXXXXXXXXXX
```

## Step 5: Run Your Lambda Function to run migrations
Expand Down
41 changes: 41 additions & 0 deletions infra/configs/production.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ApplicationProps } from '.././props/application-props';
import { stacks } from '../stacks';
import { config } from 'dotenv';

const environment = config({ path: '.env.production' });

export const production: ApplicationProps = {
applicationName: 'FinanceBaas',
stageName: 'production',
env: {
account: '025066284119',
region: 'us-east-1',
},
githubOrganizationId: environment.parsed.ORGANIZATION_GITHUB_ORGANIZATION_ID,
layersStack: stacks,
applications: {
core: {
domainName: 'finance.atlascli.io',
apiDomainName: 'api.finance.atlascli.io',
idPublicHostZone: 'Z03396972KP6M49QCZJPD',
applicationEnvironment: {
NODE_ENV: environment.parsed.NODE_ENV || 'development',
APP_PORT: environment.parsed.APP_PORT || '3000',
APP_NAME: environment.parsed.APP_NAME || 'NestJS Boilerplate',
API_PREFIX: environment.parsed.API_PREFIX || 'api',
APP_FALLBACK_LANGUAGE: environment.parsed.APP_FALLBACK_LANGUAGE || 'en',
APP_HEADER_LANGUAGE:
environment.parsed.APP_HEADER_LANGUAGE || 'x-custom-lang',
BACKEND_DOMAIN:
environment.parsed.BACKEND_DOMAIN || 'http://localhost:3000',
FRONTEND_DOMAIN:
environment.parsed.BACKEND_DOMAIN || 'http://localhost:4200',
SWAGGER_ENABLED: environment.parsed.SWAGGER_ENABLED || 'true',
I18N_DIRECTORY: environment.parsed.I18N_DIRECTORY || 'src/i18n',
AUTH_JWT_SECRET: environment.parsed.AUTH_JWT_SECRET || 'secret',
AUTH_JWT_TOKEN_EXPIRES_IN:
environment.parsed.AUTH_JWT_TOKEN_EXPIRES_IN || '1d',
},
},
},
};
3 changes: 2 additions & 1 deletion infra/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const DEFAULT_STAGE_NAME = 'development';
// TODO CHANGE ME
export const DEFAULT_STAGE_NAME = 'production';
export const vpcCDIR = '10.0.0.0/16';
36 changes: 0 additions & 36 deletions infra/constructs/api-gateway/api-gateway.construct.ts

This file was deleted.

Loading