DEV Community

Cover image for A Beginner's Guide to Configure AWS CDK and deploy  Hello World App.
Abdul Waqar
Abdul Waqar

Posted on

A Beginner's Guide to Configure AWS CDK and deploy Hello World App.

Prerequisite:

To work with the AWS CDK, you must have an AWS account and credentials and have installed Node.js and the AWS CDK Toolkit. See AWS CDK Prerequisites.
If you do not have AWS Account Reister now for New Account. You can use AWS Free Tier for 12 months for free. There are some services which are not available in AWS Free Tier. For pricing details See here

AWS Educate

AWS Free Tier

In this Post we have used TypeScript. Because TypeScript consider the best language for CDK
If you don't already have it, you can install it using npm.

npm install -g typescript
Enter fullscreen mode Exit fullscreen mode

Note :
If you get a permission error, and have administrator access on your system, try

sudo npm install -g typescript 
Enter fullscreen mode Exit fullscreen mode

This post covers how to set up AWS CDK for development and create Hello World project using CDK .

What is AWS CDK?

AWS CDK is an open source software development framework to model and provision your cloud application resources using familiar programming languages. With AWS CDK, you can define your infrastructure as code and provision it through AWS CloudFormation. AWS CDK is available to use in all AWS regions. In this learning repo we will use CDK with TypeScript.

We expect that Open Source AWS CDK will become the defacto standard for Infrastructure as Code (IAC) not just for AWS but other clouds as well. Thus becoming the standard IAC multicloud tool. It is already available for Terraform and Kubernetes. CDK has already become the de facto software development framework internally at AWS. AWS CDK is DevOps for Developers and is the Ultimate DevOps tool.

STEP: 1 Install the AWS CDK

Install the AWS CDK Toolkit globally using the following NPM command.

npm install -g aws-cdk
Enter fullscreen mode Exit fullscreen mode

After running the above command aws-cdk will be installed. Run the following command to verify correct installation and print the version number of the AWS CDK.

cdk --version
Enter fullscreen mode Exit fullscreen mode

Before createing an application we need to configure our CDK CLI
Run the following command to configure AWS in console.

aws configure
Enter fullscreen mode Exit fullscreen mode

If you don't have AWS access key ID, secret access key follow the below given instruction :

To get your access key ID and secret access key

  1. Open the IAM console at IAM Console.

  2. On the navigation menu, choose Users.

  3. Choose your IAM user name (not the check box).

  4. Open the Security credentials tab, and then choose Create access key.

  5. To see the new access key, choose Show. Your credentials resemble the following:

  • Access key ID: AKIAIOSFODNN7EXAMPLE

  • Secret access key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

  1. To download the key pair, choose Download .csv file. Store the .csv file with keys in a secure location.

Create first AWS CDK app

Now You have set up your development environment for writing AWS CDK apps. Great! Now let's see how it feels to work with the AWS CDK by building the simplest possible AWS CDK app.

Each AWS CDK app should be in its own directory, with its own local module dependencies. Create a new directory for your app. Starting in your home directory, or another directory if you prefer, issue the following command

mkdir hello-cdk
cd hello-cdk
Enter fullscreen mode Exit fullscreen mode

Now initialize the app using the cdk init command, specifying the desired template ("app") and programming language, We are using TypeScript.Run the following command:

cdk init app --language typescript
Enter fullscreen mode Exit fullscreen mode

The cdk init command creates a number of files and folders inside the hello-cdk directory to help you organize the source code for your AWS CDK app. Take a moment to explore. The structure of a basic app is all there; you'll fill in the details in this tutorial.

If you have Git installed, each project you create using cdk init is also initialized as a Git repository. We'll ignore that for now, but it's there when you need it.

Build the app

In most programming environments, after making changes to your code, you'd build (compile) it. This isn't strictly necessary with the AWS CDK—the Toolkit does it for you so you can't forget. But you can still build manually whenever you want to catch syntax and type errors. For reference, here's how.

npm run build
Enter fullscreen mode Exit fullscreen mode

List the stacks in the app

Just to verify everything is working correctly, list the stacks in your app.

cdk ls
Enter fullscreen mode Exit fullscreen mode

Add an Amazon S3 bucket

At this point, your app doesn't do anything because the stack it contains doesn't define any resources. Let's add an Amazon S3 bucket.

Install the Amazon S3 package from the AWS Construct Library.

npm install @aws-cdk/aws-s3
Enter fullscreen mode Exit fullscreen mode

Next, define an Amazon S3 bucket in the stack using the Bucket construct.

In lib/hello-cdk-stack.ts:

import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';

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

    new s3.Bucket(this, 'MyFirstBucket', {
      versioned: true
    });
  }
}
Enter fullscreen mode Exit fullscreen mode

Bucket is the first construct we've seen, so let's take a closer look. Like all constructs, the Bucket class takes three parameters.

  • scope: Tells the bucket that the stack is its parent: it is defined within the scope of the stack. You can define constructs inside of constructs, creating a hierarchy (tree).

  • Id: The logical ID of the Bucket within your AWS CDK app. This (plus a hash based on the bucket's location within the stack) uniquely identifies the bucket across deployments so the AWS CDK can update it if you change how it's defined in your app. Buckets can also have a name, which is separate from this ID (it's the bucketName property).

  • props: A bundle of values that define properties of the bucket. Here we've defined only one property: versioned, which enables versioning for the files in the bucket.

Synthesize an AWS CloudFormation template

Synthesize an AWS CloudFormation template for the app, as follows.

cdk synth
Enter fullscreen mode Exit fullscreen mode

Make Build of App

Now Make build of our App by runni command :

npm run build
Enter fullscreen mode Exit fullscreen mode

Deploying the stack

To deploy the stack using AWS CloudFormation, issue:

cdk deploy
Enter fullscreen mode Exit fullscreen mode

You can go to the AWS CloudFormation console and see that it now lists hello-cdk:

https://console.aws.amazon.com/cloudformation/home

You'll also find MyFirstBucket (hello-cdkstack-myfirstbucketb8884501-r3g3as4wff5f) in the Amazon S3 console.

https://s3.console.aws.amazon.com/s3/home?region=us-east-2#

Congratulations !

You app is successfuly deploy to AWS.

Latest comments (0)