I create several openstack_compute_instance_v2 resources,
how to add some timeout between creation of the resource and the next one?
So, after terraform aply, my resources will be created and some timeout will by waiting between creation of them
I don’t see any timeout options for the openstack_compute_instance_v2
in their documentation, but I don’t think I understand what it is you’re asking about with timeouts between resources. Can you give an example of what you’re trying to do?
I need to add some timeout bewteen resource instance creation.
My resource looks like
resource "openstack_compute_instance_v2" "master" {
for_each = var.vm_instances
name = each.key
....
}
Then create isnatnces like:
module "create_vms" {
source = "../modules/compute_vs"
vm_instances = {
isntance1 = {...}
instance2={...}
...
I finally found a way to do it. Need to insert in resource:
provisioner "local-exec" {
command = "sleep 120"
}
Now, it seems, terraform waits 2 mins before creating the next isntance
It sounds like you want to insert a delay between each instance, but when you use for_each
there is no ordering of the instances, and they can even be created concurrently.
It’s generally not a good idea to rely on arbitrary delays to ensure resource deployment is correct, but if you need to workaround a problem there is also the time_sleep
resource which can create a delay between dependencies with a little more control than the provisioner. You would have to write out individual resource blocks however to ensure there is a specified dependency order between the resources, but that would be the case using the local-exec
provisioner as well.
well, i create compute instance resource with our company’s custom image. And this image has flaws causing machine creation failure if i create concurrently too many instances at once.
So i need pause between them)
Should i rewrite my resource definition to smth like this?
resource "time_sleep" "wait_2_minutes" {
create_duration = "2m"
}
resource "openstack_compute_instance_v2" "master" {
for_each = var.vm_instances
name = each.key
....
depends_on = [time_sleep.wait_2_minutes]
}
Using a provisioner like you did might delay some instances from being created; for example you might get a batch of 10, then a 2 min delay, then another batch of 10, etc. If concurrency itself is the problem, then you can use the -parallelism
flag to reduce the concurrency in the CLI, but that unfortunately affects everything for now.
Your latest example doesn’t delay anything other than the start of evaluating the entire set of openstack_compute_instance_v2
instances. The only way to declare a specific delay between every instance would be to write each openstack_compute_instance_v2
instance in it’s own block with a separate time_sleep
between them.
The optimal solution of course would be to fix this at the image level to prevent the failure in the first place. Terraform is going to expect that once the provider reports the apply action is complete, it can move on to the next waiting resource, and adding a delay is only guessing at what else might be happening behind the scenes.
Hello! Thanks for the assitance . I also added -parallelism=1 and now it works as I dreamed. Declaring explicitly resource block for each instance would be tedious as i have 38 instances to create