Terraform Can Only Be Tested End To End
Terraform allows you to define your infrastructure configuration declaratively, like this:
resource "aws_instance" "web" {
ami = "ami-0abcdef1234567890"
instance_type = "t3.micro"
associate_public_ip_address = true
}
resource "aws_security_group" "allow_web" {
name = "allow-web"
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["${aws_instance.web.public_ip}/32"]
}
}
You’ll notice a few things about that example above:
aws_security_group.allow_webdepends onaws_instance.web- The security group’s
cidr_blocksinput depends on the instance’spublic_ipoutput - However, it’s not a direct relationship, because the expression
"${aws_instance.web.public_ip}/32"transforms the output of the instance before passing it into the security group.
How does this all work? How does Terraform make sure the proper value gets passed into the security group?
In order to implement all of this cleanly, the Terraform authors made a pragmatic choice. Terraform fully deploys the first resource, applies any transformations that depend on that resource’s outputs, and then passes those into any dependent resources.
You might already be able to see the problem here. Your user defined expressions and transformations are now interleaved with provider calls. This means your expressions are impossible to test without the entire end to end integration all the way through the cloud provider’s deployment.
To go back to our example above, let’s see what happens when we actually try to deploy this configuration:
- Terraform reads your configuration for
aws_instance.web. - Terraform orchestrates the AWS communication to deploy the instance.
- AWS deploys the instance and returns the result, including
public_ip = "203.0.113.42". - Terraform saves all the new values into its state.
- Terraform evaluates
"${aws_instance.web.public_ip}/32"with that concrete value, producing"203.0.113.42/32". - Terraform reads your configuration for
aws_security_group.allow_web. - Terraform orchestrates the AWS communication to deploy the security group
with
cidr_blocks = ["203.0.113.42/32"]. - AWS configures the security group and returns the result.
- Terraform saves all the new values into its state.
This is a lot happening, and the code you wrote is evaluated in step 5. What
if you had a bug in your code? What if you wrote
"${aws_instance.web.public_ip}/322" instead? Because your expression only
gets evaluated in the context of this long chain, that means you cannot even
run your code without actually deploying real cloud resources!
Workarounds for this problem exist, like cloud provider simulators, but having
to simulate an entire cloud provider just to test your deployment logic is not
an ideal situation. terraform plan doesn’t help either, because it just
tells you (known after apply), perfectly illustrating this end to end issue.
So how do we get out of this situation? The key is to notice that we have two separate pieces:
- Deployment intent (our code)
- Provider orchestration (Terraform’s code)
We really want to test (1), and testing (2) should not be something we need to worry about.
It turns out that all you need is to disallow arbitrary client side transformations, and your declaration of intent becomes purely declarative once again. If you can’t do anything differently based on the result of deployment, the whole flow is untangled and becomes a one way hand-off to the deployment engine.
The real question is, how can you preserve the power that you get from these transformations, while still gaining this nice property? I believe this is possible, but that’s the topic of future posts.