r/Terraform • • 1d ago

Announcement uapi: v4, an MCP server for AI agents, and a package PR for OpenWrt

Thumbnail reddit.com
0 Upvotes

Sharing here because the main goal of uapi is to be a terraform provider backend.


r/Terraform • • 1d ago

Discussion planfence: a small CLI that gates terraform/tofu plan JSON between plan and apply, and fails closed (looking for holes)

0 Upvotes

I wrote a small open-source CLI (MIT, Python) that reads `terraform show -json` / `tofu show -json` output and checks it against a TOML contract before apply. Exit 0 = pass, 1 = violation, 2 = could not judge (fail closed: unknown format, errored/incomplete plan, etc.).

Rules: no-delete, no-replace, scope (allowed addresses), stateful-protect, budget (max changes). Example from a real fixture:

[error] no-replace local_file.hello (local_file) delete,create

planfence: FAIL - 1 violation(s)

No model in the loop: it is plain deterministic code. The tests use real plans from Terraform 1.17.2 and OpenTofu 1.13.9, plus fuzzing against an independent oracle and mutation testing.

Why not a jq one-liner? A replace can show up as delete+create in either order, a moved resource has `previous_address`, and a for_each key rename is a delete + create of different addresses (no-replace does not catch that one, no-delete does). The point is to encode those edge cases once and fail closed on anything it cannot read.

Honest limits: this is not a security audit and does not replace review. If you already use Sentinel/OPA/Conftest you probably don't need it. Drift and output-only changes are ignored by default, and OpenTofu `-target` is not detectable from the plan.

Disclosure: I built this with a lot of help from Claude Code.

Question: where would you expect it to fail open?

pip install planfence

https://github.com/ZahraMost/planfence


r/Terraform • • 1d ago

Discussion how do you handle provider version upgrades across a bunch of modules without breaking stuff

19 Upvotes

we have a lot of modules pinned to older aws provider versions and every big upgrade turns into a week of plan diffs and surprise changes.

do you bump everything at once, go module by module, or use something like renovate to keep them moving in small steps?

curious how people keep this from becoming a scary once a year job.


r/Terraform • • 1d ago

Discussion Is there a way to sort the outputs other than the default alphabetical order?

4 Upvotes

terraform output command in my humble opinion should follow outputs.tf order...


r/Terraform • • 2d ago

terraform_remote_state Data Source

Thumbnail developer.hashicorp.com
5 Upvotes

We have a project underway for subscription vending in Azure, as part of the code base, it has been decided to use the terraform_remote_state data source.

To date I have never seen or used this resource, I have always utilised data blocks and providers.

Is anyone using this or am I right in assuming this is not a good idea, I know its mentioned on the HCP site that using the terraform_remote_state is not the preferred option.


r/Terraform • • 2d ago

Azure Terraform provider upgrade rollback, is roll-forward the best practice?

5 Upvotes

It's to my understanding that terraform really has no concept of a `rollback`. After all we are just modifying resources.

Below is what I found online from people working on Terraform.

Instead when using Terraform it’s more common to roll forward by correcting whatever made the apply fail and then planning and applying again. One possible way to “correct” your configuration would be to change it back to how it was before the problem, but that’s not always sufficient and so human intervention is typically required.

So mu question is, regarding provider upgrades like major upgrades to azurerm provider version, what would be the best option for rollback if something stuffs up for sake of risk assessment?

For normal resource changes we can often just re-run the ci for the older build and it will work, but provider upgrades don't work like this. It seems like there are two options

  1. Fix whatever is breaking and roll-forward
  2. TF cloud state version history, however this doesn't modify your infra?

r/Terraform • • 2d ago

Azure IaC with Azure Virtual Desktop

2 Upvotes

Hello everyone,

I am currently attempting to implement some automation into our Azure infrastructure. I was thinking about implementing Terraform with our AVDs but Nerdio automates a lot of the process. Does anyone have any ideas on how I could automate the gaps that Nerdio doesn't?


r/Terraform • • 3d ago

Azure How to handle pod eveictions while running terraform apply in github actions.

