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=15m
Important 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.