Giter Club home page Giter Club logo

terraform-vs-cdk's Introduction

Terraform vs AWS CDK - Comparison by building 2-tier Web Application

Source code for article: https://rwilinski.me/blog/cdk-vs-terraform/

Goal of this repo is to compare provisioning of 2-tier web app with CDK and Terraform.

Intro

What we'll be building: Infra

Key points:

  • VPC with two subnets (public and private) in each AZ, IPGW in Public, NAT Gateway in private
  • ECS Cluster in Public Subnet running Fargate cluster with Node.js service and task
  • Application Load Balancer exposed to the public connected to the Fargate service
  • Public S3 bucket with assets
  • IAM Role for task to manipulate S3 Bucket
  • Serverless RDS Aurora MySQL-compatible instance in private subnet, accessible only from ECS tasks

For this comparison's sake we'll need a Docker image with a simple Express.js stored in ECR. Simply execute ./build-and-push.sh to create an AWS Elastic Container Registry, build the Docker image and push it to the cloud.

Prerequisites

  • AWS CLI
  • AWS CDK
  • Docker
  • Terraform

CDK

  • Imperative (consequences? code flow etc.)
  • Can be written in TS, Java, Python
  • Make sure that CDK and construct libraries are the same version
  • API Changes pretty quickly, examples are outdated and require some adjustments like loadBalancer.loadBalancerDnsName vs loadBalancer.dnsName`
  • Nested Stacks and 200 resources limit is cumbersome, still not resolved aws/aws-cdk#239
  • Can write custom resources which are like modules in Terraform
  • Some of the constructs are really explicit making you wonder how you should correctly plug all the parts
  • With higher level of abstraction you end up having more circular dependencies, e.g. ECS Service wants Database URL but database's SG needs to know from which SG it should allow the traffic

Initialization

cdk init app --language=typescript

We'll need ECS, EC2, S3 and RDS packages:

npm i @aws-cdk/aws-ecs @aws-cdk/aws-ec2 @aws-cdk/aws-s3 @aws-cdk/aws-rds

To render CloudFormation file:

npm run build && cdk synth

To deploy

cdk deploy

Terraform

...

➜  find ./cdk/lib -name "*.ts" | xargs cat | wc -l
105
➜  find ./terraform -name "*.tf" | xargs cat | wc -l
516

terraform-vs-cdk's People

Contributors

rafalwilinski avatar

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.