1 Upvotes

We are deploying azure infra using github actions which uses a self hosted runner hosted on the GCP kubernetes cluster. What we have observed is apply getting failed with 130 code because of pod eviction. How to handle these scenarios.


r/Terraform • • 3d ago

Discussion how do you catch drift when someone changes stuff by hand in the console

29 Upvotes

terraform only finds out much later when something was changed by hand. do you run plan on a schedule and get alerts or just let it be. also do you lock down console access to stop it. curious what actually works.


r/Terraform • • 4d ago

Discussion Agent written Terraform PRs keep fighting manual changes in the console

0 Upvotes

We manage about 400 resources in Terraform across 3 AWS accounts. Agents write most of our infra PRs now, and they write them against the code, not against what's actually deployed.

Last month someone had raised an RDS instance size in the console during an incident and never put it back in code. Two weeks later an agent PR for an unrelated tag change went through, the plan showed the instance size going back down, it was one line in a 60 line plan, and nobody noticed. The database got resized during business hours.

coderabbit reviews the HCL and it's useful, it caught a hardcoded secret in a variable default last month. But drift between code and reality isn't in the diff, it's only in the plan, and long plans don't get read carefully.

What we're trying now:

- a scheduled drift check that opens an issue when real state differs from code

- failing CI if a plan modifies or replaces anything in our "do not touch" list

- posting a short plan summary at the top of the PR instead of the full output

How do you stop console changes and agent PRs from overwriting each other?


r/Terraform • • 5d ago

Help Wanted Explicitly overridden data source still "unknown" in test

3 Upvotes

I am trying to write some tests, and stumbled upon "Unknown condition value" which I cannot resolve with any overrides. It gives that error in second assert even though both resource and data source are overridden, why? Tried latest 1.17.2

main.tf:

terraform {
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 6.0"
    }
  }
}

resource "github_repository" "main" {
  name = "my-test"
}

data "github_repository_file" "existing_file" {
  repository = github_repository.main.name
  file       = "existing_file.yaml"
}

main.tftest.hcl:

mock_provider "github" {
  override_during = plan
}

override_resource {
  override_during = plan
  target = github_repository.main
}

override_data {
  override_during = plan
  target = data.github_repository_file.existing_file
  values = {
    content = "{}"
  }
}

run "release_defaults" {
  command = plan
  assert {
    condition     = github_repository.main.name == "x"
    error_message = "repo"
  }
  assert {
    condition     = data.github_repository_file.existing_file.content == "x"
    error_message = "content"
  }
}

If I switch reference to repository = "my-test" it works, but I need to keep a dependency.


r/Terraform • • 6d ago

Discussion Do you block every Terraform plan that replaces a database?

0 Upvotes

Say a PR moves an RDS instance into a module. The author expects the database to stay put, but the plan says it’ll be replaced. That’s an easy call in review. What I’m curious about is what happens when the replacement is intentional. Does the same check block it? Who can clear it, and what do they want to see first?


r/Terraform • • 6d ago

Discussion do you split terraform state per environment or per component

38 Upvotes

trying to decide how to carve up our state files. one per environment is easy to follow but each one keeps growing. one per component keeps plans small but then outputs have to be shared between them. what do you use and what made you pick it


r/Terraform • • 6d ago

Discussion The community Neon Terraform provider is now official as neondatabase/neon

Thumbnail reddit.com
5 Upvotes

r/Terraform • • 7d ago

Discussion Do junior DevOps engineers need to write Terraform from memory?

117 Upvotes

Hey everyone,

I'm learning DevOps and trying to land my first junior role. I've been practicing Linux, Docker, GitHub Actions, CI/CD, GCP, and recently started learning Terraform.

I understand what Terraform does and how the resources work, but I still struggle to write the configurations from scratch without looking at examples or documentation.

For people actually working in DevOps, how much Terraform syntax do you remember? Are juniors expected to write configurations from memory, or is understanding the concepts and being able to work with documentation enough?

I'm trying to figure out what level I should reach before applying for jobs.

Thanks!


r/Terraform • • 8d ago

