Git Product home page Git Product logo

aws-api-gateway-ecs-fargate's Introduction

AWS API Gateway + ECS Fargate

This is a sample Terraform AWS project for spinning up an API Gateway + ECS Fargate cluster hosting a microservice. The microservice is versioned as a Docker image on ECR. Logging is provided by CloudWatch.

Architecture, cost optimizations, security, scalability, observability, alternative setups

  1. Architecture

The API Gateway is the entry-point in the cluster, forwarding all the requests to an internal NLB which forwards the requests to the internal ECS Fargate cluster hosting the microservice.

The connectivity between the API Gateway and the internal NLB is achieved through a VPC Link, while the connectivity between the microservice and different AWS services like ECR, S3, and CloudWatch is achieved through VPC Endpoints.

Inventory of AWS services:

  • VPC + route table + private subnets
  • VPC Endpoints (ECR, S3, CloudWatch)
  • Security Group
  • ECS Fargate cluster + service + task definition
  • ECS IAM role + policy
  • NLB + Target Group + Listener
  • CloudWatch
  • API Gateway
  • VPC Link
  1. Cost optimizations

Rather than using a NAT Gateway for enabling traffic between the cluster and ECR/S3/CloudWatch, VPC Endpoints are a cheaper alternative (see https://www.vantage.sh/blog/nat-gateway-vpc-endpoint-savings).

  1. Security

The only public-facing endpoint is the API Gateway, while the NLB and ECS Fargate cluster live inside the private subnets.

The API Gateway provides SSL termination and a default SSL certificate (see https://aws.amazon.com/api-gateway/faqs). The internal connectivity between the NLB and ECS Fargate cluster is not secure.

At the firewall level of the VPC, only the following ports are opened through Security Groups:

  • microservice's port (inbound)
  • HTTPS (inbound/outbound)
  • IPv6 (outbound)
  1. Scalability

see https://docs.aws.amazon.com/AmazonECS/latest/userguide/clusters.html

  1. Observability

Only CloudWatch at this moment, more to come later.

  1. Alternative setups
  • Approximately same costs, Layer 7 routing (not suitable for high throughput and ultra-low latencies): API Gateway + ALB + ECS Fargate
  • More expensive, for senarios where you cannot use VPC Endpoints (e.g. external systems): API Gateway + LB + ECS Fargate + NAT Gateway
  • Cheaper, but less secure (not suitable for production setups): LB + ECS Fargate deployed in public subnets, w/o API Gateway

Local Terraform setup

  1. Create a new AWS profile

~/.aws/credentials, ~/.aws/config

  1. Switch to the new profile

export AWS_DEFAULT_PROFILE=

  1. Initialize Terraform

terraform init

  1. Spin up the infrastructure

terraform apply --var-file="env1.tfvars"

  1. Teardown the infrastructure

terraform destroy --var-file="env1.tfvars"

Live deployment

After Terraform successfully finished provisioning the infrastructure, go to AWS Console -> API Gateway -> Stages and access the public DNS that was made available by API Gateway (e.g. https://.execute-api.eu-west-1.amazonaws.com/dev)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.