What’s New in OpenTofu 1.9.0?
Last week from this writing, some features came to OpenTofu, and I’m excited to start using that.
At the time of this writing, the version 1.9.0 is still using rc tag, which is not recommended for production.
However, we can still try and see these new exciting features in action and wait for them in the future stable version
The Exclude flag
This flag is especially useful, because sometimes, during debugging or even some maintenance, you have to exclude one resource or another from the plan/apply lifecycle.
In the past, you’ve had to run — target= flag and plan all the targets you wanted, just because one or 2 resources could not be touched.
Now, with the exclude option, you can plan all your resources and exclude only the ones you don’t want to plan/apply.
As an example, here are my 2 resources I’m planning:
But let’s say I don’t want to run any change to my null_resource, because it’s important and might break something in production.
Let’s exclude it from my plan:
Now I can easily plan anything else besides the one I excluded.
I really liked this feature!
The for_each in the Providers Block
Let’s suppose you want to create 3 ec2 machines in 3 different regions for high availability.
In the past, you would need something like this:
provider "aws" {
# Default region for general use
region = "us-east-1"
}
provider "aws" {
alias = "region2"
region = "us-west-1"
}
eu-west-1 =
provider "aws" {
alias = "region3"
region = "eu-west-1"
}
resource "aws_instance" "region1" {
#first one will use the default region
[...]
}
resource "aws_instance" "region2" {
provider = aws.region2
[...]
}
resource "aws_instance" "region3" {
provider = aws.region3
[...]
}
As you can see, it limits our potential. Now, we have the option to use for_each loop in the providers block, so say, for example:
variable "aws_regions" {
default = {us-east-1 = "", us-west-1 = "", eu-west-1 = ""}
}
provider "aws" {
region = "us-east-1"
}
provider "aws" {
alias = "by_region"
for_each = var.aws_regions
region = each.key
}
resource "aws_instance" "example" {
for_each = var.aws_regions
provider = aws.by_region[each.key]
[...]
}
Now, you can see that we were able to plan all of them with a single resource block:
tofu plan
Each of them in their own regions.
This is definitely an improvement!
Find out more here if you want to test it on your side:
I hope to see these features in stable releases soon!
Follow me on Linkedin