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
|
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
|
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 :
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.
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 }); } } |
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
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
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
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.
ReplyDeleteI really accept every one of your opinions as you have actually been publishing useful facts and techniques with perfect details as well as figures.
ReplyDeletePlayTube
Online FilmeK
Web Url Opener
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
ReplyDeleteWhat is Umrah
What is Ramadan
What is Hajj
Five Pillars of Islam
Url Opener
islamic info center
I cherished examining this functional information talked about with useful details.
ReplyDelete
ReplyDeleteThis is the post particularly what I was trying to find.
Muhammad Ali
Akhtar Iqbal
Muhammad Usman
Bundle of appreciations for a desirable piece of relevant information from a
ReplyDeletequalified blogger with pleasurable article.
Discover Everything
The Knowledge Hub
pg slot thailand official เว็บแท้ นับเป็นแหล่งรวมเกมสล็อตที่ให้ประสบการณ์การเล่นที่น่าตื่นเต้นและสนุกสนานมากที่สุดในออนไลน์ด้วยการนำเสนอหลากหลายเกม PG ที่มีกราฟิกที่สวยงาม
ReplyDelete