Unleashing the power of AWS CDK
Tags: iac, aws, cdk, devops
Published: 2023-06-27 06:55:41

Table of Content

* Introduction to IaC
    * Why is it important to know IaC?
    * Is AWS CDK an IaC?
* Introduction to AWS CDK
    * Programming languages supported by AWS CDK
* Is AWS CDK and AWS Cloudformation the same?
* Infrastructure Creation with AWS CDK
* Conclusion

Introduction to IaC

We are here for AWS CDK, why IaC. Let me make it easier for you. When we develop any service or any application we write code for it, right? We define each component of the application from frontend to backend, with various configurations. Similarly, we define and manage the infrastructure of that application through code, and by infrastructure I mean storage, environment, operating system and multiple other infrastructure components instead of doing it manually.


Why is it important to know IaC?

* I don’t know about you, but when I came to know that historically developers would physically go to data centers with their code stored in hardware to deploy their application, I was literally amazed and shocked at the same time! 

IaC has really changed our life for good, now you see how things are easier, convenient, time-saving and cost effective. Just some configuration files and few clicks and congrats, you have deployed your application successfully by sitting at your home/office!

* Let’s pick-up some data to validate this point. After moving to its own cloud, Amazon engineers deploy code every 11.7 seconds, on average—reducing both the number and duration of outages at the same time. Netflix engineers make around 20k deployments per day. Now we know how complicated it would be to manage this much of the code changes and deployment manually, that’s where IaC comes in play - from scaling to deployment, a few configuration changes and done!
* Additionally, it saves time by automating repetitive tasks, reduces human errors that often happen in manual processes, and improves productivity by allowing developers to focus more on their code and application logic.


Is AWS CDK an IaC?

Yep! During deployment it’s not only about the frontend-backend code we write for our application, to make it work in any condition, there are multiple other building blocks as well, for example data storage, configuration management, network setups, security configurations, and more .
From setting up infrastructure to handling deployment, all of this is managed by AWS CDK.
Please note that AWS CDK is not the only IaC option, there are many more like AWS Cloudformation, Terraform, Saltstack etc.

Introduction to AWS CDK

AWS Cloud Development Kit (CDK) is an open-source software development framework for defining IaC of any application in any language of our choice, helping to create the required services and resources of AWS for an application, and also deploying the application stack over AWS Cloudformation, which itself is an AWS service.
As I mentioned in the above section, every application has a certain type of building block which needs to be taken care of that’s where AWS CDK becomes your friendly option. AWS CDK allows you to define and provision these infrastructure resources in a programmatic and declarative manner.


Programming languages supported by AWS CDK

1. TypeScript
2. JavaScript
3. Java
4. Python
5. C#
6. .NET

Is AWS CDK and AWS Cloudformation the same?

I would say, yes. CDK is like a path to access and create all the required AWS services, the same goes for AWS Cloudformation. Since AWS CDK leverages the Cloudformation service, AWS CDK provides some of the same benefits of Cloudformation but with a few key differences, one of the major key difference would be the options we have to define our CDK package in any programming language of developer's choice, as we noticed in previous section, whereas in Cloudformation we use AWS console, to leverage AWS services we define it in JSON or YML format which according to me is pretty complicated. But at the end of the day both serve the same goal and of course you can always opt for Cloudformation, maybe to get a better taste of it.


Infrastructure Creation with AWS CDK

Many developers end up embracing AWS CDK as it allows them to use the same programming languages they have been using in their application development (maybe). It also has a more structured reuse format than Cloudformation, let's say we have to create new s3 bucket and IAM role for our application. For both the S3 bucket and IAM role, I understand that both have different parameters and requirements but to some extent we could use similar lines of code to create both. 
Check out the code snippet;

import * as cdk from 'aws-cdk-lib';
import { aws_s3 as s3 } from 'aws-cdk-lib';
import { aws_iam as iam } from 'aws-cdk-lib';

export class HelloCdkStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
   
   //creating new S3 bucket
    new s3.Bucket(this, 'MyFirstBucket', {
      versioned: true
    });
    
    //creating new IAM role
     new iam.Role(this, 'MyRole', {
      assumedBy: new iam.ServicePrincipal('sns.amazonaws.com'),
      description: 'An example of IAM role in AWS CDK',
      managedPolicies: [
        iam.ManagedPolicy.fromAwsManagedPolicyName(
          'AmazonAPIGatewayInvokeFullAccess',
        ),
      ],
    });
  }
}

We could also easily test our written code as CDK provides testing frameworks. These testing frameworks help ensure that the AWS services and resources defined in the CDK code are provisioned correctly with the expected values.

Conclusion

The benefits of AWS CDK are immense for us. We can develop and deploy our resources faster. 
AWS CDK is just a high level of CloudFormation, so if you want to know exactly how it works, my suggestion is to have a try with CloudFormation, do some work 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 CDK, or maybe you can change it to other IaC tools such as Terraform or Saltstack.