Top reasons why we use AWS CDK over CloudFormation
Tags: aws, awscdk, cloudformation, devops
Published: 2019-11-12 02:58:59

<!-- wp:paragraph -->
<p>If you're working with <strong>AWS Infrastructure</strong>, you may know that currently there are some tools/frameworks support to implement your AWS infrastructure such as <a href="https://aws.amazon.com/cloudformation/">CloudFormation</a>, <a href="https://www.terraform.io/">Terraform</a>, <a href="https://docs.aws.amazon.com/cdk/latest/guide/home.html">AWS CDK</a>. What is the best tool that your team can rely on and use? What is the best tool can help you increase productivity and quality? Do you have the answer? If not, read this post, I will give you our answer and reasons.</p>
<!-- /wp:paragraph -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:paragraph -->
<p>Our team has been working with <strong>AWS</strong> for a couple of years. We have designed and implemented infrastructure components including <strong>networking</strong>, <strong>server</strong>, <strong>storage</strong>, and other AWS services either <strong>manually</strong> or <strong>automation</strong>. Before using A<strong>WS CDK</strong>, we have used <strong>AWS CloudFormation</strong>, but working with it is a challenge, hard to define and get an overview for a complex template in either <strong>JSON</strong> or <strong>YAML</strong> format. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this post, I will not provide specific detail of <strong>AWS CDK.</strong> If you don't know what it is, then check out the <a href="https://docs.aws.amazon.com/cdk/latest/guide/home.html">AWS CDK home page</a> to get some overviews and see how it works.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":419,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://i1.wp.com/blog.innomizetech.com/wp-content/uploads/2019/11/Screen-Shot-2019-11-11-at-9.40.30-PM.png?fit=640%2C514&amp;ssl=1" alt="AWS CDK sample" class="wp-image-419"/><figcaption>Sample code how to create a VPC on AWS with AWS CDK</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>We select AWS CDK as the <strong>primary</strong> tool for our Infrastructure as Code (IaC) because of the following reasons:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>With AWS CDK we "WRITE LESS AND DO MORE</strong>"</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We first impressed <strong>AWS CDK</strong>, when we try to find alternative ways to reduce complexity and overcome challenges of using <strong>CloudFormation</strong>. For example, we have a CloudFormation template to define and configure networking resources including VPC, subnets, route tables, security groups, bastion hosts, integrate gateway, nat gateways, etc. We need to write around <strong>1000 lines of code</strong> in JSON format using CloudFormation. When we try with AWS CDK, we only need to write around <strong>50 lines of code</strong>. As you can see, <strong>AWS CDK can do the same thing, even add more features such as conditional number of NAT gateway, number of subnets and availability zones.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The following code will create all required resources that allow you to create a new VPC on AWS in minutes, you can then modify or add more resources depends on your requirements:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>import * as cdk from '@aws-cdk/core';
import * as ec2 from '@aws-cdk/aws-ec2';

export class NetworkStack extends cdk.Stack {
  public readonly vpc: ec2.Vpc;

  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    this.vpc = new ec2.Vpc(this, 'VPC', {
      cidr: '10.0.0.0/16',
      natGatewaySubnets: {
        subnetName: 'Public'
      },
      subnetConfiguration: [
        {
          cidrMask: 26,
          name: 'Public',
          subnetType: ec2.SubnetType.PUBLIC
        },
        {
          name: 'Application',
          subnetType: ec2.SubnetType.PRIVATE
        },
        {
          cidrMask: 27,
          name: 'Database',
          subnetType: ec2.SubnetType.ISOLATED
        }
      ]
    });

