Search This Blog

Monday 16 November 2020

CDK - Making CloudFormation easier

 In the previous post I created my first AWS Resource using AWS CloudFormation. This was done using a JSON file called the CloudFormation template.

While CloudFormation makes it possible to codify our AWS resources, the templates used for CloudFormation have their limitations:

  • The Templates can be represented in only JSON or YAML.
  • This means the files tend to be very verbose. 
  • We do not have the logic abilities that a programming language provides.
Enter CDK (or Cloud Development Kit)
  • Most programmers are comfortable working with programming languages like Java, and .NET. CDK allows us to model our resources in these languages (also Python or TypeScript)
  • Programming languages allow for better organization of infrastructure into modules, reusing these modules as well as IDE benefits.
  • At its backend CDK uses CloudFormation. So all benefits of CloudFormation are available here.
The first step is to ensure you have AWS CLI installed (since at the end of the day, CDK will use CloudFormation). Also ensure that the AWS user credentials being used to run cli has admin access.
The next step is to install AWS CDK. CDK requires Node.js to be installed. With that CDK can be installed through the npm package manager.

Now that installation is done we can setup and run CDK. To create a CDK project
1
cdk init app --language typescript
This will create a basic project with required default configurations.
1
2
3
ls
README.md         cdk.json          jest.config.js    node_modules      package.json
tsconfig.json     bin               cdk.out           lib               
package-lock.json test
The application starter file is based in the bin folder:
1
2
3
4
5
6
7
8
9
import 'source-map-support/register';
import * as cdk from '@aws-cdk/core';
import {
    CdkAppStack
} from '../lib/cdk-app-stack';

//This is the starting point for the app
const app = new cdk.App();
new CdkAppStack(app, 'CdkAppStack');

The actual code is in CdkAppStack.ts :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import * as cdk from '@aws-cdk/core';
import * as dd from '@aws-cdk/aws-dynamodb';

export class CdkAppStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // The code that defines your stack goes here
    new dd.Table(this, 'cdkSampleTable', {
      tableName: 'cdkSampleTable',
      billingMode: dd.BillingMode.PROVISIONED,
      partitionKey: { name: 'hashKey', type: dd.AttributeType.STRING },
      readCapacity: 5,
      writeCapacity:5
    });
  }
}
This is all the code needed to create a Dynamo db table. Compare this to the CloudFormation Template I had to write in the previous post. I can even write tests to ensure the code runs correctly.
To deploy this code:
cdk deploy
CdkAppStack: deploying...
CdkAppStack: creating CloudFormation changeset...
[██████████████████████████████████████████████████████████] (3/3)

   CdkAppStack

Stack ARN:
arn:aws:cloudformation:us-east-1:060315252813:stack/CdkAppStack/083f6000-26d6-11eb-8629-0a818cac4dad
Based on the aws configure settings, the Dynamo Table will be setup in the set region and set account. To see the CloudFormation template generated by the code, cdk provides a command - cdk synth
cdk synth
Resources:
  cdkSampleTable964E91EB:
    Type: AWS::DynamoDB::Table
    Properties:
      KeySchema:
        - AttributeName: hashKey
          KeyType: HASH
      AttributeDefinitions:
        - AttributeName: hashKey
          AttributeType: S
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 5
      TableName: cdkSampleTable
    UpdateReplacePolicy: Retain
    DeletionPolicy: Retain
    Metadata:
      aws:cdk:path: CdkAppStack/cdkSampleTable/Resource
  CDKMetadata:
    Type: AWS::CDK::Metadata
There was one another setup step that was needed - I needed to install the aws-dynamodb package
npm install @aws-cdk/aws-dynamodb
npm WARN cdk-app@0.1.0 No repository field.
npm WARN cdk-app@0.1.0 No license field.

+ @aws-cdk/aws-dynamodb@1.73.0
added 20 packages from 1 contributor and audited 762 packages in 7.773s

27 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

7 comments:

  1. It is a brilliant blog. I like the way you express information to us.  Redspider web design company  is about to tell you that I am very happy and I appreciate your effort. Thanks for sharing your blog.

    ReplyDelete
  2. I really accept every one of your opinions as you have actually been publishing useful facts and techniques with perfect details as well as figures.
    PlayTube
    Online FilmeK
    Web Url Opener

    ReplyDelete
  3. I have read a lot of posts on this particular topic. But your post guided me in the right way. Thanks for sharing this wonderful piece of work. What is Zakat
    What is Umrah
    What is Ramadan
    What is Hajj
    Five Pillars of Islam
    Url Opener
    islamic info center

    ReplyDelete
  4. I cherished examining this functional information talked about with useful details.

    ReplyDelete

  5. This is the post particularly what I was trying to find.
    Muhammad Ali
    Akhtar Iqbal
    Muhammad Usman

    ReplyDelete
  6. Bundle of appreciations for a desirable piece of relevant information from a
    qualified blogger with pleasurable article.
    Discover Everything
    The Knowledge Hub

    ReplyDelete
  7. pg slot thailand official เว็บแท้ นับเป็นแหล่งรวมเกมสล็อตที่ให้ประสบการณ์การเล่นที่น่าตื่นเต้นและสนุกสนานมากที่สุดในออนไลน์ด้วยการนำเสนอหลากหลายเกม PG ที่มีกราฟิกที่สวยงาม

    ReplyDelete