Skip to content

Commit

Permalink
Initial repo
Browse files Browse the repository at this point in the history
  • Loading branch information
hanhdt committed Jan 14, 2024
0 parents commit 226eff7
Show file tree
Hide file tree
Showing 84 changed files with 3,691 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.vscode/*
node_modules/
dist/
temp/
deploy/
global-s3-assets/
regional-s3-assets/
*.build.log
tsconfig.tsbuildinfo
*.zip
.pnpm-debug.log
pnpm-lock.yaml
repo-state.json
app-config-dev.json
app-config-prod.json
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Boilerplate CHANGELOG.md

## 1.0.0

### Notes

- [X] Docs: add CHANGELOG.md
- [X] Docs: What to do after cloned the boilerplate
- [X] Docs: for updating source/common/config/rush/deploy.json
- [X] Remove .vscode from git
- [X] Fix: The location of compiled temp file problem
- [X] Logger: Declare/export logger in utils, can set level and can be used directly
- [X] Folder structure: The structure of service source (src)
- [X] Folder structure: The naming of source, want to plus "handler" to api controller
- [X] Fix: cdk deploy same s3/bootstrapping problem
- [X] Fix: cdk deploy can set env parameters
- [X] Create CDK custom template
- [X] Test: add integration test using Cucumber.js
- [X] Feat: add destroy stacks script
- [X] Test: add code coverage report using Jest
- [X] Feat: add build stacks script and publish packages to npm registry
- [X] Util: managing environment variables of business service
68 changes: 68 additions & 0 deletions PREPARE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# PREPARE

## jq

```sh
$sudo apt update
$sudo apt install -y jq
```

## aws-cli

```sh
$curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
$unzip awscliv2.zip
$sudo ./aws/install
$/usr/local/bin/aws --version
aws-cli/2.11.2 Python/3.11.2 Linux/4.14.305-227.531.amzn2.x86_64 exe/x86_64.amzn.2 prompt/off
```

## nodejs

```sh
$nvm install 16
$nvm use 16
Now using node v16.19.1 (npm v8.19.3)

$node --version
v16.19.1
```

## pnpm

```sh
npm install -g [email protected]
npm install -g [email protected]
```

## git

```sh
$git --version
git version 2.39.2
```

## rush

```bash
$git config --global user.name "sample"
$git config --local user.email "[email protected]"
$npm install -g @microsoft/rush
$git clone https://github.com/microsoft/rushstack
$cd rushstack
$rush update
$rush rebuild
$rush build
$rush rebuild --verbose
```

## aws profile(if necessary)

```bash
aws configue
cat ~/.aws/credentials
[defaule]
aws_access_key_id=xxx
aws_secret_access_key=yyy
aws_session_token=zzz
```
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Monorepo Microservices Boilerplate

This is microservices boilerplate for monorepo with CDK and Rush.

- CDK is used for producing CloudFormation output and microservice deployments
- Rush.js is used for managing dependencies and compiling multiple business service repos

![CDK Intro](docs/assets/aws_cdk_intro.png)

![Rush Monorepo](docs/assets/monorepo.png)

## 1. Get started

### 1.1. Clone the boilerplate repo from Github

```bash
$git clone https://github.com/hanhdt/ts-microservices-rush-boilerplate.git
```

### 1.2. Rename the boilerplate repo to your own repo name

## 2. Prerequisites

- see PREPARE.md

### AWS Account & IAM User

First of all, AWS Account and IAM User is required. IAM user's credential keys also are required.

### Dependencies

To execute this template codes, the following modules must be installed.

- AWS CLI: aws --version

- Node.js: node --version

- AWS CDK: cdk --version

- Rush: rush --version

## 3. Setup target environment configuration (for example: dev)

![app-config-demo](docs/assets/app-config-sample.png)

Because we need to ensure that a single source code is maintained, all configurations are managed in config/app-config-[your-suffix].json. And several files are made according to the environment you want to deploy to, and you have to choose one of them when deploying.
For example, if you want to deploy to dev environment, you have to create `config/app-config-dev.json`

- reference to config/app-config-sample.json

## 4. Initial setup and bootstrap

Execute the following command:

```bash
$./script/initial_setup.sh config/app-config-dev.json
```

## 5. Init a new service repo (for example: sample-service)

<img src="docs/assets/service-repo-sample.png" height="640px">

To create new service repository, do the following step:

- Change `App` to your new `service-folder` name in `config/app-config-[environment].json`

- Execute the following command:

```bash
$./script/create_new_service.sh config/app-config-dev.json <service-folder>
```

- Rename `@hanhdt/sample-service` in `package.json` to your new `service-name`

## 6. Deploy procedure

To deploy stacks, run the following commands:

```bash
$./script/deploy_stacks.sh config/app-config-dev.json
```

Read more details about adding Rush configuration at `script/README.md`

## 7. Local Test

- see source/package/services/sample-service/README.md

## Appendixes

### Fix error: "SSM parameter /cdk-bootstrap/hnb659fds/version not found" in the Step 7

```bash
$cd source/packages/services/<appName>
$cdk deploy --no-previous-parameters -c config=dev
```

### Create local configuration file

The `.env` file will be placed at the service folder.
And it is scoped to the service.

You can put `.env` file in the `source/packages/services/<ServiceName>/config` folder.

```bash
$cd source/packages/services/<ServiceName>/config
$touch .env
```
15 changes: 15 additions & 0 deletions config/app-config-sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"AWSAccountID": "<accountId>",
"AWSProfileName": "<profileName>",
"AWSProfileRegion": "ap-northeast-1",

"Solution": "<solutionName>",
"Environment": "dev",
"App": "<appName>",

"Version": "0.0.1",
"Build": "0",
"QualifierCustom": "hnb659fds",

"LogLevel": "info"
}
Binary file added docs/assets/add-rush-configs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/app-config-sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/aws_cdk_intro.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/monorepo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/service-repo-sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions script/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Deployment services

## 1. Update Rush configuration files

Add your service project to

* source/rush.json
* source/common/config/rush/deploy.json

![Add Rush Config](../docs/assets/add-rush-configs.png)

## 1. Quick deploy

This script will deploy all services under `source/packages/services` folder and its infrastructure to AWS.

```sh
$cd ~/ts-monorepo-microservices-boilerplate
$chmod +x ./deployment/*.sh
$./deployment/deploy_stacks.sh config/app-config-dev.json
```

## 2. Single service deploy

If you want to deploy a single service, you can use the following command.

```sh
$cd ~/source/packages/services/sample-service
$npm run deploy:[dev|prod]
```

## 3. Confirm deployed services

```sh
$ curl --location --request GET 'https://x6wlyex6pg.execute-api.ap-northeast-1.amazonaws.com/prod/canframes/?sort=asc' --header 'Accept: application/vnd.aws-cdf-v1.0+json' --header 'Content-Type: application/vnd.aws-cdf-v1.0+json' --header 'Content-Type: text/plain'
{"canframemodels":[{"year":"2023","model":"sample1-01"},{"year":"2023","model":"sample1-02"}]}


$ curl --location --request GET 'https://wnsjf6x03g.execute-api.ap-northeast-1.amazonaws.com/prod/canframes/?sort=asc' --header 'Accept: application/vnd.aws-cdf-v1.0+json' --header 'Content-Type: application/vnd.aws-cdf-v1.0+json' --header 'Content-Type: text/plain'
{"canframemodels":[{"year":"2023","model":"sample2-01"},{"year":"2023","model":"sample2-02"}]}
```

## 4. Destroy all services

```sh
$cd ~/ts-monorepo-microservices-boilerplate
$chmod +x ./deployment/*.sh
$./script/destroy_stacks.sh config/app-config-dev.json
```
42 changes: 42 additions & 0 deletions script/build_stacks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

# Configuration File Path
export GLOBAL_APP_CONFIG=$1
export PROJECT_STAGE=$(cat $GLOBAL_APP_CONFIG | jq -r '.Environment') #ex> development
export SOLUTION_NAME=$(cat $GLOBAL_APP_CONFIG | jq -r '.Solution')
export APP_NAME=$(cat $GLOBAL_APP_CONFIG | jq -r '.App')

currntPWD=`pwd`
cd ${currntPWD}/source/

echo ==--------CheckDedendencies---------==
aws --version
node --version
npm --version
cdk --version
rush version
pnpm --version
git --version
jq --version

echo ==--------ConfigInfo---------==
echo $GLOBAL_APP_CONFIG
echo $AWS_PROFILE
echo $PROJECT_STAGE
echo $SOLUTION_NAME
echo $APP_NAME
echo .
echo .

echo ==--------BuildEnvironment---------==
# rush purge
rush install
rush update
rush rebuild

echo .
echo .

echo "Build Stacks Completed"

exit
29 changes: 29 additions & 0 deletions script/create_new_service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Configuration File Path
export GLOBAL_APP_CONFIG=$1
export SERVICE_NAME=$2
export SOLUTION_NAME=$(cat $GLOBAL_APP_CONFIG | jq -r '.Solution')
export APP_NAME=$(cat $GLOBAL_APP_CONFIG | jq -r '.App')
export PROJECT_STAGE=$(cat $GLOBAL_APP_CONFIG | jq -r '.Environment') #ex> development

echo ==--------ConfigInfo---------==
echo "$GLOBAL_APP_CONFIG"
echo "Profile: $AWS_PROFILE"
echo "Env: $PROJECT_STAGE"
echo "Solution: $SOLUTION_NAME"
echo "App: $APP_NAME"
echo "Service: $SERVICE_NAME"
echo .
echo .

echo ==--------CreateServiceRepo---------==
currntPWD=`pwd`
cd ${currntPWD}/source/packages/services
mkdir $SERVICE_NAME
SAMPLE_REPO_DIR="${currntPWD}/source/packages/services/sample-service"
cd $SAMPLE_REPO_DIR
tar cf - --exclude="./dist" --exclude="./node_modules" --exclude="./cdk.out" . | (cd ${currntPWD}/source/packages/services/$SERVICE_NAME && tar xvf -)
echo .
echo .
exit
Loading

0 comments on commit 226eff7

Please sign in to comment.