Blog

How to Create Infrastructure as Code for Java Applications

Written by Kunal Dabir | May 6, 2024 6:09:48 PM

You’ve built the coolest application, or the most innovative feature, or you’ve made some updates crucial to a customer. Now you simply need to create the infrastructure as code (IaC) to get that application deployed to AWS. And in this final step, you are annoyed. The challenge for many of us is that we want to focus on application development, without worrying about the intricacies of how it gets deployed and hosted. We want it to be deployed and just work. With the cloud, we can absolutely do this more easily, but it requires (in many cases) adding infrastructure expertise to the endless skillset asked of us. 

Options for Creating Your IaC

So you turn to the options at hand: 

  1. Start from scratch. Whether you are using Terraform, Chef, Puppet, Pulumi, Ansible (or others), you are going to need to create your configuration files that declare your desired state. With Terraform specifically, you’ll write your configuration file (.tf files), and you’ll write your variable declaration file (variables.tf or variables.tf.json) to declare the input variables required to provision resources, and you’ll write your variable definition files (terraform.tfvars) to assign values to the input variables. Once you terraform plan, confirm your infrastructure, and `terraform apply`, finally you will get your state file (terraform.tfstate) to store info about the managed infrastructure. This is NOT easy! It requires you to know a lot of information about your infrastructure, security policies, and resource requirements - all beyond how you want your application to run. 

  2. Use a template. Fortunately, there are templates that DevOps and platform engineers have created for you. These templates will save you time and you won’t have to spend weeks composing IaC from scratch. The challenge with templates are: 1. you still have to know what to fill them out with, which often means a treasure hunt for information, and 2. they get out of date quickly, so unless they’ve just been updated, you might be introducing risk to your Java application without knowing it. You can read more about the problems with golden templates (no one wants to talk about). 

  3. Create infrastructure from code. You have already spent a lot of time developing your Java application. You’ve made choices within the code that you want to run smoothly on the infrastructure. So what if you could create your IaC from your application code? The good news is infrastructure from code allows you to do just that. 

Infrastructure from Code for Java Applications

Infrastructure from Code treats your Java application as the source of truth. It auto-generates IaC from the application code itself to standardize and streamline cloud deployments. It also doesn’t require any application source code changes. Using infrastructure from code can save you a lot of time as you are creating IaC that adheres to best practices and policies including AWS and Azure policies, AWS Well Architected Framework, and compliance standards like SOC 2, HIPAA, NIST, CIS, GDPR (and plenty more). 

While infrastructure from code is a new approach to IaC generation, it is absolutely the streamlined and efficient solution to getting an application deployed more easily. 

Three Ways to Try Infrastructure from Code

If you have a Java application and want to try infrastructure from code, StackGen has three different ways for you to get started. 

  1. Dev Edition: The free-to-use dev edition is a SaaS based version where you can connect your repos, view your deployment architecture, select AWS policies and compliance standards and then generate your IaC. From there, you can create a pull request and follow your standard CI/CD process. If you don't have a repo to connect, you can use the demo repo made available in the dev edition. 

  2. CLI: If you are not into a UI, you can also use the CLI to get your IaC created. It follows a similar process. You can walk through the process using our docs

4 Steps to Generate Your IaC for Your App

Once you’ve set up your free StackGen account by logging in with GitHub, GitLab or Google, you can follow these four steps to generate your IaC to deploy your Java application: 

1. Connect a repo

You need to make sure that StackGen has access to the repos you use to build your application, whether that's several repos of various microservices or one monolith repo. If you don't see the repos you need listed when you click "Repositories" in the left-hand navigation bar, you will need to connect repos to StackGen.

2. Create an appStack

To get the IaC files your application needs, you need to create an appStack. You have two options for how to do this:

3. View and edit the topology

Once you’ve connected your repo, selected the language and cloud and policies you want to apply, you’ll create an appStack. You can then click in to view your architecture and make any required changes with the drag and drop feature. 

4. Generate IaC files

After you are happy with your architecture, simply export your IaC. Then follow your usual deployment process. 

Auto-generate your infrastructure as code in just minutes versus days or weeks. You no longer need to be an infrastructure expert, instead you can focus on the development work you were hired to do. 

Try StackGen to generate your infrastructure from code.