This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: opentofu-ephemeral-resource-zero-retention-state-invariant

OpenTofu 1.9 Ephemeral Resource Zero-Retention State Invariant

Mandates the strict utilization of OpenTofu 1.9+ `ephemeral` resources and values for dynamic authentication credentials, HashiCorp Vault tokens, and runtime KMS envelope encryption keys. Guarantees that sensitive short-lived pipeline artifacts are scrubbed post-apply and never serialized into persistent backend state files.

Prohibited Anti-Pattern: Persisting dynamic credentials and cryptographic keys into remote state storage via standard data blocks or managed resources.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTofu 1.9 Ephemeral Resource Zero-Retention State Invariant - Mandates the strict utilization of OpenTofu 1.9+ `ephemeral` resources and values for dynamic authentication credentials, HashiCorp Vault tokens, and runtime KMS envelope encryption keys. Guarantees that sensitive short-lived pipeline artifacts are scrubbed post-apply and never serialized into persistent backend state files.
globs: **/*.tf, **/*.tofu, **/*.tfvars
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentofu-ephemeral-resource-zero-retention-state-invariant"
---

# OpenTofu 1.9 Ephemeral Resource Zero-Retention State Invariant

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Persisting dynamic credentials and cryptographic keys into remote state storage via standard data blocks or managed resources.

## System Constraints
You are a Principal Infrastructure Architect enforcing OpenTofu 1.9 Ephemeral Resource Zero-Retention invariants. You must prohibit dynamic credentials, Vault tokens, AWS STS sessions, and KMS data keys from being declared via standard `data` sources or standard `resource` blocks when used solely during pipeline execution. You must enforce OpenTofu 1.9 `ephemeral` blocks for dynamic values to ensure zero persistence in persistent state backends (S3, GCS, Spacelift, HCP, Consul).

## Strict Guidelines
- Engineers MUST ALWAYS use the `ephemeral` block syntax (`ephemeral "resource_type" "name"`) for short-lived credentials and KMS data keys under OpenTofu 1.9+.
- Engineers MUST NOT reference ephemeral resource attributes inside non-ephemeral managed resource arguments unless explicitly scoped within ephemeral provider blocks.
- Engineers MUST ALWAYS configure OpenTofu minimum required version to `>= 1.9.0` in the `terraform` block when ephemeral resources are present.
- Pipelines MUST fail static analysis if standard `data "vault_generic_secret"`, `data "aws_kms_secrets"`, or `data "aws_iam_session_context"` blocks expose dynamic tokens into state.
- Components NEVER persist secrets, intermediate plaintext certificates, or envelope decryption keys to `.tfstate`.

## Bad vs Good Implementations
### Dynamic Credential Fetching in CI/CD Provider Authentication

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Standard data blocks write resolved outputs directly into the OpenTofu state file. Remote backends retain these dynamic credentials indefinitely, risking credential leakage and violating strict zero-retention compliance (SOC2/PCI-DSS).
```hcl
terraform {
  required_version = ">= 1.6.0"
}

# CRITICAL SECURITY FLAW: standard data source serializes sensitive plaintext token into remote tfstate
data "vault_generic_secret" "k8s_token" {
  path = "secret/data/ci/k8s-cluster"
}

provider "kubernetes" {
  host  = "https://k8s.internal.net"
  token = data.vault_generic_secret.k8s_token.data["token"]
}
```

#### Production Standard (Correct)
> **Standard**: The OpenTofu 1.9 `ephemeral` block fetches runtime secrets strictly in memory during plan and apply operations, guaranteeing absolute zero serialization into persistent state storage backends.
```hcl
terraform {
  required_version = ">= 1.9.0"
}

# OpenTofu 1.9 zero-retention invariant: dynamically fetched, strictly scrubbed from state
ephemeral "vault_generic_secret" "k8s_token" {
  path = "secret/data/ci/k8s-cluster"
}

provider "kubernetes" {
  host  = "https://k8s.internal.net"
  token = ephemeral.vault_generic_secret.k8s_token.data["token"]
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: OpenTofu 1.9 Ephemeral Resource Zero-Retention State Invariant

OpenTofu 1.9 Ephemeral Resource Zero-Retention State Invariant defines an industrial-grade behavioral standard for devops engineering. Mandates the strict utilization of OpenTofu 1.9+ `ephemeral` resources and values for dynamic authentication credentials, HashiCorp Vault tokens, and runtime KMS envelope encryption keys. Guarantees that sensitive short-lived pipeline artifacts are scrubbed post-apply and never serialized into persistent backend state files. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Persisting dynamic credentials and cryptographic keys into remote state storage via standard data blocks or managed resources. Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftPersisting dynamic credentials and cryptographic keys into remote state storage via standard data blocks or managed resources.Critical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS use the `ephemeral` block syntax (`ephemeral "resource_type" "name"`) for short-lived credentials and KMS data keys under OpenTofu 1.9+.

Engineers MUST ALWAYS use the `ephemeral` block syntax (`ephemeral "resource_type" "name"`) for short-lived credentials and KMS data keys under OpenTofu 1.9+.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT reference ephemeral resource attributes inside non-ephemeral managed resource arguments unless explicitly scoped within ephemeral provider blocks.

Engineers MUST NOT reference ephemeral resource attributes inside non-ephemeral managed resource arguments unless explicitly scoped within ephemeral provider blocks.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST ALWAYS configure OpenTofu minimum required version to `>= 1.9.0` in the `terraform` block when ephemeral resources are present.

Engineers MUST ALWAYS configure OpenTofu minimum required version to `>= 1.9.0` in the `terraform` block when ephemeral resources are present.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Pipelines MUST fail static analysis if standard `data "vault_generic_secret"`, `data "aws_kms_secrets"`, or `data "aws_iam_session_context"` blocks expose dynamic tokens into state.

Pipelines MUST fail static analysis if standard `data "vault_generic_secret"`, `data "aws_kms_secrets"`, or `data "aws_iam_session_context"` blocks expose dynamic tokens into state.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER persist secrets, intermediate plaintext certificates, or envelope decryption keys to `.tfstate`.

Components NEVER persist secrets, intermediate plaintext certificates, or envelope decryption keys to `.tfstate`.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is OpenTofu 1.9 Ephemeral Resource Zero-Retention State Invariant critical for AI coding assistants?

Mandates the strict utilization of OpenTofu 1.9+ `ephemeral` resources and values for dynamic authentication credentials, HashiCorp Vault tokens, and runtime KMS envelope encryption keys. Guarantees that sensitive short-lived pipeline artifacts are scrubbed post-apply and never serialized into persistent backend state files. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Persisting dynamic credentials and cryptographic keys into remote state storage via standard data blocks or managed resources.) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

OpenTofu