Delving into infrastructure as a code

Finally, in a book about Python, we get to a section about code. So far, I’ve given you a lot of information about what needs to be accomplished but to accomplish the things we want especially in this book, we must have a method, a tool, a weapon, i.e., code.

Now the word “code” scares a lot of people in the tech industry, even developers. It’s weird being afraid of the thing that is under everything you work with. But that’s the reality sometimes. If you, dear reader, are such a person, first off, it’s a brave thing to purchase this book, and secondly, all you are doing is denying yourself the opportunity to solve all the problems you have in the world. Seriously.

Now, the reason is that code is the weapon of choice in almost every situation. It is the solution to all your automation problems, monitoring problems, response problems, contract problems, and maybe other problems that you may have that I don’t know about. And a lot of it requires a minimal amount of code.

Important note

Remember this: the amateur writes no code, the novice writes a lot of code and the expert writes code in a way that it seems like they’ve written nothing at all, so expect a lot of code in this book.

Let me explain further. To maintain the consistency of service required by DevOps, you need something constant; something that your resources can fall back on that they can use to maintain themselves to a standard. You can write code for that.

In addition to that, you need to be able to automate repetitive tasks and tasks that require reactions faster than what a human being can provide. You need to free up your own time while also not wasting your client’s time. You can write code for that.

You also need to be flexible and capable of dynamically creating resources regardless of the change in environment as well as the ability to switch over to backups, failovers, and alternates seamlessly. You can write code for that.

Infrastructure as code (IaC) is particularly useful for that last part. In fact, you can use it to encapsulate and formulate the other two as well. IaC is the orchestrator. It gives the cloud services a proverbial shopping list of things it wants and the configuration it wants them in and in exchange for that, and it gets the exact configuration that was coded on it.

The fact that IaC is a get-exactly-what-you-want system is a word of caution because as with everything involving computers, it will do exactly what you want, which means you need to be very specific and precise when using these frameworks.

Let’s look at a little sample that we will use to demonstrate the concept behind IaC using some simple pseudocode (without any of that pesky syntax).