Learn how to set a timeout for your Terraform apply commands to prevent runaway processes and ensure predictable infrastructure deployments.
Terraform, a powerful infrastructure-as-code tool, doesn't offer a universal timeout setting for the terraform apply command. However, managing timeouts effectively is crucial for robust infrastructure provisioning. This article outlines various methods to control timeouts in your Terraform workflows.
Terraform itself doesn't have a global timeout setting for terraform apply. However, timeouts can be managed in a few ways:
Resource-Specific Timeouts: Some resources have built-in timeout options. For example:
resource "google_compute_region_instance_group_manager" "grp" {
# ... other settings
timeouts {
create = "30m"
update = "20m"
delete = "10m"
}
}Terraform Enterprise/Cloud: These platforms offer global timeout settings for runs. Check your organization or workspace settings.
External Tools: When using CI/CD or scripting, you can wrap terraform apply within timeout mechanisms provided by the tool itself.
State Lock Timeout (CLI): The -lock-timeout flag for terraform apply controls how long Terraform waits to acquire a state lock:
terraform apply -lock-timeout=15mImportant Considerations:
This code provides examples of how to manage timeouts in Terraform using resource-specific timeouts for creation, update, and delete operations, as well as using external tools like the Bash timeout command to limit the duration of Terraform commands. It also shows how to set a state lock timeout using the Terraform CLI. The code emphasizes the importance of investigating the root cause of timeouts, considering resource dependencies, and testing timeout settings.
This document provides code examples for managing timeouts in Terraform, as discussed in the article.
1. Resource-Specific Timeouts:
# Example using Google Cloud Platform resource
resource "google_compute_instance" "default" {
name = "example-instance"
machine_type = "n1-standard-1"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
# Set timeouts for create, update, and delete operations
timeouts {
create = "10m" # Allow 10 minutes for instance creation
update = "5m" # Allow 5 minutes for instance updates
delete = "2m" # Allow 2 minutes for instance deletion
}
}
# Example using AWS resource
resource "aws_instance" "web" {
ami = "ami-0c55b159c9e67027c"
instance_type = "t2.micro"
# Set timeouts for create and delete operations
timeouts {
create = "5m"
delete = "10m"
}
}3. External Tools (Example with Bash and timeout command):
#!/bin/bash
# Set a timeout for the entire Terraform apply process (e.g., 20 minutes)
timeout 20m terraform apply
# Check the exit code of the 'timeout' command
if [[ $? -eq 124 ]]; then
echo "Error: Terraform apply timed out after 20 minutes."
exit 1
fi
echo "Terraform apply completed successfully."4. State Lock Timeout (CLI):
# Set a 15-minute timeout for acquiring the state lock
terraform apply -lock-timeout=15m Important Considerations:
Remember to adapt these examples to your specific cloud provider, resources, and desired timeout durations.
terraform apply, Terraform might have partially provisioned resources. It's crucial to understand how your resources and providers handle partial success and rollback scenarios.wait_for scripts or health checks to ensure resources are fully operational after creation or updates, rather than relying solely on timeouts.| Method | Description
In conclusion, while Terraform itself lacks a global timeout for terraform apply, you can effectively manage timeouts using resource-specific configurations, leveraging features in Terraform Enterprise/Cloud, employing external tools within CI/CD pipelines, or utilizing the CLI's state lock timeout. Remember that timeouts often signal deeper infrastructure issues, so prioritize identifying and resolving those root causes. Always test timeout values in controlled environments and consider the impact of resource dependencies. By understanding and implementing these timeout management techniques, you can enhance the reliability and resilience of your Terraform-managed infrastructure.
"terraform apply" fail with timeout error - GCP pipeline - Discourse ... | We have a quickstart open source snowplow deployments on GCP. It has run a month. Just now, I updated the terraform.tfvars files. While I âterraform applyâ on my jump server, I received this timeout error. â Error: timeout while waiting for state to become âcreatedâ (last state: âcreatingâ, timeout: 20m0s) â â with module.iglu_server.google_compute_region_instance_group_manager.grp, â on .terraform/modules/iglu_server/main.tf line 228, in resource âgoogle_compute_region_instance_group_manage...
General Settings - Application Administration - Terraform Enterprise ... | Terraform Run Timeout Settings. The default timeout setting for Terraform runs are 2h for plans, and 24h for applies. These are configurable on a global ...
Timeout setting for terraform binary - Terraform - HashiCorp Discuss | Hi, I have been using terraform binary to automate some VM deployment. While doing terraform apply, the plan gets executed and gets failed in sometime. But actually the plan is in process still. Terraform just gets failed after waiting until its default timeout. Is there any way so that I can set timeout for terraform while doing terraform apply?
Resources - Retries and Customizable Timeouts | Terraform ... | The timeout value specifies the maximum time Terraform will invoke the retry function. You can retrieve the timeout from the *schema.ResourceData struct by ...
Any way to change default timeout of 10m when queueing jobs ... | Is there any way to change the timeout when running a remote CLI apply in TF Cloud? I know thereâs a -lock-timeout option w/ apply but is that whatâs happening here? Docs are unclear. Our scenario is we have a bunch of jobs that get queued up and never get triggered because terraform apply times out waiting in CI/CD⊠Preparing the remote apply... To view this run in a browser, visit: https://app.terraform.io/app/XXX/XXX/runs/run-XXX Waiting for 1 run(s) to finish before being queued... Wait...
Command: apply | Terraform | HashiCorp Developer | -lock-timeout=DURATION - Unless locking is ... All planning modes and planning options for terraform plan - Customize how Terraform will create the plan.
Terraform Apply Command | Options, Examples and Best Practices ... | The terraform apply -lock-timeout command is used to specify a duration for which Terraform will wait for a state lock before it gives up and returns an error.