AWS Cloud Development Kit using Java

Table of Contents

In this blog, you will learn about AWS Cloud Development Kit using Java. Without further ado let’s get started!

You may also be interested in: “React 18 Features – What’s new in the box?”

Introduction to AWS Cloud Development Kit

The AWS CDK lets you build applications in the cloud with the expressive power of a programming language. It is recommended for users who have moderate to high experience of using AWS services. Let’s have a look at some key concepts while using AWS CDK to create CDK Apps.

Image Source

  • An App uses AWS CDK to define AWS Infrastructure.  You can write CDK apps in languages supported by AWS such as TypeScript, JavaScript, Python, Java, or C#.
  • An App can have one or more stacks.
  • Stacks contain constructs. They are equivalent to AWS CloudFormation stacks.
  • Constructs define AWS resources, such as Amazon RDS, DynamoDB, S3 buckets, Lambda functions, and so on.
  • Stacks, Constructs, and apps are represented as classes. Constructs are instantiated within a stack to declare them to AWS and connect them to each other using well-defined interfaces.

You will get a hold of these concepts after implementing the sample-cdk-project app.

Prerequisites

First, you need to install AWS CLI on your computer. Moreover, you also need an access key and a secret key provided by the administrator of the AWS Account.

First, confirm that you have successfully installed it on your computer by entering the following command.

aws –version

Then enter the following command to set up your AWS CLI installation on your computer.

aws configure

Next, you will provide AWS Acces Key ID, AWS Secret Access Key, Default Region name, and Default output format.

Getting started with AWS CDK using Java

Install AWS CDK Toolkit

CDK Toolkit is a tool for working with  AWS CDK apps and stacks. The Toolkit provides the ability to convert one or more AWS CDK stacks to CloudFormation templates and related assets and to deploy stacks to an AWS account along with other functions. Now it’s time to install AWS CDK Toolkit using the Node Package Manager command. For that, you will enter the following command 

npm install -g aws-cdk

Initialize Sample CDK App

First, create a new directory for the sample CDK app with the following command.

mkdir sample-cdk-project

Then make it the working directory.

cdk sample-cdk-project

Now enter the following command that will create a sample CDK app using java.

cdk init --language java

Let’s have a look at what’s going on inside the project.

Understand Sample CDK App

The good part is that you can open your project in any of your favorite IDE. The project directory looks like the following. Let’s have a look at SampleCdkProjectStack.java and SampleCdkProjectApp.java one by one.

SampleCdkProjectStack.java

This is the default stack of the sample-cdk-project. To get started, you will uncomment the example resource. SampleCdkProjectStack extends the Stack base class and includes a single Amazon SQS Queue with a timeout of processing a message set to 300 seconds. Constructs represent a cloud component and are the basic building blocks of AWS CDK apps. Amazon SQS Queue is a construct in the following example. Note that all constructs take three parameters on initialization: Scope, id, and Props.

  • Scope is the construct within which the construct is defined.
  • Id is an identifier that must be unique within the scope.
  • Props are a set of properties or keyword arguments, depending upon the language, that defines the construct’s initial configuration.
public class SampleCdkProjectStack extends Stack {
     public SampleCdkProjectStack(final Construct scope, final String id) {
        this(scope, id, null);
    }
    public SampleCdkProjectStack(final Construct scope, final String id, final StackProps props) {
        super(scope, id, props);
        // The code that defines your stack goes here
    /* final Queue queue = Queue.Builder.create(this, "SampleCdkProjectQueue")
        .visibilityTimeout(Duration.seconds(300))
        .build(); */
    }
}

SampleCdkProjectApp.java

Now let’s have a look at SampleCdkProjectApp.java. Here you will instantiate the stack (previously defined) in some scope to deploy it. This class instantiates SampleCdkProjectStack and produces the AWS CloudFormation template defined by the stack. Here you will also initialize App construct that you will use as a scope for defining an instance of SampleCdkProjectStack.

public class SampleCdkProjectApp {
   public static void main(final String[] args) {
      App app = new App();
      new SampleCdkProjectStack(app, "SampleCdkProjectStack", StackProps.builder()
   /*If you don't specify 'env', this stack will be environment-agnostic.
     Account/Region-dependent features and context lookups will not work,
     but you can deploy a single synthesized template anywhere.
     Uncomment the next block to specialize this stack for the AWS Account
     and Region that is implied by the current CLI configuration. */
     /* .env(Environment.builder()
        .account(System.getenv("CDK_DEFAULT_ACCOUNT"))
        .region(System.getenv("CDK_DEFAULT_REGION"))
        .build()) */
      /*Uncomment the next block if you know exactly what Account and Region you
       want to deploy the stack to */
        .env(Environment.builder()
        .account("123456789012")
        .region("us-east-1")
        .build()) */
      //For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
        .build());
        app.synth();
     }
}

In the above class, uncomment the code to specialize this stack for the AWS Account and Region that are implied by the current CLI configuration.

Bootstrap CDK App

When we deploy AWS CDK apps into an AWS environment, there are certain resources that may be needed for deployment such as the Amazon S3 bucket for storing files and IAM roles with certain permissions to perform the deployment. The process of providing these resources is called Bootstrapping. 

The above resources are defined in an AWS CloudFormation stack which is called CDKToolkit. The following command deploys the CDK Toolkit staging stack.

cdk bootstrap

You can have a look at the Resources in CDKToolkit CloudFormation template.

Synthesize Cloud Formation Template

The following command synthesizes and prints the CloudFormation template. You would need to specify the stack if your app contains more than one of them. Currently, there is no need to specify since your app contains only one stack.

cdk synth

Deploy CDK App

Now enter the following command to deploy the resources defined in the stack of your CDK app.

cdk deploy

You can have a look at the Resources in SampleCdkProjectStack CloudFormation template. 

This is the Amazon SQS Queue that you defined in the stack.

In the end, destroy cdk-sample-project resources using the following command

cdk destroy

Conclusion

With this, we have come to the end of our blog. In this blog, you learned the basics of AWS Cloud Development Kit and created a CDK app using java. If you want to practice more, do check out the official github repository of AWS that has examples implemented in java.

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

Happy learning!

Reference

https://docs.aws.amazon.com/cdk/v2/guide/home.html

Unimedia Technology

Here at Unimedia Technology we have a team of Cloud Native Developers that can help you develop your most complex AWS and Azure Applications.

Remember that at Unimedia, we are experts in emerging technologies, so feel free to contact us if you need advice or services. We’ll be happy to assist you.

Unimedia Technology

Your software development partner

We are a cutting-edge technology consultancy specialising in custom software architecture and development.

Our Services

Sign up for our updates

Stay updated, stay informed, and let’s shape the future of tech together!

Related Reads

Dive Deeper with These Articles

Explore more of Unimedia’s expert insights and in-depth analyses in the realm of software development and technology.

Let’s make your vision a reality!

Simply fill out this form to begin your journey towards innovation and efficiency.