Infrastructure as code (IaC) for Java-based apps on Azure
Tags: java, azure, microsoft, programming
Published: 2022-09-12 13:46:20

## The evolution of Java

Over the past several years, the [**Java**](https://www.java.com/en/) ecosystem landscape has evolved, from monolith [Java EE](https://www.oracle.com/java/technologies/java-ee-glance.html) applications running on application servers and the Spring Framework to modern smaller-footprint [Spring Boot](https://spring.io/projects/spring-boot/), [MicroProfile](https://microprofile.io/), and [Jakarta EE](https://jakarta.ee/) microservices. Today, more and more Java developers are looking at how they can bring their existing Java applications to the cloud—or at how to build new [cloud-native](https://docs.microsoft.com/en-us/dotnet/architecture/cloud-native/definition) applications. Many organizations have significant investments in the migration of mission-critical Java applications running on-premises to fully supported environments to run these apps in the cloud.

💁🏻In this blog post, let's take a closer look at Java at [Microsoft](https://www.microsoft.com/et-ee?rtc=1) and on [Azure](https://azure.microsoft.com/en-us/) and understand what Microsoft can offer to modernize existing Java-based apps or bring new ones with the well-known practice of **Infrastructure as Code or IaC**.

## Java at Microsoft

We all know that Microsoft's evolution and main focus are on [Windows](https://www.microsoft.com/en-us/windows?r=1) with [.NET](https://dotnet.microsoft.com/en-us/) mindset. 

Well, I expect this comes as a surprise to some people as I was really surprised 😯 and had a ton of questions in my mind 😕 about _how it came to be Java on Azure and why Microsoft’s choice is Java? Why not use other public clouds such as AWS?_ Because at that time, I was totally new to Azure as I got used to developing, building, and deploying apps in the [AWS](https://aws.amazon.com/) ecosystem. Maybe one of the reasons is that recent [research](https://www.codingdojo.com/blog/unicorn-languages-report) has shown Java is in the top three languages among the world’s top start-ups and Java continues to gain more popularity according to [RedMonk](https://redmonk.com/sogrady/2021/03/01/language-rankings-1-21/) and [PyPL](https://pypl.github.io/PYPL.html) ✊. 

[AWS](https://aws.amazon.com/) and [Azure](https://azure.microsoft.com/en-us/) have been persistent in the market for many years and have taken the top honors for a while now. However, we are not going to answer the question that bothers people most about which Cloud vendor to choose. In this article [AWS vs Azure](https://azure.microsoft.com/en-us/pricing/azure-vs-aws/), you can learn more about the difference. 

After making a small investigation, I found out that Microsoft actively supports [Java community](https://lightrun.com/java/16-top-java-communities-forums-and-groups-the-ultimate-guide/) organizations to help promote the future of Java, including the ongoing development of the Java language and the [JVM](https://en.wikipedia.org/wiki/Java_virtual_machine). Microsoft is contributing directly to the [OpenJDK](https://openjdk.org/), and nowadays they are providing builds of the OpenJDK, starting with [Java 11](https://www.oracle.com/java/technologies/javase/jdk11-archive-downloads.html) and [16](https://www.oracle.com/java/technologies/javase/jdk16-archive-downloads.html), and providing support for those binaries on Azure.

![Microsoft supports Java](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yj1zi5aee9p7rw8reu9m.png)

We can't forget about [GitHub](https://github.com/), which hosts millions of Java repositories and provides free CI/CD and vulnerability scans, not only for Java but other popular programming languages as well.

Microsoft also publishes plugins for popular **Java IDEs** and **DevTools** such as [Maven](https://maven.apache.org/), [IntelliJ](https://www.jetbrains.com/idea/), and [Eclipse](https://www.eclipse.org/) to meet Java developers where they are and more easily integrate them into their development workflows. 

Last but not least, there are over half a million JVMs running across production products and services at Microsoft. [LinkedIn](https://www.linkedin.com/), for example, relies heavily on Java. The backend of [Yammer](https://www.microsoft.com/en-us/microsoft-365/yammer/yammer-overview) is also written in Java. You can read more about tools for the Java developers on 👉 [How Microsoft applies Java](https://aka.ms/AAi3mcc) e-book. 

![Java is used widely across Microsoft](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cnmnbvydrnt7j7vrprz.png)

## Java on Azure

Another surprise 😮 comes with Azure offerings for the Java ecosystem as it includes diverse technologies such as [Java SE](https://www.oracle.com/java/technologies/java-se-glance.html), [Jakarta EE](https://jakarta.ee/) (successor to Java EE and J2EE), Spring, numerous application servers, and other frameworks. Whatever you’re doing with Java—building an app, using a framework, and running an application server—Azure supports your workload with plenty of choices and deployment options including _infrastructure-as-a-service_ (IaaS) with no rearchitecting or code changes you can migrate existing apps, _containers-as-a-service_ (CaaS), and fully managed _platform-as-a-service_ (PaaS) for example, Azure Spring Cloud, which provides a managed platform for hosting **Spring Boot** microservice applications.

You can use [virtual machines](https://azure.microsoft.com/en-us/services/virtual-machines/) or [virtual machine scale sets](https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/overview) to host your Java applications. Scale sets, in particular, allow you to scale your applications across hundreds to thousands of VMs very rapidly. As we all probably know, virtual machines require a high level of management and configuration versus some other options out there.

If your organization leverages containers, you can use the Kubernetes service on Azure, known as [AKS for Azure Kubernetes Service](https://azure.microsoft.com/en-us/services/kubernetes-service/#overview). Or you can use [Azure Red Hat OpenShift](https://azure.microsoft.com/en-us/services/openshift/), a jointly managed and supported offering from [Red Hat](https://www.redhat.com/en) and [Azure](https://azure.microsoft.com/en-us/). On the other hand, going even further up the spectrum, if your organization does not want to work with virtual machines or be in the business of orchestrating containers, service like [App Service ](https://azure.microsoft.com/en-us/services/app-service/)provide a managed platform for deploying code or Containers.

![Migrate and Modernize your Java apps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w68k0tvp03hryr875sla.png)

Sometimes you don’t need an entire Java-based microservice. You can build serverless APIs with the help of [Azure Functions](https://docs.microsoft.com/en-us/azure/azure-functions/functions-overview). For example, Azure functions have a bunch of built-in connectors like [Azure Event Hubs](https://azure.microsoft.com/en-us/services/event-hubs/) to process event-driven Java code and send the data to [Azure Cosmos DB](https://azure.microsoft.com/en-us/products/cosmos-db/) in real-time. [FedEx ](https://www.fedex.com/en-ee/home.html)and [UBS](https://www.ubs.com/global/en.html) projects are great examples of real-time, event-driven Java. I also recommend you to go through 👉 [Code, Deploy, Scale Java your Way](https://cdn.graph.office.net/prod/media/java/code-deploy-scale-java-your-way.pdf?v=%7B1/string%7D) e-book to discover other ways of developing modern [Java applications on Azure](https://azure.microsoft.com/en-us/resources/developers/java/).

## Infrastructure as code for cloud

After you decide to move your infrastructure to Azure Cloud and choose suitable a deployment strategy, next task you have to do is automatically provisioning your services. Of course, you can always deploy your services manually from [Azure Portal](https://azure.microsoft.com/en-us/get-started/azure-portal/) or by using [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli). However, these approaches do not provide much flexibility when it comes to creating release stages, deployment versioning, updating, deleting, and managing all your cloud resources.

**Cloud applications** usually have separate deployment environments for the stages of their release lifecycle. It’s common to have development, staging, and production environments. These environments are composed of many resources such as networking resources: [Virtual Network](https://docs.microsoft.com/en-us/azure/virtual-network/virtual-networks-overview)(VNet), [Network Security Groups](https://docs.microsoft.com/en-us/azure/virtual-network/network-security-groups-overview) (NSG) or [secret resources](https://docs.microsoft.com/en-us/training/modules/manage-secrets-with-azure-key-vault/), [compute resources](https://azure.microsoft.com/en-us/products/category/compute/), [load balancers](https://docs.microsoft.com/en-us/azure/load-balancer/load-balancer-overview), and [databases](https://azure.microsoft.com/en-us/products/category/databases/). 

Without _Infrastructure as code_ (also referred to as IaC), managing all those resources can be a disorganized and delicate process. Because the DevOps team manually connects to remote cloud providers and uses API or web dashboards to provision new hardware and resources each time when a new release happens. Or they may manually make changes to one environment and forget to follow through on the other. This is how _environment drift_ occurs. In this case, you may consider using the widely accepted practice of IaC.

Let’s briefly review the IaC definition: 

> **Infrastructure as code** is an IT practice that codifies and manages underlying IT infrastructure as software. The purpose of infrastructure as code is to enable developers or operations teams to automatically manage, monitor and provision resources, rather than manually configure discrete hardware devices and operating systems. Infrastructure as code is sometimes referred to as programmable or software-defined infrastructure.

## Azure native support for IaC

Azure provides native support for IaC via the [Azure Resource Manager](https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/overview) model. Teams can define declarative [ARM templates](https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/overview) that specify the infrastructure required to deploy solutions.

Likewise, [Terraform](https://www.terraform.io/), [Ansible](https://www.ansible.com/), [Chef](https://www.chef.io/), and [Pulumi](https://www.pulumi.com/) third-party platforms also support IaC to manage automated infrastructure.

Specifying your cloud infrastructure through ARM templates alone can be quite challenging. Especially, if your resources are frequently updated and the different environments use a diverse set of resources (customization of deployments per environment), and you would like to set custom runtime validation of your resources. In this case, there is a need for a new tool to greatly simplify how you specify infrastructure in **higher-level Java code** with the best software development practices and manage low-level ARM models via **HTTP client APIs** by hiding internal complexity. The solution can support parametrization and service developers only define parameters for the resource in code and offer observability feature by default.

Azure offers an open-source [Azure SDK for Java](https://docs.microsoft.com/en-us/azure/developer/java/sdk/overview) that simplifies provisioning, managing, and using Azure resources from Java application code. To implement infrastructure as code in Java, you can use the [management libraries](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/resourcemanager#readme) of Azure Java SDK. With the management libraries, you can write configuration and deployment scripts to perform the same tasks that you can do through the [Azure portal](https://azure.microsoft.com/en-us/get-started/azure-portal/) or the [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli). There are plenty of samples available on how to use this library, you can check them on the ARM sample client library for the [Java GitHub repo](https://github.com/Azure/azure-sdk-for-java).

Java library can sync new changes automatically when ARM templates introduce new Azure features as soon as they are released without needing any code changes since the dedicated team is working on configuring them regularly to recognize the latest changes in Azure or if it is missing security-related updates.

## Java client library IaC example

Let’s assume that you create your [serverless function using Java](https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-java?tabs=bash%2Cconsumption) then you can make automation of deployment via the library. Or use other deployment options through like CLI, IntelliJ Idea, Azure Portal, and VS Code.   

Look at the following code example of how easy to create an Azure Function App in Java together with all required dependencies such as [storage account](https://docs.microsoft.com/en-us/azure/storage/common/storage-account-overview) and [app service plan](https://docs.microsoft.com/en-us/azure/app-service/overview-hosting-plans):

```java
Creatable<StorageAccount> creatableStorageAccount = azure.storageAccounts()
    .define("<storage-account-name>")
    .withRegion(Region.US_EAST)
    .withExistingResourceGroup(rgName)
    .withGeneralPurposeAccountKindV2()
    .withSku(StorageAccountSkuType.STANDARD_LRS);
Creatable<AppServicePlan> creatableAppServicePlan = azure.appServicePlans()
    .define("<app-service-plan-name>")
    .withRegion(Region.US_EAST)
    .withExistingResourceGroup(rgName)
    .withPricingTier(PricingTier.STANDARD_S1)
    .withOperatingSystem(OperatingSystem.LINUX);
FunctionApp linuxFunctionApp = azure.functionApps().define("<function-app-name>")
    .withRegion(Region.US_EAST)
    .withExistingResourceGroup(rgName)
    .withNewLinuxAppServicePlan(creatableAppServicePlan)
    .withBuiltInImage(FunctionRuntimeStack.JAVA_8)
    .withNewStorageAccount(creatableStorageAccount)
    .withHttpsOnly(true)
    .withAppSetting("WEBSITE_RUN_FROM_PACKAGE", "<function-app-package-url>")
    .create();
```

Java library supports almost all Azure services. You can see the full list of them [here](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/resourcemanager#:~:text=listed%20as%20below%3A-,List%20of%20services,-In%20the%20case). You can select to use the single-service package for each service and import it simply to your let’s say [Maven POM file](https://maven.apache.org/pom.html).

If you would like to leverage the [Azure Active Directory](https://azure.microsoft.com/en-us/services/active-directory/) for managing authentication flow, you can do it with Java code as well by simply adding the package.

```xml
<dependency>
  <groupId>com.azure</groupId>
  <artifactId>azure-identity</artifactId>
  <version>{LATEST_VERSION}</version>
</dependency>
```

## Conclusion

With IaC, you automate platform provisioning. You essentially apply software engineering practices such as testing and versioning to your DevOps practices. Tools like [Azure management client library for Java](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/resourcemanager) enable you to declaratively script the cloud infrastructure you require. On top of that, you can scale your Java applications smoothly and confidently on Azure including necessities such as security, supporting data and messaging services, caching, monitoring, and automation. 

### Related resources

➔ [What is infrastructure as code (IaC)?](https://docs.microsoft.com/en-us/devops/deliver/what-is-infrastructure-as-code).

➔ [Code, Deploy, Scale Java your Way](https://cdn.graph.office.net/prod/media/java/code-deploy-scale-java-your-way.pdf?v=%7B1/string%7D).

➔ [How Microsoft applies Java](https://cdn.graph.office.net/prod/media/java/how-microsoft-applies-java.pdf?v=%7B1/string%7D).

### Recommended content 💁

➔ Watch Video Tutorial:

- [Cloud-native Java development on Visual Studio Code](https://docs.microsoft.com/en-us/events/jdconf-2022/cloud-native-java-development-on-visual-studio-code).
  
- [JDConf 2022](https://docs.microsoft.com/en-us/events/jdconf-2022/?WT.mc_id=jdconf-56324-jemorg).

- [Azure Spring Apps](https://www.microsoft.com/en-us/videoplayer/embed/RE52aYU).

➔ Read the blog posts:

- [Microsoft for Java Developers Blog](https://devblogs.microsoft.com/java/).