Infrastructure as Code: A quick and simple explanation.
Tags: cloudnativeenginee, learncloudnative
Published: 2019-07-17 11:29:55

---
title: Infrastructure as Code: A quick and simple explanation.
published: true
tags: Cloud Native Enginee,Learn Cloud Native
canonical_url: https://www.thedevcoach.co.uk/infrastructure-as-code/
---

One of the most essential topics when you’re a [Cloud Native Software Engineer](https://dev.to/loujaybee/what-is-a-cloud-native-software-engineer-4fhm-temp-slug-4843097) is a concept called Infrastructure as Code. Yesterday as I started writing the second part to the series on [monolith to Microservice series](https://thedevcoach.co.uk/restful-api-serverless-framework/) I realised I couldn’t start the post until I had addressed the essential topic of Infrastructure As Code.

![](https://i0.wp.com/www.thedevcoach.co.uk/wp-content/uploads/2019/07/terraform.png?fit=462%2C267&ssl=1)

Just like many concepts in technology, infrastructure as code sounds scary. But in reality it’s a simple concept that any software engineer (or would be software engineer) can easily understand. I promise. Once you get over the first few hurdles you’ll wonder why you didn’t explore it sooner.

**By the end of this article you will understand what Infrastructure as Code is, why you need it and why you should ALWAYS create infrastructure in code and not manually.**

## What is Infrastructure?

Before we get into what Infrastructure as Code (the concept) we need to briefly touch on infrastructure on its own. We need to know what infrastructure is and why it’s relevant to Cloud Native Software Engineers.

For a long time, as a software engineer, I didn’t touch any infrastructure. Someone else always seemed to deal with it and I lived in a world of blissful ignorance. But somewhere along the way it became my turn to understand how my code actually gets onto and runs on a server. Looking back on my naivety about infrastructure makes me cringe a little. But you’re not going to make the same mistakes as I did, right? 

Okay, that’s enough pre-amble let’s get to it and answer our first question: What is infrastructure?… _**In software the concept of infrastructure is simply the hardware that our software runs on.**_

Yep, that’s really it.

## Infrastructure examples

But let’s give our conversation a little more concrete-ness by looking at a couple examples of common infrastructure.

**Hosts / Web Servers** — Hosts are the physical machines that run our code. Imagine your laptop tethered to the internet or 24/7. A web server is simply a specific type of optimised computer that is connected to the internet for the purposes of serving web content like the pages you’re currently reading!

**DNS servers**  — DNS servers (or sometimes just DNS records) map friendly URL domain names to the raw IP’s (machine addresses) where our code lives. When we’ve got a server we usually want some form of DNS.

**Databases** — Databases are the storage systems that hold the data of our applications. It’s your name on facebook, it’s your payment information on Netflix and it’s even the amount of money you have in the bank!

And these are just more traditional types of Infrastructure. The explosion of cloud provider services means we have lots of other types of infrastructure too, like [Containers,](https://dev.to/loujaybee/containers-benefits-and-making-a-business-case-22ol-temp-slug-6945730) [Serverless](https://dev.to/loujaybee/setting-up-a-rest-api-on-serverless-framework-5b9l-temp-slug-9273665) and all the companion infrastructure like [monitoring](https://dev.to/loujaybee/serverless-error-monitoring-strategies-41c2-temp-slug-1962472), alerting etc.

![Web server diagram (AWS)](https://i0.wp.com/d1.awsstatic.com/logos/Developer/webHostingAWSArchitectureDiagram.daf6433122d7f7c948371f4e397881183ddbb3a2.png?resize=760%2C409&ssl=1)

[Source: AWS](https://media.amazonwebservices.com/architecturecenter/AWS_ac_ra_web_01.pdf)

Okay, so that was a quick look at what infrastructure is and some examples. In summary: It’s the nuts and bolts that get our applications out onto the internet. Without it we have nowhere to put our code to run so that people can access it.

Which gives us a great jumping off point to look at what Infrastructure As Code is and why I’m raving about it.

## What is Infrastructure as Code?

![Terraform & CloudFormation](https://i2.wp.com/cdn-images-1.medium.com/max/1600/1*dnH0Nke1qhA-COTc5cBVoA.png?resize=760%2C249&ssl=1)

[Source: Hackernoon (Medium)](https://hackernoon.com/your-infrastructure-as-code-cloudformation-vs-terraform-34ec5fb5f044)

Infrastructure as code is the process of writing infrastructure in code! Literally. Okay, I realise that’s a bit imprecise, so let’s dig into it further…

Say you want to run a website, like WordPress (just like this website). For that, you’ll need a web server. So, what are your options? You could buy your own server, hook it up to the power, connect it to the internet, upload some files onto it and tinker around and voila! Sounds okay, right? I mean, it would work. Or — alternatively — you could open a cloud hosting account, poke around on the interface and you’re up and running. Both of these are viable options, but there is a third option…

And that is…. writing your infrastructure as code. First, you write in a file what configuration you want, then you execute a command and that infrastructure (in our case, a web server) will be created. The process is usually (but doesn’t have to be) done via a cloud account.

Put simply: Infrastructure as code allows us to codify the instructions we want to apply to our infrastructure estate (rather than doing it manually).

But, what’s the difference here? Why is writing our infrastructure as code so different to the previous manual options?

Let’s cover that now.

## Why is infrastructure as Code useful?

There are many reasons we’d want to write our Infrastructure as Code. In fact I’ll definitely write a whole article on that topic alone. But for today I wanted to cover a few of the main reasons that Infrastructure as Code is useful, just to paint some colour onto our conversation. As I want to make sure that you leave today not only knowing what Infrastructure as Code is but also feeling very confident on why it’s just so damn important!

### Collaboration

Infrastructure, when written in a text file can then be stored in version control system. Which allows many people to collaborate on the same codebase at the same time and utilise the cool tools like code review, branching, versioning, history, automated build pipelines etc.

### Traceability

Closely related to collaboration is traceability. Sometimes it’s important to know what changes have been made, and why. Maybe in future a new Software Engineer has joined our team and is questioning decisions. By having our Infrastructure as Code we’ve got a history of who made what changes, when and hopefully why (if it’s linked to a work ticket with a good description).

### Disaster Recovery

If something really bad were to happen to our software, our server gets destroyed in a fire (for instance) we usually need to recover it — and fast! By having our infrastructure written as code we can recover even large systems very quickly. All we need to do is re-run our infrastructure creation plans and voila, we’re back up and running in a very short amount of time.

### Compliance

It’s important in Software companies that we comply to certain rules and regulations. If you work for a bank, for instance, it might be a requirement that you ensure all servers have a certain level of security applied to them. When we write infrastructure as code we can easily analyse whether we’re applying the correct controls and restrictions to our software.

Ah! Hopefully we’re now starting to paint a more concrete picture of WHY infrastructure as code is so important and why it’s often superior to configuring our software manually. In fact Infrastructure as Code is so powerful that you should…

## Write ALL your infrastructure as code

![Terraform example code](https://miro.medium.com/max/830/0*amwV_MPhEcsLUpWr.)

[Source: Hackernoon](https://hackernoon.com/manage-aws-vpc-as-infrastructure-as-code-with-terraform-55f2bdb3de2a)

There are very few rules that I stand by in software and have come to adopt over time. But one of my rules is: Always test your software. And another rule I have is: setup ALL Infrastructure as Code. I have very few exceptions to these rules. Because if you don’t… ninety nine times out of a hundred you’re going to put yourself into a really difficult position with infrastructure that is difficult to change and a pain to manage. And for your own health and wellbeing, you don’t want to be that person.

Now that you’ve got a good handle on what Infrastructure as Code is, why not check out the first of the [monolith to microservice series](https://dev.to/loujaybee/setting-up-a-rest-api-on-serverless-framework-5b9l-temp-slug-9273665) to see how we put Infrastructure as Code into practice in a real life scenario.

**_Have you ever not written your infrastructure as code and have felt the pain? I want to hear some horror stories below!_**  

The post [Infrastructure as Code: A quick and simple explanation.](https://www.thedevcoach.co.uk/infrastructure-as-code/) appeared first on [The Dev Coach](https://www.thedevcoach.co.uk).