#35 - Infrastructure as code (IAC) revisited 2021
Manual fixes become configuration drift when the path through code is too slow.
17 min read
Our first episode was about Infrastructure as code, and we feel that it is time to revisit the topic after almost two years. Another reason is the release of the second edition of Infrastructure as Code book by Keif Morris. Thus, in this episode, we revisit the definition of Infrastructure as code and try to summarize what has changed over the years. We hope you like it!
You have the repo and you still have the drift
Plenty of teams have a Terraform repository and still have the problems it was supposed to fix. There’s a server somebody built by hand two years ago that nobody wants to touch. A developer who needs a database opens a ticket and waits for someone on the infrastructure side to have a free afternoon. And when somebody asks whether production matches what’s in the repository, the honest answer is that you’d have to go and look.
Infrastructure as code was the first thing we talked about on this show, in February 2020. Almost two years on it’s worth asking what actually moved, because two things did. Kief Morris published a second edition of Infrastructure as Code in December 2020, a much bigger book than the 2016 original, with whole chapters on things that barely existed when he wrote it. And at the end of September, AWS shipped an API that changes how quickly your tooling catches up with the services you want to use.
So we went back to the definition, and we’re using the book’s instead of inventing one. Morris calls infrastructure as code “an approach to infrastructure automation based on practices from software development” that “emphasizes consistent, repeatable routines for provisioning and changing systems and their configuration”. You make changes to code, then use automation to test and apply those changes to your systems. Nothing in there names a tool, and that turns out to be the useful part.
What makes it infrastructure as code
Start with what the definition rules in and out, because the boundary is where most of the arguments happen.
Does writing scripts that configure servers count?
Yes, when the definitions live in source control and automation applies them. A resource API isn’t the test.
Morris has framed it that way since the first edition. His 2016 case for the practice opens on definition files for tools like Chef, Puppet and Ansible, checked into Git the way source code is, and calls the whole thing “using development practices and tools to manage infrastructure”. Two activities sit under that one name. Provisioning creates the thing. Configuration decides what’s inside it once it exists. Both count, and neither counts when it’s a person at a prompt making a change nobody can reproduce.
The public cloud is the obvious provisioning case and not the only one. A VMware installation on-premise gives you a control API to create and destroy virtual machines the same way a cloud does, and there are Terraform providers for it. Open the Terraform registry and the provider list covers pretty much anything you can name. Bare metal takes two steps instead of one: you PXE boot a server, it comes up with an IP, and your code goes to work on it from there. That works fine. Just keep the two steps apart in your head, because they break in different ways.
Pulumi muddies the boundary in a more interesting way. When you describe infrastructure in a general-purpose programming language, the infrastructure code can sit inside your product’s codebase rather than beside it. Docker looks like a candidate for a moment and then doesn’t hold up. It’s a packaging format, RPM 2.0, a way to state what your application needs at runtime. Packaging an application is a different activity from creating what it runs on.
Provisioning and configuration are two different jobs
That boundary is easier to see if you remember what the work looked like before cloud APIs existed.
Where do Puppet, Chef, Ansible and SaltStack fit now?
They manage what’s inside a machine. Creating the machine is a separate activity, though the line between the two has blurred.
These tools were built to run commands on machines rather than to call resource APIs. They either SSH to your servers and run commands described in some specification, or they run an agent on each server that calls home and gets commands to execute. Some can be extended with real code, Chef being written in Ruby and Ansible taking Python modules, and an extension like that may well call an API. Ansible went further than extension: its own AWS guide documents an ec2 module that provisions and de-provisions instances. So keep the two activities apart rather than the tools.
Public cloud changed the job by putting a creation and deletion API in front of everything, and the on-premise virtualisation vendors followed. Servers, databases, machine images, buckets, the lot. You can describe most of an estate in one codebase now, which is a different job from configuring a box after it boots. Both are legitimate. Only one of them got easier in the last ten years.
The snowflake server and the fear of touching it
The benefit that pays for all of this isn’t speed. It’s that changing things stops being frightening.
What does infrastructure as code actually reduce?
The effort and the risk of making a change, which is the benefit the book puts second and we’d put first.
The bill arrives at change time. You want to ship a feature that needs something adjusted on that machine, and the predictability of the change is already gone. You touch something you were sure was inert and the whole thing breaks. Now you’re debugging a configuration nobody designed, with no snapshot and no history.
A specification in Git gives you the history instead. You can read how the thing evolved, see who changed what and why, and go and ask them if they’re still around. You can review a change before it lands, the same way you review application code. And you can reconcile: terraform plan tells you how the specification compares with what’s actually in the cloud. If you haven’t touched the code and the plan still shows a diff, that diff is your configuration drift, and where it came from is the next question: somebody on the console, another automation, or a service that moved a value on its own. From there you can decide to bring reality back to the specification or to fold reality into it.
Self-service, and what it asks of a developer
Reducing the risk of a change makes the next benefit possible: letting other people make changes.
Should developers write their own infrastructure code?
They can write the specification and ship it through the same pipeline you use. The hard part isn’t the code, it’s knowing the platform underneath it.
The point of a platform is to stop being the thing in the way. When a developer needs a database, the answer is that it’s in code, you know code, add the specification to the repository. If it builds and the tests pass, it goes to production. We set up the pipeline for changing infrastructure; we don’t sit in the middle of each change as a gate. Help is there for anyone who wants it, and nobody waits on somebody else’s free afternoon.
What comes back is the harder question. Developers don’t tend to jump at the chance to define infrastructure, and when they do the output is usually weaker than what someone focused on infrastructure would produce. The interesting part is which bit is hard. This year we worked with two developers from a JavaScript background who were learning Terraform and AWS at the same time, and after two weeks they described it as painful and said they were lost. They’d picked up the Terraform language quickly. What they were missing was AWS: the quirks, the defaults, and the question of how traffic actually reaches your application and what it passes through on the way.
It doesn’t have to be AWS. Whatever your code runs on is the thing to understand, containers on a managed service or OpenShift on-premise. Writing a Kubernetes manifest means knowing something about how your application behaves in production, and that shift in who needs to know what is most of what DevOps turned out to mean. Some developers do hold that picture, and they’re usually the seniors, sometimes CTO level. Asking it of a mid-level developer alongside their own stack is asking a lot. That tension is exactly why big companies started building platform teams, and it’s why platform teams take up real space in the second edition of the book after being absent from the first.
Reliable, secure, cost-effective, and provable
Self-service only works if the resulting estate stays defensible, which is the next claim on the list.
How does code help when somebody audits you?
You show the code, then show a plan with no diff as evidence that the configuration you manage still matches it.
Reliability and security come from the same property: nobody is fiddling. What you deployed is what’s running. A lot of security failures start as a legitimate fix, somebody opens a port to get something working and the port stays open, and that whole category gets smaller when hand changes aren’t part of the workflow. The code describes what should be there and it’s about 99% accurate; there’s always a little configuration living somewhere else.
Compliance gets easier for the same reason. The control says you must have a disaster recovery site, you show the specification that creates it, and when they ask whether that’s really what’s running, you show a plan with no diff. Be precise about what that plan covers: the resources and attributes in that state, minus anything listed under ignore_changes, which is excluded from the comparison on purpose. A resource nobody imported is outside the evidence too. A clean plan says the managed configuration matches the code. Whether the site survives a failover is a test, and a diff cannot answer it.
Cost control works off the other half of the property: if you can recreate a system, you can delete it. Tag every resource created through infrastructure as code, and the untagged remainder becomes a list to work through. Some of it was created by hand, some by another tool, and some belongs to a service where tags aren’t supported in the first place. Find the owner and check what depends on a resource before it goes. Production is slower to clean up, because availability requirements limit what you can take away to find out.
Troubleshooting gets the same first step. Check for drift before anything else: if the plan shows something changed outside the code, there’s a very good chance that’s your incident. A clean plan doesn’t clear the environment, it closes one line of enquiry and you keep digging. Kubernetes runs that reconciliation continuously inside the cluster: delete a Pod a ReplicaSet owns and the controller replaces it, because the ReplicaSet exists to keep a stable set of replica Pods running.
Keep the state small and apply continuously
The book turns these benefits into three practices: define everything as code, continuously test and deliver all work in progress, and build small, simple pieces you can change independently. The first one is the name of the thing. The other two are where teams get it wrong.
How big should a Terraform state get?
Small enough that a plan stays quick, which starts to hurt somewhere around a hundred resources.
Refreshing state means pulling every resource it covers to see how it’s feeling, so one enormous specification gets slow exactly when you want fast feedback. Split it by who’s allowed to change what: IAM and security in one state, networking in another, databases in a third, DNS in a fourth. The people with the access to change each piece are the ones who hold it, and everyone else looks up what they need and consumes it. Modules are the other half of the same idea, libraries you can version and change independently instead of editing one monolith.
Then test and deliver continuously, the way you would with application code. A change set that sits and grows is a change set whose failure you won’t be able to attribute. This is how change management worked in the nineties: collect a pile of tickets, apply them all on one weekend, spend the following month working out what broke. The modern version of the same mistake is subtler. You’re on the server, the fix is one line, it works, and now you owe the code a change you’ll make later. Later is when drift starts. So the path back through the code has to be fast, because if the right way is slow people take the quick way, and the quick way is a line typed on a server.
What it does to the four key metrics
The book closes its first chapter by tying all of this to the four key metrics from the DORA research, which is a fair test of whether the argument holds.
Which of the four metrics does infrastructure as code move first?
Lead time for changes, because the resources a change needs stop being a queue.
Delivery lead time drops when a team can provision what they need without waiting. Deployment frequency rises when applying a change doesn’t need a change window on a Saturday. Change failure rate falls for the reason above: one change at a time means a failed test names its own culprit, instead of a triage meeting picking through a month of accumulated work. Time to restore service improves because the drift check is quick, and because terraform plan, pulumi preview --refresh and CloudFormation’s drift detection all answer the same question from different angles.
Two caveats. DORA was an independent research organisation before it was acquired by Google and joined Google Cloud in December 2018, so read a report that recommends faster delivery knowing who publishes it. And the four aren’t the whole set: the 2021 Accelerate State of DevOps report, published in September, pairs them with a fifth measure of operational performance, reliability, which the team expanded from availability so that latency, performance and scalability count too.
There’s a related argument we didn’t settle. Mattias reads the speed benefit as a pipeline property: a quick pipeline delivers your application and your infrastructure, and if you need an S3 bucket you have it now. Andrey says the pipeline isn’t what gives you that, the API is, and continuous delivery is what you build on top once the infrastructure is described. That order matches how our consulting work tends to go: infrastructure as code first, then continuous integration and delivery on top of it, for application code and infrastructure both.
The API underneath changed in September
Which brings us back to the release that started all this.
Why did your provider take months to support a new AWS service?
Because every AWS service family had its own API shape, so somebody had to hand-write provider code for each one.
Anyone who has used the AWS CLI has met this. You describe EC2 instances, you list S3 buckets, and the output of one barely resembles the other: list ECS tasks and you get a bare list of ARNs, describe instances and you get a large JSON document full of detail. Every discrepancy is work for the people maintaining the Terraform AWS provider, which is why support for a new service arrived weeks or months after the service did. Pulumi inherited the same lag, since its classic AWS provider is bridged from the Terraform one.
Google hasn’t had this problem to the same degree, because its APIs are described in machine-readable form and the Terraform provider is generated from those descriptions, so a new resource can appear in the provider close to the day it launches. AWS Cloud Control API, generally available on 30 September, is AWS taking the same route: one set of CRUD operations over supported resource types from the CloudFormation registry, with consistent inputs, return types and error messages.
Both vendors shipped on the same day. HashiCorp released the AWS Cloud Control provider, awscc, as a tech preview, generated from the unified schema and meant to run alongside the existing aws provider rather than replace it. Pulumi released AWS Native on the same basis. Both are previews, and being in the CloudFormation registry is not the same as being reachable. AWS documents how to determine whether a resource type supports Cloud Control API: legacy types classified as non-provisionable don’t, and describe-type tells you which side a given type falls on. Then the type still has to be in the provider version you’ve pinned. The mature aws provider is where most of your configuration will live for a while yet.
What this means for teams
None of this is new theory. The second edition of the book mostly confirms what practitioners worked out the hard way, and what’s actually changed in two years is that the tooling arrives faster and the organisational pattern now has a name. The useful question is which of these you can test this week.
- Run a plan against production in your biggest state and read the diff as a report, not a chore. Every line in it is something to explain: a hand change, another process, or code that never matched reality.
- Pick the one machine nobody wants to touch and write down why. Either get it into code, or write the rebuild plan you’d follow when it finally breaks.
- Tag everything created through infrastructure as code, then list what isn’t tagged. Work that list by owner before you delete anything on it.
- Take your slowest state and split it along access boundaries, starting with IAM and networking.
Common questions, answered
How do I find out what changed in production outside my code?
Run your tool’s reconciliation against production and read the diff. terraform plan on a specification you haven’t edited shows what no longer matches the code. Use pulumi preview --refresh for the same job: plain pulumi preview compares against the state Pulumi recorded last time, not the live resources. CloudFormation has had drift detection since November 2018.
A diff says what differs, not who caused it: a console change, another automation, or an attribute the code leaves alone through ignore_changes. Investigate each line, and run the check on a schedule: a difference found on a Tuesday is cheaper than one found mid-incident.
How do I use an AWS service my Terraform provider doesn’t support yet?
Check whether the resource type supports Cloud Control API, then add the awscc provider next to the aws provider you already use and declare that one resource through it. Presence in the CloudFormation registry isn’t enough. AWS documents how to determine support per type, legacy non-provisionable types are excluded, and the type has to be in the provider version you’ve pinned.
awscc is a tech preview, so keep it to the resources that need it and leave everything else on the aws provider. When a type isn’t supported, you’re back to waiting for the aws provider to catch up.
How do I clean up cloud resources nobody owns?
Tag every resource created through infrastructure as code, then work the untagged remainder as a list to investigate before anything is deleted. A missing tag can mean a hand-made resource, another tool, a service that doesn’t support tags, or a gap in your own configuration.
Work through non-production first, where you can delete and recreate to prove the specification is complete. In production, availability requirements make this slower, so use the list to start the conversation with whoever owns the workload.
Related episodes
- Episode 1, Infrastructure as Code is the conversation this one revisits: when to start, which tools, and what we thought in February 2020.
- Episode 9, Terraform in CI covers the pipeline side of continuous apply, including what it means to hand credentials for the whole kingdom to an automated process.
- Episode 10, Are We Wrong About Terragrunt? has Anton Babenko on splitting environments and modules, which is the practical version of keeping states small.
- Episode 19, Deleting Resources in the Cloud is the cleanup argument in full, including what to do with a resource that exists but isn’t in the code.
- Episode 25, All the Things You Wanted to Know About Pulumi has Paul Stack on describing infrastructure in a general-purpose language and where that boundary sits.
Resources
- Infrastructure as Code: Dynamic Systems for the Cloud Age, 2nd edition, Kief Morris, December 2020, settles the definition, the benefit list and the three core practices this article works through.
- Why Infrastructure as Code, Kief Morris, 19 May 2016, settles that the practice covers definition files for tools like Chef, Puppet and Ansible, not only cloud provisioning.
- Announcing AWS Cloud Control API, 30 September 2021, settles what the API does: one CRUD interface over supported resource types, with consistent inputs and error messages.
- Using Cloud Control API resource types settles how to check whether a given resource type is reachable through the API, and that legacy non-provisionable types are not.
- Announcing Terraform AWS Cloud Control Provider Tech Preview, 30 September 2021, settles that
awsccis auto-generated, is a preview, and is designed to sit alongside the existing AWS provider. - Announcing the Pulumi AWS Native Provider, 30 September 2021, settles Pulumi’s equivalent release and its claim of same-day support for new AWS resources.
- Accelerate State of DevOps 2021, September 2021, settles the four metrics as DORA words them and the fifth, reliability, which the team expanded from its earlier availability measure.
- DORA joins Google Cloud, 20 December 2018, settles who owns the research programme behind those metrics.
Since we recorded
The preview providers took their time. HashiCorp and AWS announced that the awscc provider had reached general availability on 30 May 2024, with more than 950 resources and data sources, so the day-one promise took two and a half years to come out of preview. The bigger change wasn’t technical: HashiCorp moved Terraform to the Business Source License in August 2023, which produced the fork we covered in episode 54 and the migrations we went through in episode 64. The platform team argument here got a whole episode of its own, with a practitioner, in episode 38.
