In this post, we will learn in detail what is terraform desired and current state.
Terraform’s responsibility is to create/update/destroy infrastructure resources to match the desired state as described in the configuration.
Desired State:
For example: If our desired state is as below
resource "aws_instance" "myec2" {
ami = "ami-0ca285d4c2cda3300"
instance_type = "t2.medium"
}
This should result in an AWS EC2 t2.medium instance.
The code you saw above is the desired state that we want.
Current State:
The current state is the actual state of a resource that is deployed.
For example: If our desired state is as below
resource "aws_instance" "myec2" {
ami = "ami-0ca285d4c2cda3300"
instance_type = "t2.medium"
}
our desired state is t2.medium instance but let’s say the current instance running is t2.micro. So it means our desired state and the current state is not matching.
Try it out
Let’s first deploy a t2.medium ec2 instance using the below code.
provider "aws" {
region = "us-west-2"
access_key = "<access_key>"
secret_key = "<secret_key>"
}resource "aws_instance" "myec2" {
ami = "ami-0ca285d4c2cda3300"
instance_type = "t2.micro"
}
Learn how to deploy from the link: Terraform — 101
In this post, we will learn in detail what is terraform desired and current state.
Terraform’s responsibility is to create/update/destroy infrastructure resources to match the desired state as described in the configuration.
Desired State:
For example: If our desired state is as below
resource "aws_instance" "myec2" {
ami = "ami-0ca285d4c2cda3300"
instance_type = "t2.medium"
}
This should result in an AWS EC2 t2.medium instance.
The code you saw above is the desired state that we want.
Current State:
The current state is the actual state of a resource that is deployed.
For example: If our desired state is as below
resource "aws_instance" "myec2" {
ami = "ami-0ca285d4c2cda3300"
instance_type = "t2.medium"
}
our desired state is t2.medium instance but let’s say the current instance running is t2.micro. So it means our desired state and the current state is not matching.
Try it out
Let’s first deploy a t2.medium ec2 instance using the below code.
provider "aws" {
region = "us-west-2"
access_key = "<access_key>"
secret_key = "<secret_key>"
}resource "aws_instance" "myec2" {
ami = "ami-0ca285d4c2cda3300"
instance_type = "t2.micro"
}
Learn how to deploy from the link: Terraform — 101
This will deploy a AWS EC2 t2.micro instance.
Now let’s modify the instance_type to t2.medium
provider "aws" {
region = "us-west-2"
access_key = "<access_key>"
secret_key = "<secret_key>"
}resource "aws_instance" "myec2" {
ami = "ami-0ca285d4c2cda3300"
instance_type = "t2.medium"
}
run the terraform plan
Terraform will detect the changes between the desired state (t2.medium) and the current state (t2.micro)
# aws_instance.myec2 will be updated in-place
~ resource "aws_instance" "myec2" {
id = "i-0a84f30f5656d7800"
~ instance_type = "t2.micro" -> "t2.medium"
tags = {
"Name" = "terraform"
}
# (28 unchanged attributes hidden)# (6 unchanged blocks hidden)
}Plan: 0 to add, 1 to change, 0 to destroy.
As we can see terraform has detected the change.
Remember, terraform tries to ensure that the deployed infra is based on the desired state. If there is a difference between the two, terraform plan will show the necessary changes required to achieve the desired state.
Let’s do the terraform apply
This will update the instance type to t2.micro
Hope this clarifies what the desired state and current state is and how the terraform handles the difference between them.
Now let’s modify the instance_type to t2.medium
provider "aws" {
region = "us-west-2"
access_key = "<access_key>"
secret_key = "<secret_key>"
}resource "aws_instance" "myec2" {
ami = "ami-0ca285d4c2cda3300"
instance_type = "t2.medium"
}
run the terraform plan
Terraform will detect the changes between the desired state (t2.medium) and the current state (t2.micro)
# aws_instance.myec2 will be updated in-place
~ resource "aws_instance" "myec2" {
id = "i-0a84f30f5656d7800"
~ instance_type = "t2.micro" -> "t2.medium"
tags = {
"Name" = "terraform"
}
# (28 unchanged attributes hidden)# (6 unchanged blocks hidden)
}Plan: 0 to add, 1 to change, 0 to destroy.
As we can see terraform has detected the change.
Remember, terraform tries to ensure that the deployed infra is based on the desired state. If there is a difference between the two, terraform plan will show the necessary changes required to achieve the desired state.
Let’s do the terraform apply
This will update the instance type to t2.micro
Hope this clarifies what the desired state and current state is and how the terraform handles the difference between them.
Comments
Post a Comment