Welcome to part three of me navigating the clouds. In this Article we would be talking about something very important, something that practically should be the Backbone of DevOps and it is no other than Infrastructure as code.
Definition
According to Wikipedia, Infrastructure as code is the process of managing and provisioning computer data centers through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools. Now the question should be why Infrastructure as code? well, why not? Imagine a small startup named Trex Inc. Trex provide subscription services to the community. Trex's IT operations need to set up several servers to host applications, Operating systems, testing servers, production servers or even host databases. The bottom line is they need quite a bit of servers, before the unset of Infrastructure as code, managing IT infrastructure was a manual process. People would physically put servers in place and configure them. Only after the machines were configured to the correct setting required by the OS and applications would those people deploy the application, now imagine configuring the testing servers for the differnt operating systems just to make sure your application runs smoothly and after all that doing same to the production server, not only is it time wasting but unsurprisingly, this manual process would often result in several problems arising from human error.
Note we haven't started talking about cost, scaling and availability, consistency, how fast it is to restore back a server during down time? that will take all day but we are here to talk about infrastructure as code so lets dive into its attributes.
Attributes of Infrastucture as Code (Iac)
I guess this should be obvious but well infrastructure as code are written in well code, By code i mean various configuration languages like hashicorp's HCL for terraform, JSON or YAMl for ansible.
IaC codes are stored using Version Control System like github, gitlab, gitbucket, etc. VCS make it easy for users to collaborate by providing a centralized repository where all the code, configurations, templates, etc. can be found. It also lets users review or restore older versions of files.
Declarative or Imperative programming: Declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow here the system uses logic and a preset commands to find out what needs to be done from the list of given values . Imperative programming is a programming paradigm that uses statements that change a program’s state here the commands given will be in a step by step mode that tells the config file exactly what needs to be done.
Making a cheese sandwich declarative and Imperative style.
The process of making a sandwich is quite easy using this as an example to differentiate between Declarative and imperative programming.
Imperative cheese making
- get two slices of whole wheat bread
- get lettuce
- get cheese
- get salsa
- get turkey slices
Now for the Procedure
- put the lettuce between the two slices
- put the cheese on the lettuce
- put turkey slices on the lettuce
- put salsa on the lettuce.
serve. Notice how all steps were accounted for and represented. it simply follows a step by step approach to programming. Ansible is Imperative.
Declarative cheese making
food sandwich "cheese-sandwich"{
ingredients = [
"lettuce" , "cheese" , "salsa" , "turkey"
]
}
Declarative programming here is working based on some already created set of instructions on how to make sandwiches and all you just need to do is tell it what you what and list the values/ingredients you want used. Terraform is Declarative.
Idempotent and Consistent: Idempotent is the awareness of software to know the state of the configurations of the Infrastructre. For example if I request for a Sandwich it will be created and pushed and if I ask for a sandwich again it will send a notice notifying me of the presence of a sandwich already and will ask if i want to edit the existing sandwich or replace with another, Idempotency is achieved by either automatically configuring an existing target or by discarding the existing target and recreating a fresh environment. Consitency is self explanatory the infrastructure built today will build tomorrow.
Infrastructure as Code typically consists of two methods, push and pull. The main difference between the two methods is how the servers are configured. In the pull method, the to be configured server pulls its configuration from the controlling server whereas the push method, the controlling server pushes the configuration to the destination system Terraform and ansible use the push model.
Benefits of Infrastucture as Code
Fom the attribute given above we could dish out alot of benefits but to summerise it all
- Automated Deployment
- Consistent Environment
- Processes could be easily repeated with a click of a button
- Reusable components
- Documented Architecture
Summary
- Infrastructure as Code isn't Scary
- Manual processes are the enemy
- When in doubt have a Sandwich