Skip to content

AWS ECS running a simple Dockerised NodeJS app on Fargate with LoadBalancer, Route53 URL, WAFv2, AppSync and GraphQL.

Notifications You must be signed in to change notification settings

joe-glasgow/aws-ecs-simplewebsite

Repository files navigation

AWS ECS Simple Website with AppSync!

This Project is a boiler plate for running a secure nodejs webapp through ECS using Fargate with CDK.

A backend is provided through GraphQL AppSync.

Getting up and running!

Configure your aws-cli and CDK

Be sure how you know how run CDK in context

Build the GraphQL stack

The graphQL stack creates an Aurora DB instance that interfaces with AppSync GraphQL for saving and retrieving data.

Build GraphQL Stack first to set API Key and GraphQL URL in param store

cdk deploy GraphQLStack -c domain=<sub.yourdomain.com> -c aws-access-key=$(aws configure get aws_access_key_id) -c aws-secret-access-key=$(aws configure get aws_secret_access_key) -c region=eu-west-1

Build the Webapp stack

The webapp stack runs a simple NodeJS server instance with micro-service which consumes the graphql endpoint. The app is loadbalanced with its container hosted on Fargate and protected using WAF rules.

Build WebappStack that provides the secrets to allow aws cli to retrieve params set in AppSync GraphQL Stack

cdk deploy WebappStack -c domain=<sub.yourdomain.com> -c aws-access-key=$(aws configure get aws_access_key_id) -c aws-secret-access-key=$(aws configure get aws_secret_access_key) -c region=eu-west-1

The cdk.json file tells the CDK Toolkit how to execute your app.

Setup domains and Route53

Register or transfer a domain using Route53.

Follow the steps to create a Hosted Zone

Tip:

Create accounts for development/testing/production

I wished to have some sub-domains for various development/testing and production environment so this tutorial helped set that up!

Certificate Creation for HTTPS

Uses the CDK method DnsValidatedCertificate which authorizes your certificates without having to manually approve. This requires the previous step completed for domains in Route53.

ECS Registry, Cluster, Tasks, Service and Docker

WebappStack is deployed to ECS and the task is run based on the Dockerfile in the simple directory.

This application uses the ECS Patterns for an application load balanced fargatge service

Testing the application

TODO

CI Integration

TODO

Local Development

To run the Webapp with GraphQL instances locally with Docker:

  1. Deploy the GraphQL stack as described above
  2. Change directory to the simple application cd simple
  3. Export the required variables used by the Docker instance $ export AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id) $ export AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key)
  4. Build the docker image providing the new env variables e.g. $ docker build --build-arg AWS_ACCESS_KEY_ID --build-arg AWS_SECRET_ACCESS_KEY .
  5. Get the latest docker image id $ docker image ls
  6. Run the latest image id and bind port 3000 $ docker run -p 3000:3000 <docker image id from step 5>

Useful commands

  • npm run build compile typescript to js
  • npm run watch watch for changes and compile
  • npm run test perform the jest unit tests
  • cdk deploy -c domain=<INSERT DOMAIN NAME HERE> deploy this stack to your default AWS account/region, providing a domain name
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template