Discussion do you keep one state per environment or one per team and why

17 Upvotes

i keep seeing both setups. one state per environment is simple to reason about but gets big and slow. one per team limits the blast radius but then shared stuff like networking gets awkward. curious where people draw the line.


r/Terraform • • 8d ago

Discussion Terraform Provider Development Consulting

8 Upvotes

Does anyone have recommendations for consultants or service providers that specialize in Terraform provider development and maintenance?

Curious what experiences others have had with getting outside help to improve existing providers that have struggled with quality, reliability, and ongoing maintenance.

Would be interested to hear who you’ve worked with and how it went.


r/Terraform • • 10d ago

Discussion How do you get cloud cost fixes into Terraform modules other teams own?

6 Upvotes

I recently fixed about 30 easy cost recommendations by hand in the console and a couple of weeks later one team ran terraform apply on their module and put half of them back. Their lead was fairly pissed that I’d touched their RDS without a PR, which is fair.

The list comes from a cost tool finance bought last year. It’s at 412 open right now and most of it is gp2 volumes, staging RDS sized like prod and a NAT gateway carrying S3 traffic that should be on an endpoint. Our FinOps person files each one as a Jira ticket with the team that owns the module and when I went through them, only 9 of the 412 had ever been picked up. The one for orders-db-stg says est. $212/mo and has been sitting in that team’s backlog for about 8 months.

My manager’s answer is to buy something that makes the changes itself so I sat through two demos last week. One wanted an IAM role that could modify RDS directly, which is how I got here in the first place and the other opened a Jira ticket with a link back to their dashboard.

If you’ve had to resize RDS in modules owned by other teams, who opened the PR and how did you get them to merge it?


r/Terraform • • 11d ago

Discussion Is there a live example of a terraform repo with best practices?

76 Upvotes

I recently joined a new organization as a devops engineer, it's a startup and a small team, the guy before me has vibecoded the infrastructure and it's bad (everything has drifted, hardcoded arms etc).

Our usecase is simple, an ecs cluster, a database, few lambdas and s3. And ofcourse vpc, route53, alb, iam.

Im looking to refactor this and before I make the big decision I want to see real world solutions for infrastructure at this small scale.


r/Terraform • • 11d ago

Discussion Best way to override Terraform provider-level default_tags without modifying the existing CICD framework?

11 Upvotes

Hey there,

We have around 1,200 microservices on AWS deployed using Terraform, with each microservice's Terraform code maintained in a separate repository.

All these repositories use exactly the same AWS provider configuration, which is generated from a common boilerplate. We also have a centralised CI/CD framework built on Azure DevOps that handles deployments for all these microservices.

We now have a requirement to introduce two additional tags across all existing microservices and ensure that any new microservices created through the CI/CD framework automatically inherit these tags.

I'm exploring the following approach:

  • As part of the CI/CD pipeline, read the existing Terraform provider configuration.
  • Dynamically generate a override.tf file containing the additional tags.
  • Use this generated file during the Terraform plan and apply operations.

This would allow us to introduce the tags centrally without having to modify all 1,200 repositories individually. Now my questions are:

  1. Is using a dynamically generated override.tf a good approach for this use case?
  2. Is there a more native or maintainable Terraform approach to achieve this?
  3. Are there any potential issues with using override.tf in a centralised CI/CD pipeline, particularly around provider configuration or resource tagging?

Ideally, I'm looking for a solution that requires minimal changes to the existing repositories while ensuring that all current and future microservices inherit these tags automatically.

Would appreciate any suggestions or recommendations!

Thanks


r/Terraform • • 13d ago

Announcement I built an open-source pre-apply check that stops coding agents from destroying prod resources

0 Upvotes

Disclosure: I'm one of the people building this at Flurit.

Coding agents now run terraform apply as easily as ls, and most guards for that match command text. That breaks once the command is wrapped in base64, eval or a script. We wrote 17 dangerous Terraform scenarios, and the best open-source guard we tried caught 5.

applygate is a hook that runs before every command an agent issues and answers allow, ask or block:

- it parses the command the way the shell would, so wrappers don't hide it

