0
0
Terraformcloud~5 mins

Immutable infrastructure concept in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Immutable infrastructure means you never change servers or resources after they are created. Instead, you replace them with new ones when you need updates. This approach helps avoid unexpected problems and keeps your system stable.
When you want to update your application without downtime by replacing old servers with new ones.
When you want to avoid configuration drift caused by manual changes on servers.
When you want to roll back easily by switching to a previous version of infrastructure.
When you want to improve security by deploying fresh servers with updated patches.
When you want to automate infrastructure updates safely and predictably.
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "app_server" {
  ami           = "ami-0c94855ba95c71c99"
  instance_type = "t2.micro"

  tags = {
    Name = "immutable-app-server"
  }
}

This Terraform file defines a simple AWS EC2 instance.

The provider block sets the AWS region.

The aws_instance resource creates a new server with a specific Amazon Machine Image (AMI) and instance type.

When you want to update this server, you change the AMI or instance type and apply again, which replaces the old server with a new one instead of modifying it.

Commands
This command initializes the Terraform working directory. It downloads the AWS provider plugin needed to create resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command shows what Terraform will do before making any changes. It helps you verify that the new server will be created as expected.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.app_server will be created + resource "aws_instance" "app_server" { + ami = "ami-0c94855ba95c71c99" + instance_type = "t2.micro" + tags = { + "Name" = "immutable-app-server" } } Plan: 1 to add, 0 to change, 0 to destroy.
This command creates the new server as defined in the configuration. The -auto-approve flag skips manual confirmation to speed up deployment.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.app_server: Creating... aws_instance.app_server: Still creating... [10s elapsed] aws_instance.app_server: Creation complete after 15s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply action without prompting
This command removes the server created by Terraform. It is used to clean up resources when they are no longer needed.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
aws_instance.app_server: Refreshing state... [id=i-0abcd1234efgh5678] An execution plan has been generated and is shown below. Terraform will perform the following actions: # aws_instance.app_server will be destroyed - resource "aws_instance" "app_server" { - ami = "ami-0c94855ba95c71c99" -> null - instance_type = "t2.micro" -> null - tags = { - "Name" = "immutable-app-server" } -> null } Plan: 0 to add, 0 to change, 1 to destroy. aws_instance.app_server: Destroying... [id=i-0abcd1234efgh5678] aws_instance.app_server: Destruction complete after 10s Destroy complete! Resources: 1 destroyed.
-auto-approve - Automatically approve the destroy action without prompting
Key Concept

If you remember nothing else from this pattern, remember: never change servers in place, always replace them with new ones to keep infrastructure stable and predictable.

Common Mistakes
Manually logging into servers to change configuration instead of updating Terraform code.
This causes configuration drift and breaks the immutable infrastructure principle.
Make all changes in Terraform code and redeploy to replace servers cleanly.
Modifying Terraform resources in place without replacing them when updates are needed.
This can cause unexpected side effects and unstable infrastructure.
Change resource parameters that force replacement, then apply to create new resources.
Summary
Initialize Terraform to prepare the working directory and download providers.
Use terraform plan to preview changes before applying them.
Apply changes with terraform apply to create new immutable servers.
Destroy resources with terraform destroy when cleaning up.