    const vpcSecurityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', {
      vpc: this.vpc,
      description: 'Allow ssh access to ec2 instances',
      allowAllOutbound: true
    });

    vpcSecurityGroup.addIngressRule(
      ec2.Peer.anyIpv4(),
      ec2.Port.tcp(22),
      'allow ssh access from the world'
    );
  }
}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>Easy to share and reuse your infrastructure as a library</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Declarative infrastructure as code</strong> tends to be in languages that we don’t use every day like HCL, YAML, or JSON. AWS CDK uses<strong> object-oriented,</strong> provides <strong>abstraction</strong> techniques to create a model of our system, we can define <strong>modules</strong> to <strong>share across projects</strong>. We can use built-in construct libraries or from the community that can increase our productivity.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>CDK is a developer-friendly version of Cloud Formation</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>AWS CDK is an <strong>imperative</strong> programming language, supporting Java, JavaScript, Python, TypeScript and .NET. We can utilize our developer programming skills to reduce the time for learning a new syntax like <strong>Terraform</strong>. Think about a project that we use TypeScript as the primary programming language for Front-end, Back-end, CI/CD and IoC.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Easy to use logic (if statements, for-loops, etc) when defining your infrastructure</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We know how to write an if or for-loops, right? How hard is it to write a <strong>condition</strong> when using CloudFormation? If you know the answer, then you will know why love AWS CDK.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":423} -->
<figure class="wp-block-image"><img src="https://i2.wp.com/blog.innomizetech.com/wp-content/uploads/2019/11/image-9.png?fit=640%2C241&amp;ssl=1" alt="CDK conditional logic" class="wp-image-423"/><figcaption>Condition logic with AWS CDK</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Easy to get and integrate with our coding review workflow</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>AWS CDK provides commands to generate the <strong>CloudFormation</strong> template, so we can still review the generated CloudFormation template before applying, it also can generate dif that makes it easy to review and make the decision.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Code completion within your IDE</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Does anyone remember how to define a full detail of an AWS resource using CloudFormation? Is there any <strong>schema</strong> or <strong>intelligent</strong> for the CloudFormation template that you can use for <strong>code completion</strong> to speed up your coding time? </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":421} -->
<figure class="wp-block-image"><img src="https://i0.wp.com/blog.innomizetech.com/wp-content/uploads/2019/11/Screen-Shot-2019-11-11-at-9.46.41-PM.png?fit=640%2C346&amp;ssl=1" alt="CDK code completion" class="wp-image-421"/><figcaption>Code completion with AWS CDK</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Allow us to test our code</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>AWS CDK allowing us to perform snapshot tests, fine-grained assertions, help us build reliable and testable code, <a href="https://aws.amazon.com/blogs/developer/testing-infrastructure-with-the-aws-cloud-development-kit-cdk/">here is the post</a> that provides step by step guide to write and run unit tests using <a href="https://jestjs.io/">Jest</a>. With CloudFormation, you also can write some tests, but it requires another tool, check out <a href="https://aws.amazon.com/blogs/infrastructure-and-automation/up-your-aws-cloudformation-testing-game-using-taskcat/">this post</a> more details.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Easy to integrate with our CI/CD process</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Deployment can be done with one single command requiring installing any additional tool.  We also use AWS CDK to define our CI/CD pipeline using AWS DevOps tools such as CodeBuild, CodeDeploy, which is much better than writing a lot of CloudFormation code.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The A<strong>WS CDK</strong> just a high level of <strong>CloudFormation</strong>, so if you want to how exactly it works, my suggestion is to have a try with <strong>CloudFormation</strong>, do some works around it, then try to convert it into AWS CDK, you will find out whether you will continue with CloudFormation or change to use AWS, or maybe you can change to other tools such as Terraform. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Nothings is the best, it depends on your requirement, team, project and your company</strong>.  <strong>Have a nice coding!</strong></p>
<!-- /wp:paragraph -->


This post is originally published on [our blog](https://innomizetech.com/blog/top-reasons-why-we-use-aws-cdk-over-cloudformation).

<!-- wp:paragraph -->
<p><strong>Hoang Le</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Learn to Share and Share to Learn.</strong></p>
<!-- /wp:paragraph -->