hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.15k stars 9.47k forks source link

Feature request: Delay resource #19243

Closed arvindsree closed 4 years ago

arvindsree commented 5 years ago

Hi there,

AWS and lots of other modules have asynchronous resources that are not actually completely created when the API returns. This causes dependancy issues during creation. Example: https://stackoverflow.com/questions/36802681/terraform-having-timing-issues-launching-ec2-instance-with-instance-profile

We need an OS independent solution for this

Something like

resource "delay_resource" "delay1" { create_delay = 10.0 destroy_delay = 2.0 input = "${resource_pre.id}" } It should have an output variable that can be passed to the next resource

Related issue https://github.com/hashicorp/terraform/issues/17726

It could also be a provisioner with a delay argument

Null resource work around with the local-exec provisioner is not OS independent.

Terraform version : 0.11.8

BrendanThompson commented 5 years ago

This is something I am encountering with MongoDB Atlas VPC Peering, wherein a Container is created prior to the Peer ( and, there is a requirement to do so ) however two fields on the Container are not available until the Peer is provisioned. The delay and refresh of the Container object thus allowing the use of those two extra fields in another resource type.


akshaykarle/terraform-provider-mongodbatlas

shadycuz commented 5 years ago

This would also be useful for creating new aws users or roles and then using that ARN for a Principal in an AWS policy. Usually it fails because the internals of AWS hasn't synced as fast as terraform.

ryber commented 4 years ago

I'd like to use this with the Okta plugin because we run into Okta rate-limits. If I can just put a 10 second pause between applications it would slow us down enough to not hit the limits.

https://github.com/articulate/terraform-provider-okta/issues/182

tksrc commented 4 years ago

This would be really useful when creating new AWS accounts. Any resoure that terraform creates immediately after the account resource has been created fail with:

Error: SubscriptionRequiredException: The AWS Access Key Id needs a subscription for the service status code: 400, request id: c87bc8e6-bdd8-4dd7-bead-298eb6d5c719

Retrying in a minute later works fine.

mcalnd70 commented 4 years ago

I have this when creating an Azure SQL Server, and then try and create an Elastic Pool

Error: Code="OperationInterrupted" Message="The operation on the resource could not be completed because it was interrupted by another operation on the same resource."

on main.tf line 43, in resource "azurerm_sql_elasticpool" "go-1": 43: resource "azurerm_sql_elasticpool" "go-1" {

Re-run it again, works fine.

The message "The operation on the resource could not be completed because it was interrupted by another operation on the same resource." when you google around is almost just around Azure SQL, so potentially not a Terraform bug. But it would be good to be able to delay resource creation in this regard.

tksrc commented 4 years ago

I have this when creating an Azure SQL Server, and then try and create an Elastic Pool

Error: Code="OperationInterrupted" Message="The operation on the resource could not be completed because it was interrupted by another operation on the same resource."

on main.tf line 43, in resource "azurerm_sql_elasticpool" "go-1": 43: resource "azurerm_sql_elasticpool" "go-1" {

Re-run it again, works fine.

The message "The operation on the resource could not be completed because it was interrupted by another operation on the same resource." when you google around is almost just around Azure SQL, so potentially not a Terraform bug. But it would be good to be able to delay resource creation in this regard.

I use this hack to go around this: Please note you can use anything else (e.g. sleep 120) if you are on a *nix based environment. I use pwsh (powershell core).

Create AWS Account

resource "aws_organizations_account" "this" { name = var.account_name email = var.account_email }

Delay hack. It gives AWS enough time to activate the account before proceeding with account resource creation below.

resource "null_resource" "before" { depends_on = [aws_organizations_account.this] }

resource "null_resource" "delay" { provisioner "local-exec" { command = "pwsh -c Start-Sleep -Seconds 120" } triggers = { "before" = "${null_resource.before.id}" } }

Other resources that can be safely deployed after the delay.

pkolyvas commented 4 years ago

Good news!

We just released the time_sleep resource in the time provider.

https://www.terraform.io/docs/providers/time/r/sleep.html

This gives you a straightforward, cross-platform sleep option. This resource should, almost exclusively, be considered a workaround for issues that we hope would be reported and handled in Terraform Provider logic.

Downstream resources can usually introduce or adjust retries in their code to handle time delay issues for all Terraform configurations. Upstream resources can be improved to better wait for a resource to be fully ready and available.

That said, I believe this addresses the need documented in this issue and I'm going to close it.

Heiko-san commented 4 years ago

Good news!

We just released the time_sleep resource in the time provider.

Great !

And if it was documented at https://www.terraform.io/docs/providers/index.html, I actually could have saved the time for writing my own. ;)

pkolyvas commented 4 years ago

@Heiko-san thanks for the catch - we forgot to cherry pick all of the website changes into our stable website; the provider docs were orphaned here: https://www.terraform.io/docs/providers/time/index.html

They'll be in our Misc. Providers list shortly: https://www.terraform.io/docs/providers/type/misc-index.html (within a few hours of this post.)

ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.