Sohaib
← All Medium articles

Medium · April 14, 2025 · 4 min read

Navigating Common Terraform Scenarios: What Every DevOps Engineer Should Know for Cloud and On-Premises Infrastructure

#devops-tool#terraform#devops

Terraform has become a go-to tool for managing infrastructure across cloud environments and on-premises setups, thanks to its declarative approach to infrastructure as code. However, even with Terraform’s simplicity and power, things can go wrong, whether you’re managing cloud resources or on-premises infrastructure. As DevOps engineers, it’s crucial to understand what can go wrong and how to handle these scenarios. In this article, we explore 10 common Terraform scenarios and explain their potential impact, whether you’re working in the cloud or managing on-prem environments.

1. What Happens if Your State File is Accidentally Deleted?

The state file in Terraform is the key to tracking your managed infrastructure. If it gets deleted, Terraform will lose track of all the resources it manages. On the next terraform apply, it will try to recreate everything from scratch. This can lead to conflicts with existing resources, causing downtime or accidental overwrites. It’s essential to back up your state files and use remote backends, such as S3 with versioning enabled, to avoid such mishaps.

2. What Happens if Multiple Team Members Run terraform apply Simultaneously?

Terraform uses the state file to track resource changes. If multiple team members run terraform apply simultaneously, the state file may become corrupted. One apply will succeed while the others will fail. This scenario could lead to infrastructure drift, where the actual infrastructure doesn’t match the state defined in the code. To prevent this, it’s critical to use state file locking mechanisms, such as Terraform Cloud or backends like S3 with DynamoDB for state locking.

3. What Happens if a Resource Fails Halfway Through a terraform apply?

During a terraform apply, if a resource creation fails midway, Terraform will leave the successfully created resources intact but mark the state of the failed resource as "tainted." On the next run, Terraform will attempt to recreate the tainted resources. This could leave your infrastructure in an inconsistent state, and it’s crucial to manually verify and fix any partial deployments.

4. What Happens When AWS API Rate Limits Are Hit During a Large Terraform Apply?

AWS imposes rate limits on its API calls. When running large deployments with Terraform, you may hit these limits, causing throttling errors. Terraform will retry a few times before failing the apply process. The resources that were successfully created before hitting the limit will remain intact, but the deployment may be incomplete. To avoid this, consider deploying resources in smaller batches or using terraform apply with retries.

5. What Happens if Terraform Plan Shows No Changes, But Infrastructure Was Modified Outside Terraform?

If someone manually modifies infrastructure outside of Terraform, the state file might not reflect these changes. Running terraform plan without a refresh will show no changes, even though the infrastructure has drifted. To detect and correct such issues, use terraform refresh or terraform plan -refresh-only to sync your state with the actual infrastructure.

6. What Happens if You Delete a Resource Definition from Your Configuration?

If you delete a resource definition from your Terraform configuration and run terraform apply, Terraform will destroy the corresponding resource in your infrastructure. This could be catastrophic if you didn’t intend to delete the resource. To avoid this, use lifecycle { prevent_destroy = true } in your resource configurations or manually remove the resource from the state with terraform state rm.

7. What Happens if a Provider API Changes Between Terraform Versions?

Terraform relies on providers to interact with cloud APIs and other infrastructure services. If a provider’s API changes between Terraform versions, you may face compatibility issues. Plans or applies may fail due to mismatched configurations or unsupported features. To prevent this, always check the provider’s documentation for breaking changes before upgrading Terraform versions and ensure that your configurations are compatible with the latest API versions.

8. What Happens if You Have Circular Dependencies in Your Terraform Modules?

Circular dependencies occur when two or more resources depend on each other in a loop. Terraform will fail to initialize or plan with an error indicating a dependency cycle. To fix this, you’ll need to refactor your Terraform modules and break the circular references, possibly by splitting resources into separate stages or using depends_on to enforce an explicit ordering.

9. What Happens if You Exceed AWS Service Quotas During Deployment?

When you exceed service quotas (such as the number of EC2 instances or VPCs), AWS will return a “quota exceeded” error. Terraform will mark these resources as failed, and the deployment will be incomplete. To resolve this, request an increase in your AWS service quota and then rerun the Terraform deployment.

10. What Happens if You Lose Access to the Remote Backend Storing Your State?

Terraform relies on a remote backend (like S3, Azure Blob Storage, or Terraform Cloud) to store the state file, especially in collaborative environments. If you lose access to the backend, all Terraform operations will fail. This effectively blocks any infrastructure changes. It’s crucial to have proper access controls and backups in place to avoid losing access to the remote backend and the critical state file it stores.

Conclusion

Whether you’re managing cloud resources with AWS, Azure, or GCP, or working with on-prem infrastructure, understanding these common Terraform scenarios will help you prepare for potential issues. By implementing best practices like using remote backends, state file locking, and careful version management, you can avoid most of these pitfalls. With Terraform, DevOps engineers can confidently build and maintain infrastructure across environments, if they understand what to watch out for.

So, whether you’re automating your cloud infrastructure or managing on-prem systems, always be prepared for these common scenarios, and your Terraform journey will be much smoother!

Originally published on Medium ↗

Sohaib Shamsi

I build the systems money moves through.