Skip to content

MatthewCYLau/node-aws-fargate-terraform

Repository files navigation

AWS Fargate Node App

A reference project to deploy a Node Express app onto Amazon ECS on AWS Fargate with Terraform, inspired by this tutorial documentation

A microservice which creates, and authenticates users from a MongoDB database

AWS Architecture

Pre-requisite

  • Make sure you have installed Terraform, AWS CLI, and configured a default AWS CLI profile (see doc here)
terraform -help # prints Terraform options
which aws # prints /usr/local/bin/aws
aws --version # prints aws-cli/2.0.36 Python/3.7.4 Darwin/18.7.0 botocore/2.0.0
aws configure # configure your AWS CLI profile
  • You have created a database on MongoDB Atlas and have obtained a database connection string

Configuration

  • Create a Github project, and generate a personal access token (see doc here)

  • Create an S3 bucket to store Terraform state. Populate bucket name in 01-main.tf

  • Create a secret on AWS Secrets Manager named DockerHubAccessToken with key DOCKER_HUB_ACCESS_TOKEN, and your Docker access token as value

  • Create a secret on AWS Secrets Manager named MongoPassword with key MONGO_PASSWORD, and your MongoDB password as value

  • Populate terraform.tfvars:

default_region            = "us-east-1"
docker_username           = "matlau"
github_username           = "MatthewCYLau"
github_project_name       = "node-aws-fargate-terraform"
app_name                  = "node-aws-fargate-app"
environment               = "production"
mongo_username            = "admin-matlau"
mongo_host                = "mattewcylau-5ltcp.mongodb.net"
mongo_database_name       = "node-aws-fargate-app"

Deploy

cd deploy # change to deploy directory
terraform init # initialises Terraform
terraform apply # deploys AWS stack. See output for AWS loadbalancer DNS name
terraform destroy # destroys AWS stack

When prompted for github_token, provide the value and hit Return. Alternatively, create a local environment variable named TF_VAR_github_token

Usage

  • Create a user by making POST request to /api/users with the following JSON body:
{
  "email": "[email protected]",
  "password": "password",
  "name": "jondoe"
}
  • See Postman collection here

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

If you find this project helpful, please give a ⭐ or even better buy me a coffee ☕ 👇 because I'm a caffeine addict 😅

Buy Me A Coffee

License

MIT

About

Deploy a Node Express app onto Amazon ECS on AWS Fargate

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published