One of many first choices you’ll must make when working with the AWS Cloud Growth Package (CDK) is selecting the language for writing your Infrastructure as Code (IaC). The CDK at the moment helps TypeScript, JavaScript, Python, Java, C#, and Go. Over the previous few years, I’ve labored with the CDK in TypeScript, Python, and Java. Whereas there’s ample info accessible on-line for TypeScript and Python, this publish goals to share my expertise utilizing Java because the language of selection for the AWS CDK.
Wait…What? Use Java With the AWS CDK?
Some could say that TypeScript is the obvious language to make use of whereas working with the AWS CDK. The CDK itself is written in TypeScript and it’s additionally essentially the most used language based on the 2023 CDK Group Survey. Java is coming in third place with a small share of use.
I do surprise if this nonetheless holds true given the variety of responses to the survey. I’ve labored with small companies and huge enterprise organizations over the past years and I see increasingly more Java-oriented groups transfer their workloads to AWS whereas adopting AWS CDK as their Infrastructure as Code software. Relying on the kind of service(s) being constructed by these groups they might or could not have any expertise with both Python or TypeScript and the Node.js ecosystem, which makes sticking to Java a simple selection.
Normal Observations
From what I’ve seen, adopting the CDK in Java is comparatively straightforward for many of those groups as they already perceive the language and the ecosystem. Integrating the CDK with their current construct instruments like Maven and Gradle is nicely documented, which leaves them with the educational curve of understanding the right way to work with infrastructure as code, the right way to construction a CDK challenge, and when to make use of L1, L2, and L3 constructs.
In comparison with TypeScript the CDK stacks and constructs written in Java include a bit extra boilerplate code and subsequently may really feel a bit extra bloated for those who come from a special language. I personally don’t really feel this makes the code much less readable and with trendy IDEs and coding assistants, I don’t really feel I’m much less productive.
The CDK additionally appears to develop into extra extensively adopted within the Java group with more moderen Java frameworks like Micronaut even having built-in assist for AWS CDK within the framework.
See as an example the next Micronaut launch configurations:
One of many benefits of Java is that it’s a statically typed language, which implies it is going to catch most CDK coding errors throughout compile time. There are nonetheless some errors which you’ll solely see throughout an precise cdk synth
or cdk deploy
. As an illustration, some constructs have required properties that may solely develop into seen for those who attempt to synthesize the stack, however in my expertise, you’ll have that in different languages as nicely.
Efficiency-wise, it feels just like the CDK in Java is a bit slower in comparison with utilizing TypeScript or some other interpreted language. I’ve not measured this, but it surely’s extra of a intestine feeling. This might need to do with the static nature of Java and its corresponding construct instruments and compile part. However, it is likely to be that the JSII runtime structure additionally has an impact and the way Java interacts with a JavaScript surroundings.
Java Builders
One of many greatest variations when utilizing the AWS CDK with Java is using Builders. When creating constructs with TypeScript, you’re primarily utilizing the props
argument (map of configuration properties) whereas making a assemble. Let’s check out an instance:
const bucket = new s3.Bucket(this,"MyBucket", {
versioned: true,
encryption: BucketEncryption.KMS_MANAGED
})
The Java model of the above snippet makes use of a Builder class that follows the builder sample for developing the properties. When you’re unfamiliar with the Builder sample in Java, I like to recommend trying out this weblog publish about utilizing the Builder sample. Relying on the CDK assemble, you may be capable of outline a CDK useful resource in two alternative ways.
Within the first instance, you utilize the Builder
for the Bucket
properties.
Bucket bucket = new Bucket(this, "MyBucket", new BucketProps.Builder()
.versioned(true)
.encryption(BucketEncryption.KMS_MANAGED)
.construct());
The choice is that constructs can have their very own builder class, which makes it rather less verbose and simpler to learn.
Bucket bucket = Bucket.Builder
.create(this, "MyBucket")
.versioned(true)
.encryption(BucketEncryption.KMS_MANAGED)
.construct();
IDE Assist
General IDE assist is de facto nice when working with CDK in Java. I exploit IntelliJ IDEA each day and auto-completion actually helps when utilizing the Builder objects.
Because the CDK documentation can be contained in the CDK Java supply code, wanting up documentation is very easy. It’s much like how you’ll do it with any form of different object or library.
Third-Social gathering Assemble Assist
The CDK itself is written in TypeScript, and for every supported programming language, a particular binding is generated. Which means that when a brand new useful resource or function for an AWS service is added within the TypeScript variant of the CDK, it’s additionally accessible to builders utilizing a Java-based CDK.
Apart from the default CDK constructs, there are additionally loads of community-generated constructs. Assemble Hub is a superb place to search out them.
From what I’ve seen, most constructs popping out of AWS will assist Java as one of many default languages. Group-supported constructs nonetheless may not. There are a number of standard constructs that solely assist TypeScript and Python. Filtering on Assemble Hub for AWS CDK v2-based constructs, sorted by programming languages ends in the next knowledge.
Language | Variety of constructs libraries |
Typescript | 1164 |
Python | 781 |
.Internet | 511 |
Java | 455 |
Go | 132 |
Relying on the kind of infrastructure or third-party providers you’re planning to make use of, you may not be capable of use all accessible constructs. As an illustration, the constructs maintained by DataDog are solely accessible in Typescript, Python, and Go. In my private expertise, although, most assemble builders are open to supporting Java. Third-party constructs are primarily based on projen and jsii, which implies that including a Java-based model is more often than not a matter of configuration within the package deal.json file of the challenge.
"jsii": {
"outdir": "dist",
"targets": {
"java": {
"package": "io.github.cdklabs.cdknag",
"maven": {
"groupId": "io.github.cdklabs",
"artifactId": "cdknag"
}
},
"python": {
"distName": "cdk-nag",
"module": "cdk_nag"
},
"dotnet": {
"namespace": "Cdklabs.CdkNag",
"packageId": "Cdklabs.CdkNag"
},
"go": {
"moduleName": "github.com/cdklabs/cdk-nag-go"
}
},
"tsc": {
"outDir": "lib",
"rootDir": "src"
}
},
(An instance of how JSII is configured for the CDK NAG challenge)
As soon as the configuration is in place and the artifacts have been pushed to, as an example, Maven Central, you’re good to go.
When excited about it, I as soon as had a third celebration assemble I needed to make use of that didn’t assist Java (but). It obtained added fairly shortly and there was additionally an alternate answer for it, so I can not keep in mind having points with the decrease variety of accessible constructs.
Examples, Tutorials, and Documentation
I believe it’s good to mirror on the truth that there are extra CDK examples and tutorials accessible in TypeScript and Python in comparison with Java. This displays the findings within the utilization chart from the CDK Group Survey. Nonetheless, studying TypeScript as a Java programmer is comparatively straightforward (in my private opinion). When you’re new to the AWS CDK, there’s a ton of instance code accessible on GitHub, YouTube, and quite a few weblog posts and tutorials. When you’re already utilizing the CDK together with Java remember to write some weblog posts or tutorials, so others can see that and profit out of your information!
Abstract
Java is a really viable possibility when working with the AWS CDK, particularly for workload groups already acquainted with the language and its ecosystem. IDE assist for the CDK is great with options like auto-completion and easy accessibility to supply code documentation.
All in all, the expertise is de facto good. Take into account that selecting Java to your infrastructure as code all is dependent upon the context and the surroundings you’re in. I’d counsel selecting the language that’s most relevant to your particular scenario. When you nonetheless must make the selection and are already working with Java, I’d undoubtedly advocate making an attempt it out!