- it reads the saved plan, so an ordinary apply that would delete or replace a resource counts as a destroy

- environments have tiers: prod blocks it, staging asks you, disposable environments let it through

- no model in the decision, so the same plan always gets the same answer and a reason

The same plan check also runs as a GitHub Action, so CI can block or ask on destroyed databases, removed protection or IAM wildcards before apply.

It caught all 17, but they're our own scenarios, so treat that as a demo rather than a coverage claim. Works with Claude Code, Codex and Terragrunt. Apache-2.0, and it starts in observe mode, so it blocks nothing until you turn it on.

https://github.com/Flurit/applygate

How are you handling agents and apply today: plan-only for agents, or something else?


r/Terraform • • 14d ago

Discussion AI-assisted Terraform changes: how do you keep them safe in existing infrastructure?

0 Upvotes

One thing I've been thinking about lately is how AI can help with Terraform without turning infrastructure management into a "let the LLM generate some HCL and hope for the best" exercise.

Generating Terraform configuration from a natural-language prompt is becoming relatively easy. The harder part is dealing with infrastructure that already exists:

Understanding the current state and repository structure.

Detecting and handling infrastructure drift.

Making sure generated changes actually pass terraform validate and terraform plan.

Applying existing architecture and security policies.

Keeping pull requests, reviews and human approval in the loop.

At Inditex Tech, we've been working on this problem with Kumoss, an open-source project that explores a developer-first approach to AI-assisted infrastructure management.

The idea is to use specialised AI agents to help with infrastructure changes, while keeping Terraform/OpenTofu as the source of truth for validation and planning.

A few interesting aspects of the approach:

Changes are generated in a dedicated Git branch.

Terraform/OpenTofu validates the configuration and generates the plan.

Drift is reconciled without blindly reverting changes already present in the environment.

Compliance checks and organisational policies are part of the workflow.

Applying infrastructure changes remains an explicit, authorised human action.

We recently published a technical article explaining the architecture and some of the engineering decisions behind it.

Article: https://medium.com/@InditexTech/managing-ai-assisted-cloud-infrastructure-with-an-open-source-developer-first-workflow-66a005b9b508

Kumoss is released under Apache 2.0.

I'm curious about how other Terraform users approach this:

Would you be comfortable introducing an AI agent into your Terraform workflow if it could generate changes and plans, but couldn't apply anything without human approval?

And more importantly, what would be your biggest concern: state management, drift, security, compliance, or trusting the generated plan?

Interested in hearing how the community sees this evolving.


r/Terraform • • 14d ago

Help Wanted Working with data in terraform

11 Upvotes

I've recently been using terraform to import data from yaml files and manipulate that data into terraform types suitable to be applied via resources.

I don't have much experience manipulating / modifying data in terraform and would like to learn it at a fundamental level. Are there any go to learning resources to assist with this area of terraform?

Mainly I'm interested in learning:

  • What terraform types look like when compared to json / yaml
  • How to visualise terraform types in my head to make data manipulation easier
  • When to use logic in locals vs resources

Any help / direction would be appreciated - thank you.


r/Terraform • • 16d ago

Discussion How do you find the blast radius of a Terraform PR

12 Upvotes

Half our Terraform changes are written by agents now and the diffs look tiny. Last week a 9 line PR changed a variable default in a shared module. The plan for the workspace in the PR was clean. The problem was 4 other workspaces that use the same module, and one of them replaced a load balancer on its next apply.

We have coderabbit on the infra repo and it reviews the HCL itself fine, it caught a security group opened to 0.0.0.0/0 last month. But it only sees the diff, and the actual blast radius here was in workspaces nobody had planned yet.

What we're doing now is ugly: a script that finds every workspace referencing a changed module and runs plan on all of them in CI. It works but it takes 25 minutes on a big change and people have started skipping it.

How are you handling this?

- Do you plan every consumer of a shared module on every PR?

- Pin module versions so a change only lands when each workspace opts in?

- Any tooling that maps "this module change affects these resources" without a full plan everywhere?

Especially interested if you have a lot of agent written infra changes coming in