Hello and thank you for terraform,
Using terraform, is there a way to daisy-chain depends_on
for resources created using count
or for_each
? So inject a dependency into the resource graph?
This is for some cluster stacks where the goal is to have only one AWS EC2 instance being modified at a time (no ASG, AWS CF or containers).
I can accomplish this using static variables, see the example below, but I think because depends_on
requires …
“A single static variable reference is required: only attribute access and
indexing with constant keys. No calculations, function calls, template
expressions, etc are allowed here.”
https://www.terraform.io/docs/configuration/resources.html#depends_on-explicit-resource-dependencies
I have not been able to daisy-chain depends_on
for resources created using count
or for_each
.
Is there a way to do this using terraform? If not, then are there any plans to add support for doing this?
Or should a terraform generator (such as python-terrascript) be used to automate writing the terraform file?
I do not think terraform will be able to generate terraform DSL and then consume it within the same plan/apply, and I would want to avoid getting terraform to invoke another terraform process.
> cat daisychain.tf
provider "aws" {
profile = "default"
region = "us-east-1"
}
locals {
ami = ""
# instance_type = "t2.small"
instance_type = "t3.small"
}
resource "aws_instance" "node-01" {
ami = local.ami
instance_type = local.instance_type
}
resource "aws_instance" "node-02" {
ami = local.ami
instance_type = local.instance_type
depends_on = [ aws_instance.node-01 ]
}
resource "aws_instance" "node-03" {
ami = local.ami
instance_type = local.instance_type
depends_on = [ aws_instance.node-02 ]
}
So that when the instance_type is changed:
~ instance_type = "t2.small" -> "t3.small"
each EC2 instance is changed sequentially, one instance at a time:
aws_instance.node-01: Modifying... [id=i-...69]
aws_instance.node-01: Still modifying... [id=i-...69, 10s elapsed]
aws_instance.node-01: Still modifying... [id=i-...69, 20s elapsed]
aws_instance.node-01: Still modifying... [id=i-...69, 30s elapsed]
aws_instance.node-01: Still modifying... [id=i-...69, 40s elapsed]
aws_instance.node-01: Modifications complete after 42s [id=i-...69]
aws_instance.node-02: Modifying... [id=i-...45]
aws_instance.node-02: Still modifying... [id=i-...45, 10s elapsed]
aws_instance.node-02: Still modifying... [id=i-...45, 20s elapsed]
aws_instance.node-02: Still modifying... [id=i-...45, 30s elapsed]
aws_instance.node-02: Still modifying... [id=i-...45, 41s elapsed]
aws_instance.node-02: Modifications complete after 43s [id=i-...45]
aws_instance.node-03: Modifying... [id=i-...ff]
aws_instance.node-03: Still modifying... [id=i-...ff, 10s elapsed]
aws_instance.node-03: Still modifying... [id=i-...ff, 20s elapsed]
aws_instance.node-03: Still modifying... [id=i-...ff, 30s elapsed]
aws_instance.node-03: Still modifying... [id=i-...ff, 40s elapsed]
aws_instance.node-03: Modifications complete after 42s [id=i-...ff]
Apply complete! Resources: 0 added, 3 changed, 0 destroyed.
Thanks