Learning AWS Cloud Development Kit with Hands On


AWS

In this tutorial, we are going to learn how to use AWS Cloud Development Kit. But before moving ahead let's have a quick overview of AWS Cloud Development Kit.

 

AWS Cloud Development Kit Overview

  • We can build reliable, scalable, cost-effective applications in the cloud with a programming language using AWS Cloud Development Kit.
  • With AWS CDK, we can define more infrastructure with less code by using high-level constructs that provide defaults for our AWS resources.
  • With AWS CDK, we can use programming idioms like parameters, conditionals, loops, composition, and inheritance to model our system design.
  • With AWS CDK, we can place our infrastructure, application code, and configuration all in one place.
  • AWS CDK lets us use software engineering practices such as code reviews, unit tests, and source control.
  • With AWS CDK, we can connect AWS resources together (even across stacks) and grant permissions using simple, intent-oriented APIs.
  • AWS CDK lets us import existing AWS CloudFormation templates to give our resources a CDK API.
  • The languages that AWS CDK supports include TypeScript, JavaScript, Python, Java, C#/.Net, and (in developer preview) Go.

In this tutorial, we will be working with AWS CDK in Java. Now let's head straight to the implementation part!

Working with AWS CDK in Java

First, we will log in to our AWS account and then type CloudShell under the Services tab. Note that AWS CloudShell is a browser-based shell that makes it easy to securely manage, explore, and interact with AWS resources and is pre-authenticated with our console credentials. In the terminal, we will enter the following command.

sudo npm install -g aws-cdk

The AWS CDK Toolkit is installed with the Node Package Manager (npm). It is the primary tool for interacting with our AWS CDK app. It executes our app, interrogates the application model we defined, and produces and deploys the AWS CloudFormation templates generated by the AWS CDK.

Learning AWS Cloud Development Kit with Hands On

Now we will enter the following commands to create a new directory for our CDK app and then get inside it.

mkdir demo-project
cd demo-project

Learning AWS Cloud Development Kit with Hands On

 

Create CDK App

Then we will enter the following command.

cdk init --language java

We will create a new AWS CDK app by invoking cdk init in an empty directory created above. cdk init uses the name of the project folder to name various elements of the project, including classes, subfolders, and files. The resulting project includes a reference to the software.amazon.awscdk Maven package which along with its dependencies are automatically installed by maven. With the above command, we have created a blank project for Java development with AWS Cloud Development Kit.

Working with AWS Cloud Development Kit in Java

Then we will enter the following command. It lists the stacks in the app. We have done this just for verification.

cdk ls

Learning AWS Cloud Development Kit with Hands On

Now we will have a look at the app's java files.  First, we will type the following command to get into the directory where the java files reside.

cd src/main/com/myorg

Let's have a look at the existing files in the folder with the following command.

ls

Here we can see two files DemoProjectApp.java and DemoProjectStack.java. We will update DemoProjectStack.java. For that, we will enter the following command.

sudo nano DemoProjectStack.java

If nano is not installed, we will enter the following command.

sudo yum install nano

Learning AWS Cloud Development Kit with Hands On

Now here we can see that an example resource Queue is created but the code is commented. We will uncomment it and the related import statements.

Learning AWS Cloud Development Kit with Hands On

 

Now let's note some important points related to the default project created.

  • We call our AWS Cloud Development Kit application an app, which is represented by the AWS CDK class App.
  • The example declares a stack class named DemoProjectStack that includes a single Amazon SQS Queue with a timeout of processing a single message set to 300 seconds. However, this only declares a stack. We still need to define (also known as to instantiate) it in some scope to deploy it and that is done in DemoProjectApp class.
  • Stacks in AWS CDK apps extend the Stack base class, as shown in the example below. In DemoProjectStack, a constructor is defined which accepts scope, id, and props. The base class constructor is invoked via super with the received scope, id, and props, as shown in the example below.
  • Constructs are the basic building blocks of AWS Cloud Development Kit apps. A construct represents a "cloud component" and encapsulates everything AWS CloudFormation needs to create the component. In our case, the construct is Amazon SQS Queue.
  • All constructs take three parameters when they are initialized: Scope, id, and Props. We should usually pass this for the scope because it represents the current scope in which we are defining the construct. An identifier must be unique within the scope. Props are a set of properties, depending upon the language, that defines the construct's initial configuration.


Learning AWS Cloud Development Kit with Hands On

 

The following DemoProjectApp class instantiates a DemoProjectStack and produces the AWS CloudFormation template that the stack defined. Note that the App construct doesn't require any initialization arguments because it's the only construct that can be used as a root for the construct tree. The App instance will be used as a scope for defining a single instance of our stack.

Learning AWS Cloud Development Kit with Hands On

 

Bootstrapping

Then we will enter the following command. Note that we will have to install maven if not installed before this command.

cdk bootstrap

cdk bootstrap deploys the CDK Toolkit staging stack. When we deploy AWS CDK apps into an AWS environment, it may require that we provide the resources the AWS CDK needs to perform the deployment. These resources include an Amazon S3 bucket for storing files and IAM roles that grant permissions needed to perform deployments. The process of provisioning these initial resources is called bootstrapping. The required resources are defined in an AWS CloudFormation stack, called the bootstrap stack, which is usually named CDKToolkit.

Learning AWS Cloud Development Kit with Hands On

 

CloudFormation template

Then we will enter the following command. It synthesizes and prints the CloudFormation template for the specified stack(s).

cdk synth

Learning AWS Cloud Development Kit with Hands On

 

Deploy CDK app

Then we will enter the following command. It deploys the resources defined by one or more of the stacks in our AWS CDK app to AWS.

cdk deploy

Learning AWS Cloud Development Kit with Hands On

 

The following diagram shows the phases that the AWS CDK goes through when we call the cdk deploy.

Learning AWS Cloud Development Kit with Hands On

 

Now let's have a look at the Stacks created as a result of the above commands. First, we will type CloudFormation under the Services tab then from the left pane we will click on Stacks. Here two Stacks are created. The first CloudFormation stack i.e. DemoProjectStack is created after cdk deploy command. The second stack i.e. CDKToolkit is created after cdk bootstrap command. We can have a look at the stack information below.

Learning AWS Cloud Development Kit with Hands On

 

Now let's switch to the Resources tab of DemoProjectStack. We can clearly see DemoProjectQueue0E1488D5 of type AWS::SQS::Queue. This resource was created in DemoAppStack.java.

Learning AWS Cloud Development Kit with Hands On

 

In the Events tab, we can see the events that occurred while creating DemoProjectStack.

Learning AWS Cloud Development Kit with Hands On

 

Now let's have a look at the Queue that was created. For that, we will type Simple Queue Service under the Services tab. Here we can see our newly created queue.

Learning AWS Cloud Development Kit with Hands On

 

Destroy resources

And in the end, we can destroy our CDK app resources by entering the following command.

cdk destroy

Learning AWS Cloud Development Kit with Hands On

 

Conclusion

With this, we have come to the end of our tutorial. In this tutorial, we learned to work with AWS Cloud Development Kit in Java. First, we had a quick overview of the AWS Cloud Development Kit. We learned to use the CDK toolkit for the process. Apart from that we also looked into java code and learned how different resources can be created using AWS Cloud Development Kit.

Stay tuned for some more informative tutorials coming ahead. Feel free to leave any feedback in the comments section.

 

References

AWS Cloud Development Kit (CDK) v2

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment