In this lesson, we dive into the fundamental differences between mutable and immutable infrastructure. Understanding these differences is essential when implementing Infrastructure as Code (IaC) and using tools like Terraform. Terraform handles resource updates—such as modifying the permissions of a local file—by destroying an existing resource and then re-creating it with the updated settings. Consider the following example:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Mutable Infrastructure in Practice
Imagine you are running an application server with Nginx version 1.17. When a new version is released, you might choose to update the web server incrementally—from version 1.17 to 1.18, and later from 1.18 to 1.19. The upgrade process often involves either:- Manually downloading and installing the new version during a maintenance window.
- Automating the process with configuration management tools like Ansible.

An in-place update may lead to configuration drift over time. If one server fails to upgrade due to dependency issues, it will remain on an older version while others move forward, complicating future maintenance.
Challenges with Mutable Infrastructure
When updating software on a system:- Dependencies must be met for a successful upgrade.
- If one or more servers (e.g., web server 3) have unmet dependencies—such as network issues, storage constraints, or compatibility differences—the upgrade might fail, leaving that server on an older version (e.g., version 1.18).
- After multiple update cycles, configuration drift can occur, where servers run different versions of software, increasing the complexity of troubleshooting and further updates.
Embracing Immutable Infrastructure
Immutable infrastructure takes a different approach. Instead of modifying existing servers, you provision new servers with the updated version (for example, replacing Nginx 1.17 with 1.18). Once the new servers are confirmed to run correctly, the old servers are decommissioned. In immutable infrastructure, resources are never modified post-deployment—they are always replaced. This method minimizes the risk of configuration drift because the previous version remains unchanged until a fully validated new version is deployed.
Benefits of Immutable Infrastructure
- Easier to version infrastructure and roll back to previous releases.
- Consistent environments reduce the complexity of governance and maintenance.
- Reliable deployment pipelines, as each release is a fresh creation rather than an update.
Terraform and Immutable Infrastructure
Terraform exemplifies immutable infrastructure by default. For instance, if you update a resource block—changing the permission from “0777” to “0700”—Terraform destroys the original resource and creates a new one with the updated settings. Revisiting our earlier example:Altering default resource creation and deletion strategies without a thorough understanding of lifecycle rules can lead to unexpected outages. Always test changes in a staging environment before applying them in production.