One of the most essential topics when you’re a Cloud Native Software Engineer is a concept called Infrastructure as Code. And it’s becoming an even more essential topic with the growth in Cloud Native technology like Serverless as Infrastructure as Code is a key Serverless concept.
Yesterday as I started writing the second part to the series on monolith to Microservice series I realised I couldn’t start the post until I had addressed the essential topic of Infrastructure As Code.
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.
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!
Networks — The environment which host our resources. Networks define who can access our resources and how.
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, Serverless and all the companion infrastructure like monitoring, alerting etc.
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?
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!
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.
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).
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.
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
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 main concepts of Terraform. Or, setup a personal AWS account to experiment with Infrastructure As Code on AWS. Or even start looking into infrastructure as code tools such as Terraform.
Have you ever not written your infrastructure as code and have felt the pain? I want to hear some horror stories below!