Express API developed in Typescript and deployed to ECS Fargate that sits behind a private Application Load Balancer and is exposed to the world using an Http API Gateway. This repo includes the CloudFormation template the defines all the resources.
- The database has been configured to reduce expenses.
- The CodeBuild project has been configured to reduce expenses.
- The VPC configuration might not fit your requirements.
- The AutoScaling policy might not fit your requirements.
To run the application execute the following procedure:
- Set up the database connection
echo "DATABASE_URL=postgresql://pets:pets123@localhost:5432/petsdb" > ./prisma/.env
- Install the dependencies
npm install
- Start the database container
npm run docker:up
- Create the database tables
npm run prisma:db:push
- Start the application. The application will be listening on port 3000.
npm run start
To run the application in watch mode execute the following commands:
npm run start:watch
Asumming that you executed the How to run the application procedure up to the step 4, to run the application in a container execute the following steps:
- Reset the database connection. For communication between containers, you need to use the container name as the host instead of localhost.
echo "DATABASE_URL=postgresql://pets:pets123@postgres:5432/petsdb" > ./prisma/.env
- Build the docker image
npm run docker:build
- Run the docker image. The container will be listening on port 80.
npm run docker:run
- NatGateway
- RDS
- Secrets Manager
- AWS account
- AWS CLI
- DockerHub account, you can use a personal account (free).
To deploy and run the solution in AWS, execute the following procedure:
- Fork this repo.
- Clone the forked repo in your machine.
- Create create a .env file in the root of the project. The contents of this file will include the parameters for the CloudFormation template:
- CodestarConnectionId: To generate the Codestar Connection, follow this tutorial: Create connection. The value should be a UUID.
- FullRepositoryId: Specify the owner and name of the repository where source changes are to be detected. Example: user/your-forked-repo.
- BranchName: Specify the name of the branch where source changes are to be detected.
- ArtifactStoreS3Location: Specify the name of the S3 bucket to store the CodePipeline artifacts. Your AWS account should have an already created S3 bucket for codepipeline. The name follows this format: codepipeline-{region}-{random-number}
- AutoScalingMinCapacity: Specify the maximum value that you plan to scale out to. If this is the first deployment, set the value to 0.
- AutoScalingMaxCapacity: Specify the minimum value that you plan to scale in to. If this is the first deployment, set the value to 0.
- DockerHubUsername: Specify the DockerHub username to be used when building the docker image.
- DockerHubPassword: Specify the DockerHub password to be used when building the docker image.
The contents of the .env file should look like this:
CodestarConnectionId=b3d48678-dd7e-4b99-bc4f-44f5571cac98
FullRepositoryId=user/your-forked-repo
BranchName=master
ArtifactStoreS3Location=codepipeline-us-east-1-96582316541
AutoScalingMinCapacity=0
AutoScalingMaxCapacity=0
DockerHubUsername=dockerhub-username
DockerHubPassword=dockerhub-password
- Verify that the VPC CIDR configuration defined in the CloudFormation template does not conflict with any other VPC in your AWS account.
- Make sure the credentials that you will use to deploy the CloudFormation stack have enough permissions to create all of the resources.
- Deploy the CloudFormation stack:
npm run cf:stack:deploy
- Check that the pipeline executes successfully.
- Change the AutoScalingMinCapacity and AutoScalingMaxCapacity to for example 1 and 2 respectively in the .env file.
- Redeploy the CloudFormation stack:
npm run cf:stack:deploy
- 🎊 Start making requests to the API. To obtain the API Invoke URL, run the following command. The API Invoke URL is in the Outputs section.
npm run cf:stack:describe
- If you don't want the solution deployed anymore, delete the CloudFormation stack. Make sure the ECR repository and the open-api bucket are empty.
npm run cf:stack:delete