Skip to content

gonzaloplaza/cognito-workshop

Repository files navigation

Cognito Workshop REST API

TypeScript GitHub Repo Size License MIT

This is another Express + TypeScript + DDD (Domain Driven Design patterns) + IoC/DI (Inversion of control and Dependency injection) + API REST boilerplate.

Author: Gonzalo Plaza gonzaloplazac@gmail.com

Installation and Configuration

yarn install

Generate .env file

cp .env.dist .env

You have to configure local environment variables with your own parameters inside .env file. These are the default values:

PORT=3000
APP_NAME="cognito-workshop"
APP_LOG_LEVEL="debug"
AWS_ACCESS_KEY_ID="your_aws_access_key_id"
AWS_SECRET_ACCESS_KEY="your_aws_secret_access_key"
COGNITO_USER_POOL="your_cognito_user_pool"
COGNITO_CLIENT_ID="your_cognito_client_id"
COGNITO_REGION="your-aws-region-x"

Development with nodemon (Runs on 3000 port)

yarn dev

Run tests and generate coverage report

yarn test #In progress

Build/Compile JS (to /dist folder)

yarn build

Authentication with AWS Cognito

AWS Cognito is implemented as default authentication service. Please, check documentation on how to configure it for more details: https://aws.amazon.com/cognito/getting-started/


Docker Compose (for development)

This project is ready to work with docker-compose 3.8 to initialize the needed stack during development process. To start working run the following commands:

docker-compose build
docker-compose up

To shut down local stack:

docker-compose down

Working docker compose environment (Port 8080)

curl http://localhost:8080/

Example JSON response (/):

{
  "success": true,
  "message": "Hello Cognito Workshop",
  "timestamp": 1648208765884
}

Build Docker image for production

You can build an optimized Docker production-ready image with the standard command:

docker build --no-cache -t cognito-workshop .

And then run the container passing environment variables within the initialization:

docker run --rm -it -p 3000:3000 \
  -e NODE_ENV=production \
  -e AWS_ACCESS_KEY_ID="your_aws_access_key_id" \
  -e AWS_SECRET_ACCESS_KEY="your_aws_secret_access_key" \
  -e COGNITO_USER_POOL="your_cognito_user_pool" \
  -e COGNITO_CLIENT_ID="your_cognito_client_id" \
  -e COGNITO_REGION="your-aws-region-x" \
  -e APP_LOG_LEVEL="info" \
  --name cognito-workshop cognito-workshop

Deploy Stack to AWS

In order to create/deploy the current stack to AWS using CloudFormation you'll need to generate your own .aws-envs file that you can copy from the provided template:

cp .aws-envs.example .aws-envs

Note: This process will need a proper role created into your AWS Account with enough permissions to create all needed resources, including IAM roles. You will need also to specify a working AWS VPC and 2 subnets. To execute it:

yarn deploy-stack # This is valid for stack create or update.

And to remove stack from cloudformation:

yarn remove-stack

Todo

  • Decouple dependency on aws key and secret variables
  • Link AWS LoadBalancer to Route53 domain and SSL certificate
  • Add tests and coverage

Help and references