Learning Library

← Back to Library

Infrastructure as Code: Imperative Approach

Key Points

  • Infrastructure as Code (IaC) is essential for modern, fast‑moving applications that need to provision and de‑provision resources repeatedly, often hundreds of times per day.
  • Manual documentation of infrastructure steps can lead to missing configuration details, causing environments (e.g., dev vs. test) to diverge and break application functionality.
  • An imperative IaC approach uses CLI commands or scripts (e.g., bash) to specify exactly how each component—Kubernetes clusters, VMs, VPCs—is created, giving fine‑grained control.
  • While imperative scripting offers powerful, step‑by‑step provisioning, it also adds complexity for tasks like teardown, scaling, or environment replication, requiring additional custom code.
  • Adopting IaC (especially moving beyond ad‑hoc scripts) helps eliminate configuration drift, ensures repeatable environments, and streamlines future infrastructure changes.

Full Transcript

# Infrastructure as Code: Imperative Approach **Source:** [https://www.youtube.com/watch?v=zWw2wuiKd5o](https://www.youtube.com/watch?v=zWw2wuiKd5o) **Duration:** 00:08:50 ## Summary - Infrastructure as Code (IaC) is essential for modern, fast‑moving applications that need to provision and de‑provision resources repeatedly, often hundreds of times per day. - Manual documentation of infrastructure steps can lead to missing configuration details, causing environments (e.g., dev vs. test) to diverge and break application functionality. - An imperative IaC approach uses CLI commands or scripts (e.g., bash) to specify exactly how each component—Kubernetes clusters, VMs, VPCs—is created, giving fine‑grained control. - While imperative scripting offers powerful, step‑by‑step provisioning, it also adds complexity for tasks like teardown, scaling, or environment replication, requiring additional custom code. - Adopting IaC (especially moving beyond ad‑hoc scripts) helps eliminate configuration drift, ensures repeatable environments, and streamlines future infrastructure changes. ## Sections - [00:00:00](https://www.youtube.com/watch?v=zWw2wuiKd5o&t=0s) **Automating Cloud Infrastructure with IaC** - Sai Vennam explains how Infrastructure as Code enables rapid, repeatable provisioning of Kubernetes, legacy VM, and VPC resources to create and replicate development and test environments in the public cloud. - [00:03:09](https://www.youtube.com/watch?v=zWw2wuiKd5o&t=189s) **Declarative vs Imperative Infrastructure Automation** - The speaker contrasts imperative scripting with declarative tools like Terraform, highlighting how declarative approaches simplify scaling, error handling, and state management in infrastructure automation. - [00:06:12](https://www.youtube.com/watch?v=zWw2wuiKd5o&t=372s) **Immutable vs Mutable Infrastructure Pitfalls** - The speaker contrasts mutable scripts that cause configuration drift with immutable infrastructure, arguing that tracking changes and using immutable deployments avoids limbo states and the need to wipe and rebuild environments. ## Full Transcript
0:00Hi everyone, my name is Sai Vennam 0:02and I'm with the IBM Cloud team. 0:03Today, let's talk about Infrastructure as Code. 0:06These days it's increasingly crucial to automate your infrastructure 0:10as applications can be deployed into production up to hundreds of times per day. 0:14In addition, infrastructure is fleeting 0:16and can be provisioned or deprovisioned in response to load. 0:19Let's start with an example. 0:21Let's say you're building out an application 0:24and you've chosen a public cloud. 0:26Now, the first thing you've decided to do is 0:28build your application on Kubernetes. 0:30So, we'll have a Kubernetes application stack. 0:35Now we don't actually have to dive deeper into Kubernetes 0:38because it isolates the hardware from the application layer. 0:42So, we don't actually have to go in deeper, 0:44it will manage that for us. 0:46Next, let's say that after a week of development 0:49we've decided to bring in a VM 0:51that holds a legacy application that we have not modernized just yet. 0:55So, we'll bring on a VM 0:59and now to actually connect up those dots 1:02we'll need a VPC (Virtual Private Cloud). 1:09So, there we go, we have a basic infrastructure in place. 1:14Now, let's say that I've developed this, it's great, 1:16all the infrastructure details are documented. 1:19Now I'm ready to move it into a test phase. 1:21Now, I know that for best practice what I should do is create a whole new environment 1:29that mimics my development environment. 1:33To do so, I'll go back to my documentation 1:36and start following the steps to spin up that infrastructure. 1:40But let's say that maybe I forgot to document one of the config switches that I've changed, 1:45or maybe the platform is different in how it handles provisioning infrastructure. 1:50Regardless, the application and test don't work the same way in the new environment. 1:55I decide, "OK, we need to fix this problem" 1:58- and to never have this problem in the future again, 2:01we need to take advantage of infrastructure as code. 2:04Let's talk about the first approach to infrastructure automation, 2:07it's going to be imperative. 2:11Now, this is kind of intuitive for most people 2:15because an imperative approach allows you to define step-by-step 2:19how to get your infrastructure into a certain state. 2:21So, in general an imperative approach would use something like 2:25a CLI along with maybe a bash script. 2:28So, for example, in this case 2:31we could do something like, "cli create k8s", 2:37and then we would define some additional commands to 2:40customize that Kubernetes deployment. 2:42We'll do the same thing for the VM, as well as the VPC. 2:51So, an imperative approach has an advantage. 2:55It allows you to really define step-by-step of how your infrastructure is provisioned 3:00and that generally comes with more power as well 3:03because you're using the cloud tools 3:05and doing it in a step-by-step process. 3:09But at the same time, this can come with complexity. 3:12For example, if you wanted to tear down your VMs, 3:15or your environments rather, 3:17or let's say you wanted to scale it up or down, 3:20you'd have to write custom scripts. 3:22It's not handled for you in an imperative approach, 3:24so this generally doesn't scale well. 3:28Another approach to Infrastructure Automation is going to be Declarative, 3:33and this is actually my favorite approach. 3:36Now, a declarative approach would be something like Terraform 3:41and what it basically allows you to do is to define the final state of your infrastructure 3:46and then it lets the provider handle the rest. 3:49So, instead of defining every step, 3:51you just define the final state. 3:53So, in this example maybe you would do something like 3:56define a Kubernetes resource 4:01and a VM resource, as well as a VPC resource. 4:09Another great thing about this 4:12is it's generally managed through just simple config maps. 4:15So, if you wanted to do something like 4:17define a host you could do that, 4:20maybe a domain, or maybe even the subnets. 4:28So, in general a declarative approach 4:31allows you to more easily manage the configuration 4:34and is my preferred approach for automating infrastructure. 4:38Let's take this simple example, 4:40if you ran the imperative script multiple times 4:43you would actually end up with a multiple environments 4:46and, in addition, let's say one of the steps halfway through failed 4:50then you would have to add error handling 4:53to tear down the steps that did succeed. 4:56Now with the declarative approach, no matter how many times you run the script, 4:59you end up with the exact same infrastructure. 5:02So, you could do it the first time, 5:04provision your environment, 5:06and then maybe run it again 5:07later on to ensure that your environment hasn't changed. 5:11So, I'd say this is very important to can understand the different approaches infrastructure as code, 5:16but, in general, I do prefer a declarative approach. 5:20Next let's talk about DevOps. 5:23Now we all understand how important about DevOps is. 5:26When developing an application, you'll first write some code, 5:30you'll want test that it actually works, 5:34and then you want to push it into production. 5:40And then you want to make sure that all of that is 5:44always working and you can repeat those processes. 5:46Now, I know there are teams out there that have a perfect agile DevOps flow, 5:51but because they're working with legacy infrastructure 5:53they have to open a ticket every time they want to get a new VM, 5:56and that's just due to the infrastructure that they're running on. 6:00Now, that really holds them back. 6:03Now, with Infrastructure as Code, when it's supported, 6:05it allows you to treat your infrastructure 6:08with the same level of quality that you treat your code. 6:10So, this includes things like versioning. 6:12So, essentially, you want to make sure that any time infrastructure changes, 6:16you're tracking that, 6:17and is generally a best approach for automation. 6:22The last thing I want to talk about 6:24is immutable vs mutable infrastructure. 6:27Now, breaking that down: 6:29an immutable infrastructure is one that can't be changed, 6:34it can't be mutated. 6:36Now, at first impression, that might sound like a bad thing, 6:39but let's break this down by seeing an example 6:42with a mutable approach to infrastructure architecture. 6:46So, we have our example here, 6:48and let's say that we're building out the app and we decide 6:52we need a database. 6:53So, to do so, we'll run a script 6:57in our development environment. 6:59This brings up that database within our VPC. 7:02Now all of this is working great, 7:04so we say, "Hey, let's just run that script across all of the environments that we have." 7:10Now, let's say that 99% of the time that works fine, 7:13but some of the times it fails, 7:15and you're in a weird limbo state. 7:17So, let's break that down. 7:19So, we're going from version one to version two. 7:27Now, we have infrastructure code 7:30in place to bring up version one, 7:32but now we ran this custom script 7:34to move it from version one to version two. 7:39What we essentially have right now is something called "configuration drift", 7:43or "environment drift". 7:45Our existing environment is no longer matching what we have in our automation. 7:51Now, the problem is: 7:52to help debug those problem situations 7:55you would have to wipe out the entire environment 7:59and then redeploy version one and then run those scripts. 8:02Now, that might seem OK the first couple of times you do it, 8:05but when moving to scale it becomes incredibly hard to maintain. 8:09So, with an immutable approach 8:11to infrastructure and infrastructure automation, 8:14every time you want to make a change to the infrastructure 8:17you bring up a brand new environment alongside the old one 8:21and then, once you verify that they're both working, 8:23you can bring down the older version. 8:26Now, it's a little expensive because 8:28you can imagine that you're running both environments at the same time, 8:32but, in general, it's the best practice 8:35to ensure that your infrastructure can move to scale. 8:38Thanks for joining me for this quick overview of infrastructure as code. 8:41If you want to learn more about this, or DevOps check out the link below. 8:45Remember you can always get started with a free account on IBM Cloud. 8:49Thank you.