Answer
For a configuration dominated by a single cloud provider, anchor the stability strategy on the global -parallelism flag: tune it so the number of concurrent provider processes stays within the provider's HTTP connection-pool capacity. Keep provider-specific retry settings at their defaults or only modestly increased to absorb occasional transient errors. High retries alone do not prevent the initial connection spike that creates the failures.
Confirmed facts
- Terraform core concurrency is controlled exclusively by the global
-parallelism flag (or the TF_PARALLELISM environment variable). No stable release up to Terraform 1.5.x offers a per-provider concurrency setting. - Provider retry arguments (e.g., the AWS provider's
max_retries and retry_mode) affect only how a plugin process recovers from transient HTTP errors; they do not reduce the number of simultaneous connections opened. - In Terraform Cloud, the workspace Parallelism setting overrides the CLI flag for remote runs. Terraform Enterprise agents can enforce their own parallelism cap, which likewise supersedes the flag when the agent executes the run.
Likely explanation
When many resources are managed by the same provider, each concurrent Terraform worker drives a provider plugin process with its own HTTP connection pool. If global parallelism exceeds the provider's pool capacity, excess workers contend for sockets, producing intermittent connection-reset or timeout errors that often succeed on retry. Lowering -parallelism directly reduces simultaneous pools and prevents the contention. Raising retries only masks the symptom; the spike remains and can hide capacity problems that would surface under sustained load.
Steps for this case
- Determine the provider's observed connection-pool capacity (for AWS, a metric such as
HTTPClient.ConnectionCount, or the provider's equivalent). Without that metric, run a short test at the default parallelism of 10 and note the error rate. - Set a moderate global parallelism below the observed limit, for example
-parallelism=4 or -parallelism=6, via the flag or TF_PARALLELISM. - Leave provider retry settings at defaults (AWS provider:
max_retries = 3, retry_mode = "standard") or raise them only slightly (e.g., to 5) if you see occasional transient spikes unrelated to parallelism. - Verify the change: run
TF_LOG=TRACE terraform apply -parallelism=4 and count simultaneous plugin invocations in the log, or watch the provider's connection metric during the apply. - If errors persist, lower parallelism further; if errors vanish but runtime grows excessively, consider whether lightweight providers (null, local) are being throttled unnecessarily.
Accounting for Terraform Cloud / Enterprise
- In Terraform Cloud, edit the workspace under Settings → General → Parallelism to match the value chosen locally; this overrides any CLI flag on remote runs.
- In Terraform Enterprise, check the agent configuration file (
agent.conf) for the parallelism setting. The agent's value supersedes both the workspace setting and the CLI flag when the agent runs the job. - Align local testing parallelism with the value enforced by the Cloud workspace or Enterprise agent so local results reproduce remotely.
Missing diagnostic detail
If you cannot observe the provider's connection-pool metric, provide the approximate error rate at the default parallelism of 10 (e.g., what percentage of resources fail with a connection timeout on the first attempt). That determines whether a lower cap is needed or the default is already safe.