How We Built Reusable Infrastructure

Senior Software Engineer Andrew Solomon explains reusable infrastructure at the International JavaScript Conference.

06.16.2022
 -

Andrew Solomon, a Senior Software Engineer at Choco, had the privilege of presenting at the 2022 International JavaScript conference. His talk, “Using TypeScript to Create Reusable Architecture Patterns,” focused on how Choco used TypeScript to create reusable architecture patterns–and how much easier it is now for developers to create new resources without specialized cloud knowledge.

At a startup, it often falls on backend engineers to set up their own infrastructure, which can have a steep learning curve. Andrew shares some of the unique challenges Choco faces, as well as some details from his presentation below.

A Challenge to Share Resources

Andrew says that before implementing reusable architecture, the engineering team at Choco was experiencing issues with a lot of repeated and re-pasted boilerplate code in order to set up infrastructure on AWS. Not only that but there was really no good way for team members to share commonly used infrastructure patterns once they were created.

In addition, when someone new joined the team, it was difficult and time-consuming for them to learn how to create architecture using old YAML templates. The team needed a better way to share their resources, with less time spent onboarding new developers. The solution for Choco was provisioning infrastructure with AWS’s CDK using TypeScript.

Some background: What Is IaC?

Infrastructure as code (IaC), is the process of managing and provisioning infrastructure through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools. The IT infrastructure can be managed using configuration files, or code.

Once the infrastructure is designed using code, you can review and make changes to it just as you would with any other code. Not only that, but it becomes easier to simplify your tech stack by using one language for backend code and IaC. You can create abstractions to reduce complexity, more easily write complex logic in templates, and write test units using familiar frameworks.

How Does IaC Work?

IaC is dependent on remotely accessible hosting to function. The configuration management tool needs to connect to and modify the remote host. The enabled cloud hosting platform offers APIs that allow users to automatically create, delete, and modify infrastructure resources on demand.

There are two approaches to take with IaC; declarative or imperative. A declarative approach defines the desired state of the system, including resources and properties. The IaC tool will configure it for you. Alternatively, an imperative approach defines the specific commands needed for the desired configuration. The commands need to be executed in the correct order.

The preferred approach to IaC is to use declarative definition files where possible. The definition files specify what the environment requires rather than the how.

Why Use Code to Define Architecture?

Instead of code, most other companies use infrastructure templates like the YAML ones Choco used to use, says Andrew. This has its challenges. First, these templates don’t provide all of the features of a modern programming language when creating infrastructure as code. This makes it difficult to abstract complexity into constructs, which results in a lot of repeated and pasted code. This also makes IaC files more complex.

Using code to define architecture has many benefits, including the ability to:

  • Easily create multiple identical environments

  • Reduce mistakes from manual configuration

  • Document what infrastructure you have and any changes you make

  • Use the same code review tools to discuss proposed infrastructure changes

  • Have easier and simpler infrastructure management (scaling)

  • Manage version control

IaC Tools

Traditionally, a configuration management platform uses human and machine-readable text files written in a markup language like YAML to declare tasks and sequences for the platform to execute. However, recent cloud development kits (CDKs) have enabled developers to write IaC using modern programming languages. In 2019, Amazon Web Services (AWS) launched its CDK, allowing developers to write IaC in JavaScript and TypeScript. Terraform has also released its own CDK version, which is in active development.

Using Modern Programming Languages for IaC

As mentioned above, in the last few years a number of tools have been developed that allow us to modern programming languages for IaC. This means rather than wrestling with YAML or JSON templates, we can use the same programming language for IaC that we use for our backend. This means developers can use all of the benefits provided by a fully functional language, as well as develop in an environment that is familiar to them.

Drawbacks

Despite the many advantages of using tools like the CDK for IaC, there are a few drawbacks to consider. First, your DevOps team may not be familiar with reusable architecture, which creates a learning curve. And while IaC does provide more flexibility, it can introduce the greater possibility of unclean code. Once IaC is instituted, you’ll no longer have a simple list of resources, which means your code will be less declarative overall.

Benefits of using TypeScript for IaC at Choco

Though the implementation is still in process, there have been many positive changes due to reusable infrastructure. First, Choco has created a shared library of common infrastructure patterns that anyone in the company can use. This resource makes it easier for new team members who are already familiar with Typescript to get started.

Additionally, the infrastructure code is cleaner overall as a result of abstracting its complexity into constructs.

Going forward, the Choco team looks to achieve even more benefits using IaC to manage its cloud infrastructure. This will include a greater speed of development with fewer mistakes as developers can leverage the shared library of constructs.

The team also expects to spend less time onboarding new developers.

Are you a developer looking for a new role that challenges your skills and inspires you to grow? You might be exactly what we’re looking for. Check out our open positions here.