This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: opentofu-state-layer-zk-envelope-encryption-standard

OpenTofu State-Layer Zero-Knowledge Envelope Encryption Standard

Mandates client-side zero-knowledge envelope encryption for all OpenTofu remote state backends using hardware-backed KMS customer managed keys with automated rotation. Guarantees that state payloads containing sensitive resource outputs and cryptographic secrets remain strictly ciphertext prior to leaving CI/CD runner memory and residing in backend storage buckets. Prevents plaintext credential leakage across storage providers, logging collectors, and intermediary transport layers.

Prohibited Anti-Pattern: Unencrypted or Provider-Managed Server-Side Encryption (SSE) of OpenTofu State Payloads

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTofu State-Layer Zero-Knowledge Envelope Encryption Standard - Mandates client-side zero-knowledge envelope encryption for all OpenTofu remote state backends using hardware-backed KMS customer managed keys with automated rotation. Guarantees that state payloads containing sensitive resource outputs and cryptographic secrets remain strictly ciphertext prior to leaving CI/CD runner memory and residing in backend storage buckets. Prevents plaintext credential leakage across storage providers, logging collectors, and intermediary transport layers.
globs: **/*.tofu, **/*.tf, **/terragrunt.hcl, **/backend.tofu, **/encryption.tofu
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentofu-state-layer-zk-envelope-encryption-standard"
---

# OpenTofu State-Layer Zero-Knowledge Envelope Encryption Standard

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unencrypted or Provider-Managed Server-Side Encryption (SSE) of OpenTofu State Payloads

## System Constraints
You are a Principal DevOps Security Architect enforcing the OpenTofu State-Layer Zero-Knowledge Envelope Encryption Standard. You must strictly require native client-side state encryption using OpenTofu 1.7+ encryption configuration blocks. Reject any configuration relying exclusively on cloud provider default server-side encryption (such as S3 AES256 or unmanaged SSE-KMS) which permits cloud provider operators or intermediate transit proxies to decrypt state data. Enforce envelope encryption using customer-managed KMS keys with key rotation and enforce authenticated encryption with associated data (AEAD) algorithms such as AES-GCM.

## Strict Guidelines
- Engineers MUST ALWAYS configure OpenTofu native state encryption blocks (`encryption { ... }`) in client configuration for remote state and plan files.
- Engineers MUST NOT rely exclusively on backend storage-level server-side encryption (SSE-S3, SSE-GCS) as a substitute for client-side state envelope encryption.
- State encryption MUST use modern AEAD ciphers (`AES-GCM` with 256-bit keys) coupled with a hardware-backed Key Management Service (AWS KMS, GCP KMS, or HashiCorp Vault Transit engine).
- KMS Key policies MUST enforce automated cryptographic key rotation and prohibit cross-boundary plaintext export.
- CI/CD runners MUST NEVER write unencrypted state or raw plan outputs to shared runner disk volumes without passing the encryption envelope key.
- Configurations MUST define both remote state and local state/plan encryption targets within the `encryption` block to prevent accidental leakage in local execution caches.

## Bad vs Good Implementations
### Client-Side Zero-Knowledge State Envelope Encryption vs Server-Side Only Backend

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Backend-level encryption (encrypt = true) only mandates S3 server-side encryption (SSE-S3 or default KMS). The state file is transferred over the wire in plain JSON payload to the storage provider, exposing sensitive credentials (database passwords, private keys, bootstrap tokens) to any principal or service-role with storage bucket read permissions.
```hcl
# INSECURE: Relies solely on S3 bucket server-side encryption.
# Plaintext state is transmitted and processed directly in CI/CD runner memory
# and can be read by any IAM identity with s3:GetObject without KMS restrictions.
terraform {
  required_version = ">= 1.7.0"
  backend "s3" {
    bucket         = "corp-production-tofu-state"
    key            = "networking/vpc.tfstate"
    region         = "us-east-1"
    encrypt        = true # Only activates server-side S3 encryption
    dynamodb_table = "tofu-state-locks"
  }
}
```

