The toolkit from AWS CDK offers the CLI command cdk or command lines interfaces. It’s one of the main tools you need for synthesizing the AWS CDK applications so that you can generate stacks of AWS CloudFormation that will be deployed in the AWS Cloud. For installing AWS CDK, you will need to run the command that follows in the terminal, which is: npm install – g- aws – cdk.
The command will begin the installation of the package for AWS CDK onto your system globally and that will enable you to build and deploy code using AWS CDK in the AWS account and begin your first project. In this article, we will be teaching you the correct method for installing AWS CDK and setting up the configurations, so that you can build and deploy your first CDK application.
The Correct Way for Installing AWS CDK (Cloud Development Kit)
First, we will start by utilizing the package manager of NPM for installing the AWS CDK onto our machine. It’s an installation method used universally, which means that the steps that follow can be applicable for Windows, Linux (Ubuntu), and macOS.
To begin the installation of the toolkit for AWS CDK for the machine, we are going to be using the node package manager in the terminal for globally installing the package:
Checking the Version of AWS CDK
After installing the AWS CDK the validation process can begin to ensure you’re using the latest versions by entering into the terminal the next command:
You can clearly see that your AWS CDK runs on version 2 (v2), and that’s the latest version currently available on the market.
What Commands Are Available in AWS CDK?
After installing the AWS CDK onto your system, it’s time where you start thinking about running the different CDK commands. You can find easily the available arguments and commands in the toolkit for AWS CDK, when you run the following:
Creating A Project on AWS CDK
If you’re thinking about creating a project on AWS CDK, you can start initializing it by using the init command in CDK. Here you will be specifying the programming language and template you want. Some possible options for you are here in the example:
This may be your first time running AWS CDK, which is why we will be beginning with a sample application. The project will contain a few sample resources, which will demonstrate the creation of a stack. To start creating the project on CDK, you need to run the command “cdk init sample-app –language typescript in the folder of the project. It will appear like this:
The code and project structure required for running the example application in AWS CDK has automatically been created by the command. Your project structure will be looking like this now:
If you take a closer look, you can view the lib folder that is right at the bottom of the project folder. That folder contains the stack that comes with all the necessary code for your resources. As we have already initialized the AWS CDK using a sample application, it will create SQS queues along with SNS topics and a subscription as well.
Building Your Application in AWS CDK
As we have already shown you how to generate a project in AWS CDK with stacks that contains a sample queue of AWS SQS, we will now show you the best way to build your application in AWS CDK. To start building the application, we will run cdk synth command. The best part about this command is it will generate templates for CloudFormation that are code based, which has been shown in the application in AWS CDK. It will look like this:
You can clearly see above in the results that the cdk synth command has shown the template for CloudFormation in the format of YAML in the terminal. However, if you want to actually deploy the application, the template has been saved in the folder cdk.out in the format of JSON.
Deploying Your Application in AWS CDK
Now that we have come to the final part of the process, which is deploying your application in AWS CDK, it’s time that you install AWS CLI onto your system and also create a profile on AWS. It’s a simple process and one that will help reap the rewards for you in the end, as you will have a complete structure of the AWS CDK and that will enable you to safely deploy your application in AWS CDK. It’s the best course of action and one that will help launch your AWS CDK application.
Once you have accomplished that you can start by proceeding by deploying your application. The first thing you will have to do here is ensuring that you’re exporting your profile on AWS with the export command AWS_Profile= (your_aws_profile_name). After that, you can begin deploying the synthesized templates with the command cdk deploy:
The Bottom Line
In this article, we have simplified the process of setting up AWS CDK and initializing a project on AWS CDK. We have also helped you in synthesizing a template on CloudFormation, and then deploying it for your account on AWS. If you’re looking for the removal of stack from your account on AWS, then you will have to run the “cdk destroy” command.
At the end of the day, it’s all about taking the proper steps and following the guide to launching your AWS CDK. It’s not that difficult once you get the hang of things and can even benefit greatly from it by launching it onto your account. We hope that now you have a better idea of how to install AWS CDK and ensure that you can achieve maximum profit from it.
Further blogs within this A Step-by-Step Guide to Install AWS CDK category.