There is the depends_on
attribute that allows explicit dependencies to be setup and creating things in order. It is limited for your scenario because it doesn't wait for the new instances to be "ready", just created.
One idea I had when reading your scenario, you could use the extermal
data source. Not positive it was intended for this kind of thing but I think it could work. Essentially you could write a script that would use the AWS CLI and whatever else is needed to see if the instance is created and ready. If you use the with depends_on
or you chain the output of the external
data source to the next instance (use the output to set a tag?), I think it would have the effect you want.
This design smells a bit to me though. There are other AWS services and features that can do this kind of thing for you e.g. ECS rolling deployments with load balancer health checks.
Resources:
https://www.terraform.io/docs/providers/external/data_source.html
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-create-loadbalancer-rolling.html
Edit:
If you are stuck with EC2 instances another native AWS way to solve this might be to use lifecycle hooks. I have used EC2 user data scripts in combination with lifecycle hooks calling Lambda functions to do rolling deployments and configuration of a custom Kafka cluster (before MSK). This required that I bring up instances in order and assigning each instance a unique broker id. Sounds similar to your scenario.
Resource:
https://docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks.html
aws_instance
resource) or are you using an autoscaling group? This is easier to achieve with an ASG even if you don't need to be able to actually autoscale (set min and max to the same and/or no autoscaling policy). – ydaetskcoR