DEV Community

Cover image for AWS CDK Node.JS: Hello World
Josue Bustos
Josue Bustos

Posted on

AWS CDK Node.JS: Hello World

Introduction

This tutorial will show you how to install the AWS CDK on Ubuntu and deploy your first JavaScript Infrastructure as Code (IAS) to AWS.

Warning: I do not assume any responsibility if you incur any charges. You are responsible for managing your resources in your AWS account.

If you find yourself stuck, I recommend reviewing the AWS documentation to find detailed concepts, definitions, and critical terminology to help you along the way.

AWS CDK Intro

"AWS CDK is a software development framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation.

You use the CDK to define your cloud resources in a familiar programming language. The CDK supports TypeScript, JavaScript, Python, Java, C#/.Net, and (in developer preview) Go." - AWS Docs.

Today we're going to leverage JavaScript, which you can port to TypeScript at a later time.

Prerequisites

This tutorial assumes you're comfortable, familiar, and have the necessary software installed on your system to follow along.

  • AWS IAM Credentials
  • JavaScript
  • Homebrew
  • Node JS 10.13.0 or later
  • Ubuntu OS
  • macOS
  • Terminal
  • Command Line Interface

Note: I will be installing AWS CDK on Ubuntu. Some OS-specific troubleshooting may be required if you run into issues.

Ready!? Let's begin.

Install The AWS CDK

Go ahead and type the following command to begin installing the CDK:

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

Verify that you have installed the CDK:

$ cdk --version
Enter fullscreen mode Exit fullscreen mode

Create A CDK Project

Create a folder where your CDK project will live:

$ mkdir MyTestService
Enter fullscreen mode Exit fullscreen mode

Change directory

$ cd MyTestService
Enter fullscreen mode Exit fullscreen mode

In an NPM fashion, you can create a CDK project using the following command:

$ cdk init app --language javascript
Enter fullscreen mode Exit fullscreen mode

Depending on your host system, it will take a moment to create all the resources necessary to prepare your IaC project. So, sit tight and have some delicious warm coffee.

A successful install will result in the following example output:

✅ All done!
****************************************************
*** Newer version of CDK is available [1.106.1]  ***
*** Upgrade recommended (npm install -g aws-cdk) ***
****************************************************
Enter fullscreen mode Exit fullscreen mode

You can view a list of your stacks by using the following command:

$ cdk ls
Enter fullscreen mode Exit fullscreen mode

example output:

MyTestServiceStack
Enter fullscreen mode Exit fullscreen mode

But we're not done yet!

Add an Amazon S3 Resources

Make sure you're in the root directory of your project. If not, the CDK commands will not function properly.

For this following command, we will add an AWS resource, or within the context Node JS, a package, like so:

$ 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. To do this, locate line 13 or the commented out text and append the following code:

const cdk = require('@aws-cdk/core');
const s3 = require('@aws-cdk/aws-s3');

class HelloCdkStack extends cdk.Stack {
  constructor(scope, id, props) {
    super(scope, id, props);

    // The code that defines your stack goes here
    new s3.Bucket(this, 'MyFirstBucket', {
    versioned: true,
    removalPolicy: cdk.RemovalPolicy.DESTROY,
    autoDeleteObjects: true
});
  }
}

module.exports = { HelloCdkStack }
Enter fullscreen mode Exit fullscreen mode

Synthesize an AWS CloudFormation template

$ cdk synth
Enter fullscreen mode Exit fullscreen mode

The "synth" command generates a CloudFormation file. Also, the actual output will extend for several lines. Makes sure to scroll down and explore the generated CloudFormation file.

Resources:
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties: 
    ....
Enter fullscreen mode Exit fullscreen mode

If you received an error like --app is required..., it's probably because you are running the command from a subdirectory. Navigate to the main app directory and try again.

Deploy Your Stack

First, bootstrap your project before deployment, ensuring that AWS is aware of the permissions between resources.

$ cdk bootstrap
Enter fullscreen mode Exit fullscreen mode

AND then you should see a similar output:

 ⏳  Bootstrapping environment aws://965805005461/us-east-2...
CDKToolkit: creating CloudFormation changeset...
Enter fullscreen mode Exit fullscreen mode

Finally, deploy your stack to the AWS Cloud. To do that, type the following command:

$ cdk deploy
Enter fullscreen mode Exit fullscreen mode

As your code is preparing to deploy, it will prompt you to for a yes or no. Type Yes and then press Enter to continue with the deployment process.

While that's doing its thing, you can navigate to the AWS CloudFormation console and see the magic in real-time.

Delete Your Resources

To delete your resources, you just deployed, type the following command:

$ cdk destroy
Enter fullscreen mode Exit fullscreen mode

When prompted to delete all the resources you have created, Type "Y" for yes — otherwise, type "N" for no.

Example prompt:

Are you sure you want to delete: MyTestServiceStack (y/n)? y
Enter fullscreen mode Exit fullscreen mode

Great Job! You just created your first AWS CDK JS APP!

These are industry sought after skills, and I hope it helps you upskill and get you where you need to go in your daily hacking or career!

Until next time!

Top comments (0)