AWS CDK Using Java - Part 2 - Deploy Your First Java CDK App
Sanjoy Kumar Malik .
Senior Software Architect - Java Architect, Cloud Architect, AWS Architect?? All views are my own
In Part 1, we have set up the environment in local machine to work with AWS CDK using Java programming language.
In this article (Part 2), we will develop and deploy our first CDK application.
We will start with three important concepts in AWS CDK: constructs, stacks, and apps.
Construct: A construct is a component within your application that represents one or more AWS CloudFormation resources and their configuration.
Stack: A stack is a collection of AWS resources that you define using CDK constructs.
App: An application or app is a collection of one or more CDK stacks.
AWS CDK integrates with AWS CloudFormation to deploy and provision your infrastructure on AWS. Infrastructure created with the AWS CDK is eventually translated, or synthesized into AWS CloudFormation templates and deployed using the AWS CloudFormation service.
AWS CloudFormation templates are declarative, meaning they declare the desired state or outcome of your infrastructure. Using JSON or YAML, you declare your AWS infrastructure by defining AWS resources and properties.
With the AWS CDK, you can manage your infrastructure imperatively, using general-purpose programming languages. Instead of just defining a desired state declaratively, you can define the logic or sequence necessary to reach the desired state. Our focus is to use Java to define the logic or sequence necessary to reach the desired state.
With the above fundamental understanding, we will turn our focus on infrastructure coding. When coding comes into picture, you naturally think about the organization of files and folders that contain the CDK code. Here comes CDK Projects. A CDK project represents all the relevant files and folders that contain the CDK code.
Creating a CDK Project
Our first CDK project is "Hello World Project." The name of the project is cdk-hello-world-java-project.
Now open one command prompt window and create a directory named cdk-hello-world-java-project.
mkdir cdk-hello-world-java-project
Go to the directory just created.
cd cdk-hello-world-java-project
At this point, the directory cdk-hello-world-java-project is completely empty. Now, we will create a new AWS CDK project by executing the command cdk init in this empty directory as follows:
cdk init app --language java
After successful creation, you will see the below output.
The newly created project folder structure will be like below.
The source folder has two subfolders as follows:
Next, we will import the project into Eclipse (as existing maven project). Then rename the packages to com.example.helloworld.
Writing CdkHelloWorldJavaProjectStack.java
Here CdkHelloWorldJavaProjectStack represents the default stack of the cdk-hello-world-java-project project. We define a stack by extending or inheriting from the Stack construct.
Our stack will create a SQS queue with visibility timeout of 300 seconds. We will express this in code as follows:
Writing CdkHelloWorldJavaProjectApp.java
The CdkHelloWorldJavaProjectApp.java is the application file of our cdk-hello-world-java-project project.
Inside our code, we need to import the App construct from the AWS Construct library. It is the only construct that can be used as the root.
We will create an app by defining an app instance in the application file.
import software.amazon.awscdk.App;
. . .
. . .
App app = new App();
App construct provides the context for your Stack construct. Within the scope of an App construct, we will define our Stack constructs.
Remember, AWS CDK stack needs an environment. An environment consists of the AWS account and AWS Region that you deploy a stack to. The AWS CDK can determine environments from your credentials and configuration files. These files can be created and managed with the AWS Command Line Interface (AWS CLI). You can pass environment information from these files in your CDK code through environment variables that are provided by the CDK.
For the current project, we will define the Stack construct as follows:
Then, we need to synthesized the app to create AWS CloudFormation templates for the stack defined above.
app.synth();
The complete code for the CdkHelloWorldJavaProjectApp.java will be like this:
AWS CDK Bootstrapping
Each environment that you want to use with the AWS CDK must first be bootstrapped. Bootstrapping prepares your AWS environment by provisioning specific AWS resources (e.g., Amazon S3 bucket to store CDK project files, IAM roles configured to grant permissions needed by the AWS CDK to perform deployments) in your environment that are used by the AWS CDK.
领英推荐
We can use cdk bootstrap command to bootstrap an environment. Open one windows command prompt and execute the cdk bootstrap command from the project directory.
The CDK CLI retrieves the template and deploys it to AWS CloudFormation as a stack, known as the bootstrap stack. By default, the stack name is CDKToolkit.
Visit your AWS CloudFormation console. You will see CDKToolkit stack has been created.
Now our environment is bootstrapped.
Build Your CDK App
You can use the below command.
At this point, you should have a CDK app containing a single CDK stack.
Run the cdk list command to verify that.
Synthesize A CloudFormation Template
We need to synthesize a CloudFormation template from our CDK stack (CdkHelloWorldJavaProjectStack). To synthesize a CloudFormation template, run the cdk synth command from the root of your project. Since our app contains a single stack, the CDK CLI automatically detects the stack to synthesize.
Deploy Your CDK Stack
We use the CDK CLI cdk deploy command to deploy our CDK stack. Similar to cdk synth, you don't have to specify the AWS CDK stack since the app contains a single stack.
You will see CloudFormation stack CdkHelloWorldJavaProjectStack has been created and AWS CloudFormation provisions a SQS queue as part of this stack.
If you go to the details of the SQS queue, you will see default visibility timeout is set to 5 minutes (300 seconds) as we configured in our code.
Modify Your Application
We will modify the default visibility timeout to 180 seconds (3 minutes) as follows:
After the above changes in code, again deploy your CDK app.
Now again check your SQS queue details. You will see that default visibility timeout is set to 3 minutes (180 seconds).
That's cool! Now, we will delete our CDK app.
Deleting CDK App
We will use CDK CLI cdk destroy command to delete our application.
The above destroy command deletes the CloudFormation stack associated with our CDK stack, which includes the resources we created.
Note: Please delete all the resources including CDKToolkit CloudFormation stack, CDK S3 bucket created by visiting the AWS Management console.
(Optional) Pushing Code to GitHub
Visit your GitHub account and create one repository named cdk-hello-world-java-project.
Then execute the below git commands from the root directory of your project.
git add .
git commit -m "initial commit"
git remote add origin https://github.com/<<GITHUB-USERNAME>>/cdk-hello-world-java-project.git
git push -u origin master
That's all for this article!
Author of Flight of a Lifetime
2 个月Thanks for the invitation, Sanjoy. Do you like reading? I was hit by a car and killed! So I wrote an inspiring book :) Flight of a Lifetime: an astonishing true story of #life #death #resurrection #horses - written to bring hope where there is only despair… Read it, be inspired by it - then tell your friends :)?
Aspiring Corporate Director / Management Consultant / Corporate Leader
3 个月Thanks for inviting, sharing an informative-insightful article, & Best wishes, Sanjoy Kumar Malik . Syed Awees, ACCA. Syed Suheb.