Learning Library

← Back to Library

Terraform: Declarative Infrastructure Automation

Key Points

  • Sai Vennam from the IBM Cloud team introduces Terraform as an open‑source, declarative tool for automating infrastructure and services.
  • He contrasts Terraform’s “declare the destination” approach with imperative step‑by‑step automation, using a rideshare analogy.
  • The example infrastructure includes a VM, a Kubernetes cluster, and a VPC that interconnect them, illustrating the desired vs. current (empty) state.
  • Terraform workflows consist of three main phases: writing the *.tf* configuration file, running **terraform plan** to compare desired and actual states, and executing **terraform apply** to provision the resources.

Full Transcript

# Terraform: Declarative Infrastructure Automation **Source:** [https://www.youtube.com/watch?v=HmxkYNv1ksg](https://www.youtube.com/watch?v=HmxkYNv1ksg) **Duration:** 00:08:44 ## Summary - Sai Vennam from the IBM Cloud team introduces Terraform as an open‑source, declarative tool for automating infrastructure and services. - He contrasts Terraform’s “declare the destination” approach with imperative step‑by‑step automation, using a rideshare analogy. - The example infrastructure includes a VM, a Kubernetes cluster, and a VPC that interconnect them, illustrating the desired vs. current (empty) state. - Terraform workflows consist of three main phases: writing the *.tf* configuration file, running **terraform plan** to compare desired and actual states, and executing **terraform apply** to provision the resources. ## Sections - [00:00:00](https://www.youtube.com/watch?v=HmxkYNv1ksg&t=0s) **Terraform Declarative Automation Overview** - Sai Vennam introduces Terraform, contrasts its declarative model with imperative scripting using a rideshare analogy, and outlines a sample setup involving a VM, a Kubernetes cluster, and a VPC. - [00:03:04](https://www.youtube.com/watch?v=HmxkYNv1ksg&t=184s) **Terraform Apply Phase and Modules** - The speaker explains how Terraform’s apply phase uses provider APIs and your API token to provision resources and generate output variables (e.g., URLs), while emphasizing Terraform’s modular, pluggable design and strong community support for cloud providers. - [00:06:13](https://www.youtube.com/watch?v=HmxkYNv1ksg&t=373s) **Terraform DevOps-First Load Balancer Deployment** - The speaker explains how Terraform’s code‑plan‑apply workflow lets you iteratively add a load balancer to an existing VPC/VM/Kubernetes setup, showcasing a DevOps‑first method that prevents configuration drift. ## Full Transcript
0:00Hello everyone, my name is Sai Vennam 0:01and I'm with the IBM Cloud team. 0:03Today we're going to be talking about Terraform. 0:06Terraform is an open-source tool 0:08originally developed by HashiCorp, 0:10that enables you to automate and manage your infrastructure 0:13and platform and services as well. 0:15It does all of this using a declarative language 0:19and that's the first thing we're going to focus on 0:21when we go through our example 0:22to go through Terraform. 0:25So, I generally like to start with an example here. 0:29So, say you're driving in your car, 0:31trying to get from point A to point B. 0:33Generally you would follow a set of instructions, right? 0:36So, going from point A to point B, 0:38you have to take a left turn, 0:40get on the highway, take this exit 0:42- you end up at your destination. 0:44Now, that's kind of an imperative approach to automation. 0:48The way Terraform does it: 0:50imagine you called a taxi or a rideshare service 0:53and told them exactly where you wanted to go - point B. 0:57The car, or the driver, then takes care of the rest 1:00- you don't have to worry about every step of the way to get there 1:03and that's the approach Terraform takes. 1:05That's why it's so effective 1:07at managing your infrastructure. 1:09Today we'll start with an example. 1:11Say you've got 3 different resources 1:13that you're trying to spin up as part of your infrastructure. 1:16We'll say you've got a VM, 1:19we'll also say you've got a Kubernetes cluster, 1:24and say that they're networked together using a VPC 1:27- or "Virtual Private Cloud". 1:30So, current state: nothings there. 1:33Desired state is this set of infrastructure. 1:36So, in Terraform there are going to be 3 major phases, 1:39and we'll go step-by-step. 1:40The first phase: 1:41you actually have to create the the terraform file. 1:44So, we'll start with that - 1:46where you actually write or code up that Terraform file. 1:50Now, in this Terraform file we're going to have 1:523 major resources, right? 1:54So, we'll start with the VM 1:57and a set of arguments to support that. 1:59Things like the name, and networking, data center, 2:02that kind of thing. 2:03We'll have the Kubernetes cluster, 2:06a set of arguments for that, 2:07and then finally we've also got the VPC. 2:10It's probably going to refer 2:11to some of the network settings of the VM and the Kubernetes cluster, 2:14but it's going to network those together 2:16and so it has arguments to support that. 2:19So, that's your coding phase. 2:21Say you've got a Terraform file ("TF" file), 2:24with these things defined. 2:27Next up, we've got the "plan" phase. 2:30Now, this is an actual Terraform command. 2:33So, in the TF CLI, or "Command-Line Interface", 2:35you can run "terraform plan", 2:37and what it's going to do is it's going to compare 2:39the desired state to what actually exists. 2:42So, on day-zero it'll notice that you have 2:45none of these resources, 2:46so it'll say, "Hey, you have to create all of them". 2:49So, create the VM, 2:51create the cluster, 2:54as well as create the VPC. 2:56So, it has a plan - 2:57and it tells you, the user, this is what it's going to do. 3:00Next, if everything looks good, 3:02you'll do the "apply" phase. 3:04This is another Terraform CLI command. 3:06In the apply phase you can actually 3:08take those resources and spin them up. 3:11So, that's exactly what's going to happen: 3:13Terraform is going to work against the cloud providers 3:16using real APIs - your API token - 3:18to spin up these infrastructure resources 3:21and it's going to output some interesting or 3:23auto-generated variables along the way. 3:25So, for example, maybe the Kubernetes dashboard URL, 3:29or maybe even a URL to access your application, 3:36but, regardless, 3:38it's going to output a number of these output variables. 3:43So, that's generally how the terraform workflow goes 3:50to get you from point A to point B 3:52in a situation where you had nothing 3:54and now you have something. 3:56So, that's what I wanted to start with. 3:57Next, I want to go into the fact that Terraform 4:00has a strong open community 4:02and it's pluggable by design. 4:06So, by "pluggable" essentially what I mean here 4:09is the fact that it's made up of 4:10modules and resources where 4:12you put in input variables and output comes out, 4:15as well as the community is out there, 4:17and cloud providers are out there, 4:18building and supporting things called "cloud providers", 4:21or "providers" in the Terraform world, 4:23to enable you to connect up to any cloud 4:25and automate infrastructure there. 4:27So, in this process, 4:29we actually made something called a Terraform module. 4:32A Terraform module is a way of grouping together some terraform automation. 4:40So, we've got a terraform module here. 4:42It actually takes a set of inputs 4:43and also creates some output 4:46and as part of every module, 4:48- well, you can define this, but, 4:50in general, when you're working with cloud providers, 4:53and using Terraform, 4:54you'll also define what's called a "provider". 4:57Now, a provider can be a number of things, 5:00but in our case, we are using it as a cloud provider 5:03to connect up to a given cloud. 5:08So, that's the first thing that a provider can do is 5:11connect you up to IaaS providers 5:15- IBM Cloud, or AWS, or Azure - 5:18it enables you to connect up to some infrastructure provider, 5:21spin up things like VMs, 5:22or that kind of thing - maybe bare metal. 5:25You can also use a provider, in the Terraform sense, 5:28to spin up platforms as well. 5:31So, it enables you to manage, for example, 5:33Cloud Foundry running in the cloud. 5:37Finally, you can even manage SaaS offerings. 5:40Things like CloudFlare, or other software services, 5:44can actually be managed by Terraform. 5:46So, although Terraform is considered to be 5:49an infrastructure automation tool, 5:51it's expanded its role 5:52to support other types of providers as well. 5:56So, essentially, we've got our flow here, 5:58our module, inputs, and outputs, 6:00and so we've got a kind of pipeline 6:01to be able to spin up resources. 6:04The last advantage I want to mention here with Terraform 6:07is the fact that it enables you to essentially 6:11have the best practices for DevOps. 6:13So, we'll say "DevOps first". 6:18Now, let's take an example for this. 6:20Now, in our example here, 6:22we did a day-one kind of deployment, 6:24- nothing to something. 6:25But let's say we're going back and iterating on this. 6:28So, we've got our VPC, 6:31we've got our VM, 6:33as well as Kubernetes. 6:37Now let's say we're iterating on this and we 6:38want to create something new on top of this infrastructure. 6:42Let's say we want a load balancer. 6:47So, that's our desired state. 6:49So, we'll do our three phases, 6:51starting with the code phase. 6:53So, we'll say here that we want a load balancer, 6:57and we'll have set of arguments to support that. 7:00When we get to the plan phase, 7:02Terraform is going to realize, 7:03"hey we actually already have the VM, Kubernetes, and VPC", 7:07so it checks the current state of the world 7:09and realizes that all we need is that new load balancer. 7:13So, it will say, "hey, let's just add the load balancer this time around", 7:17and, as a user, you'll confirm that make sure it looks good, 7:20go to the apply phase, go ahead and apply it, 7:23maybe some more config variables will come out 7:25for this new capability that you added. 7:28So, essentially, with Terraform 7:29you have a DevOps-first approach, 7:32one of the key advantages to this 7:34is that it enables you to avoid potential "config drift" - 7:37that's when the configuration that defines your infrastructure 7:41actually doesn't match what's actually there. 7:44So, as long as all changes to your infrastructure 7:46go through the Terraform file and Terraform pipeline, 7:49you can essentially eliminate the risk of configuration drift. 7:53In addition, since we've set up 7:54a module where we can take input variables and have a final state, 7:58what we can essentially do is recreate this 8:01- maybe switch up the environment variables a bit 8:03and then create a whole other environment 8:06that looks just like this 8:08but this time we can make one for Test. 8:13And maybe this one was Development, 8:15and we can do that again for maybe Production. 8:18It's another advantage of taking a Terraform 8:21approach to infrastructure automation: 8:23it enables you to put DevOps first. 8:26Thanks for joining us for this quick overview of Terraform. 8:28If you enjoyed this video 8:30be sure to check out our other video on infrastructure as code. 8:33As always, if you like this video, or have any comments, 8:36be sure to drop a "like" or a comment below. 8:38Stay tuned and subscribe for more videos like this in the future. 8:42Thank you.