#### Production Standard (Correct)
> **Standard**: Client-side envelope encryption with OpenTofu's native encryption block ensures that state and plan files are encrypted before egressing the runner process using an ephemeral data key generated via KMS. Even if the S3 bucket is compromised, state contents remain unreadable without explicit decrypt permissions on the designated KMS Key.
```hcl
# SECURE: Enforces OpenTofu 1.7+ client-side state envelope encryption using AWS KMS and AES-GCM.
terraform {
  required_version = ">= 1.7.0"

  encryption {
    key_provider "aws_kms" "primary_kms" {
      kms_key_id = "arn:aws:kms:us-east-1:123456789012:key/mrk-abc123statekey"
      region     = "us-east-1"
      key_spec   = "AES_256"
    }

    method "aes_gcm" "aead_primary" {
      keys = key_provider.aws_kms.primary_kms
    }

    state {
      method   = method.aes_gcm.aead_primary
      enforced = true
    }

    plan {
      method   = method.aes_gcm.aead_primary
      enforced = true
    }
  }

  backend "s3" {
    bucket         = "corp-production-tofu-state"
    key            = "networking/vpc.tfstate"
    region         = "us-east-1"
    dynamodb_table = "tofu-state-locks"
  }
}
```

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 State-Layer Zero-Knowledge Envelope Encryption Standard

OpenTofu State-Layer Zero-Knowledge Envelope Encryption Standard defines an industrial-grade behavioral standard for devops engineering. Mandates client-side zero-knowledge envelope encryption for all OpenTofu remote state backends using hardware-backed KMS customer managed keys with automated rotation. Guarantees that state payloads containing sensitive resource outputs and cryptographic secrets remain strictly ciphertext prior to leaving CI/CD runner memory and residing in backend storage buckets. Prevents plaintext credential leakage across storage providers, logging collectors, and intermediary transport layers. 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: Unencrypted or Provider-Managed Server-Side Encryption (SSE) of OpenTofu State Payloads 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 DriftUnencrypted or Provider-Managed Server-Side Encryption (SSE) of OpenTofu State PayloadsCritical
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 configure OpenTofu native state encryption blocks (`encryption { ... }`) in client configuration for remote state and plan files.

Engineers MUST ALWAYS configure OpenTofu native state encryption blocks (`encryption { ... }`) in client configuration for remote state and plan files.

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 rely exclusively on backend storage-level server-side encryption (SSE-S3, SSE-GCS) as a substitute for client-side state envelope encryption.

Engineers MUST NOT rely exclusively on backend storage-level server-side encryption (SSE-S3, SSE-GCS) as a substitute for client-side state envelope encryption.

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

State encryption MUST use modern AEAD ciphers (`AES-GCM` with 256-bit keys) coupled with a hardware-backed Key Management Service (AWS KMS, GCP KMS, or HashiCorp Vault Transit engine).

State encryption MUST use modern AEAD ciphers (`AES-GCM` with 256-bit keys) coupled with a hardware-backed Key Management Service (AWS KMS, GCP KMS, or HashiCorp Vault Transit engine).

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

KMS Key policies MUST enforce automated cryptographic key rotation and prohibit cross-boundary plaintext export.

KMS Key policies MUST enforce automated cryptographic key rotation and prohibit cross-boundary plaintext export.

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

CI/CD runners MUST NEVER write unencrypted state or raw plan outputs to shared runner disk volumes without passing the encryption envelope key.

CI/CD runners MUST NEVER write unencrypted state or raw plan outputs to shared runner disk volumes without passing the encryption envelope key.

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.
6

Configurations MUST define both remote state and local state/plan encryption targets within the `encryption` block to prevent accidental leakage in local execution caches.

Configurations MUST define both remote state and local state/plan encryption targets within the `encryption` block to prevent accidental leakage in local execution caches.

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 State-Layer Zero-Knowledge Envelope Encryption Standard critical for AI coding assistants?

Mandates client-side zero-knowledge envelope encryption for all OpenTofu remote state backends using hardware-backed KMS customer managed keys with automated rotation. Guarantees that state payloads containing sensitive resource outputs and cryptographic secrets remain strictly ciphertext prior to leaving CI/CD runner memory and residing in backend storage buckets. Prevents plaintext credential leakage across storage providers, logging collectors, and intermediary transport layers. 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 (Unencrypted or Provider-Managed Server-Side Encryption (SSE) of OpenTofu State Payloads) 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