<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Levente Laszlo Papp</title>
    <description>The latest articles on DEV Community by Levente Laszlo Papp (@tret).</description>
    <link>https://dev.to/tret</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4259360%2F5f73262d-07a0-4980-95cc-5e622915e48e.png</url>
      <title>DEV Community: Levente Laszlo Papp</title>
      <link>https://dev.to/tret</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tret"/>
    <language>en</language>
    <item>
      <title>Zero-downtime credential rotation in OpenTofu: the two-phase pattern (and the two bugs it taught me)</title>
      <dc:creator>Levente Laszlo Papp</dc:creator>
      <pubDate>Sun, 22 Nov 2026 22:40:51 +0000</pubDate>
      <link>https://dev.to/tret/zero-downtime-credential-rotation-in-opentofu-the-two-phase-pattern-and-the-two-bugs-it-taught-me-lje</link>
      <guid>https://dev.to/tret/zero-downtime-credential-rotation-in-opentofu-the-two-phase-pattern-and-the-two-bugs-it-taught-me-lje</guid>
      <description>&lt;p&gt;Rotating a long-lived credential sounds trivial: create a new one, hand it out, delete the old one. In practice "hand it out" is never instant. A pipeline that started five minutes ago still has the old token in its environment. A service read its config at startup and caches it. A deployment that pushes the new secret to three systems finishes the first one long before the third.&lt;/p&gt;

&lt;p&gt;If the old credential is revoked the moment the new one exists, everything in that gap breaks. What you want is an &lt;strong&gt;overlap&lt;/strong&gt;: for a while, both the old and the new credential are valid, and consumers are steered to the new one while the old one quietly ages out.&lt;/p&gt;

&lt;p&gt;This post describes how I implement that overlap purely in OpenTofu, with no scheduler, no Lambda and no external state, and two bugs I hit along the way that are worth knowing about if you build something similar.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;First things first: if your workload can use workload identity federation (OIDC), do that instead. A secret that doesn't exist needs no rotation. This pattern is for the long tail federation doesn't reach: tokens for self-hosted tools, SaaS API keys, client secrets for integrations, keys in config files.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The idea: two slots that take turns
&lt;/h2&gt;

&lt;p&gt;Every rotated credential gets two slots, &lt;strong&gt;phase 1&lt;/strong&gt; and &lt;strong&gt;phase 2&lt;/strong&gt;. Each slot holds its own, independent credential. They take turns being the one consumers get, and each switch has an overlap window during which the previous credential still works.&lt;/p&gt;

&lt;p&gt;Two parameters per credential:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;E&lt;/code&gt; (&lt;code&gt;expires_in&lt;/code&gt;): how long a credential is handed out before it is replaced,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;W&lt;/code&gt; (&lt;code&gt;rotation_window&lt;/code&gt;): the overlap, e.g. 20% of &lt;code&gt;E&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A full cycle lasts &lt;code&gt;2E&lt;/code&gt;. With &lt;code&gt;t0&lt;/code&gt; as the start of the cycle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;           0                E-W        E                 2E-W       2E
phase 1    ├──── in use ─────┼── old ───┤                  ├──── in use ───►
phase 2                      ├───────────── in use ────────┼── old ───┤
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;phase 1 &lt;strong&gt;exists&lt;/strong&gt; in &lt;code&gt;[t0, t0+E)&lt;/code&gt; and &lt;code&gt;[t0+2E-W, t0+2E)&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;phase 2 &lt;strong&gt;exists&lt;/strong&gt; in &lt;code&gt;[t0+E-W, t0+2E)&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;consumers &lt;strong&gt;get&lt;/strong&gt; phase 2 in &lt;code&gt;[t0+E-W, t0+2E-W)&lt;/code&gt;, and phase 1 otherwise.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So a new credential is always created &lt;code&gt;W&lt;/code&gt; before the switch, consumers move to it at the switch, and the previous one keeps existing for another &lt;code&gt;W&lt;/code&gt; before it is destroyed. At &lt;code&gt;2E&lt;/code&gt; the cycle starts over.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time without a scheduler
&lt;/h2&gt;

&lt;p&gt;OpenTofu has no built-in notion of "run this at 03:00". It only knows what time it is &lt;em&gt;when you plan&lt;/em&gt;: &lt;code&gt;plantimestamp()&lt;/code&gt;. That turns out to be enough, as long as you run &lt;code&gt;tofu apply&lt;/code&gt; on a schedule that is much shorter than &lt;code&gt;W&lt;/code&gt; (a daily pipeline for credentials rotated monthly, say).&lt;/p&gt;

&lt;p&gt;Three small resources hold the timeline of one credential:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;locals&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;expiry_minutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;duration_parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expires_in&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;rotation_window_minutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rotation_window&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
    &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;duration_parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rotation_window&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expiry_minutes&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Changing expires_in restarts the cycle.&lt;/span&gt;
&lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"terraform_data"&lt;/span&gt; &lt;span class="s2"&gt;"expiry"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;expiry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expiry_minutes&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;lifecycle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;create_before_destroy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Fires once per full cycle (2E).&lt;/span&gt;
&lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"time_rotating"&lt;/span&gt; &lt;span class="s2"&gt;"cycle_timer"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;rotation_minutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expiry_minutes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;lifecycle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;create_before_destroy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="n"&gt;replace_triggered_by&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;terraform_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expiry&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# t0: the plan time at creation, frozen by ignore_changes, reset whenever the cycle timer fires.&lt;/span&gt;
&lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"terraform_data"&lt;/span&gt; &lt;span class="s2"&gt;"deterministic_time"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plantimestamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;lifecycle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;create_before_destroy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="n"&gt;ignore_changes&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;replace_triggered_by&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;time_rotating&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cycle_timer&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The trick is &lt;code&gt;deterministic_time&lt;/code&gt;. &lt;code&gt;plantimestamp()&lt;/code&gt; changes on every plan, but &lt;code&gt;ignore_changes&lt;/code&gt; freezes the value stored at creation, so its &lt;code&gt;input.time&lt;/code&gt; is a stable &lt;code&gt;t0&lt;/code&gt;. When &lt;code&gt;time_rotating&lt;/code&gt; reaches the end of the cycle it is replaced, and &lt;code&gt;replace_triggered_by&lt;/code&gt; replaces &lt;code&gt;deterministic_time&lt;/code&gt; with it, which captures a fresh &lt;code&gt;t0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The phase rules are then plain arithmetic, which I keep in a resource-free module so it can be unit tested with &lt;code&gt;tofu test&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;locals&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expiry_minutes&lt;/span&gt;
  &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rotation_window_minutes&lt;/span&gt;

  &lt;span class="n"&gt;after_e_minus_w&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;timecmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;timeadd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"${local.e - local.w}m"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="n"&gt;before_e&lt;/span&gt;         &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;timecmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;timeadd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"${local.e}m"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="n"&gt;after_2e_minus_w&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;timecmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;timeadd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"${2 * local.e - local.w}m"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="n"&gt;before_2e&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;timecmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;timeadd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"${2 * local.e}m"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="s2"&gt;"phase1_active"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;before_e&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;after_2e_minus_w&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="s2"&gt;"phase2_active"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;after_e_minus_w&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;before_2e&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="s2"&gt;"use_phase2"&lt;/span&gt;    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;after_e_minus_w&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;after_2e_minus_w&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with &lt;code&gt;start_time = terraform_data.deterministic_time.input.time&lt;/code&gt; and &lt;code&gt;now = plantimestamp()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, the credential module (whatever mints the token) is instantiated once per phase, only while that phase exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="s2"&gt;"phase1"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;source&lt;/span&gt;         &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"./extensions/gitlab/rotation"&lt;/span&gt;
  &lt;span class="n"&gt;count&lt;/span&gt;          &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phase1_active&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="n"&gt;configurations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;secret_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="s2"&gt;"phase2"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;source&lt;/span&gt;         &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"./extensions/gitlab/rotation"&lt;/span&gt;
  &lt;span class="n"&gt;count&lt;/span&gt;          &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phase2_active&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="n"&gt;configurations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;secret_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;locals&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;active_phase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;use_phase2&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phase2&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phase1&lt;/span&gt;
  &lt;span class="n"&gt;credential&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;active_phase&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;secret_id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything downstream (a GitHub Actions secret, a Key Vault secret, a variable group) consumes &lt;code&gt;local.credential&lt;/code&gt;. When the active phase flips, OpenTofu updates those consumers in the same apply. When a phase stops existing, its &lt;code&gt;count&lt;/code&gt; drops to 0 and the old credential is destroyed, which for most services means revoked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bug 1: the dependency cycle when the timer fires
&lt;/h2&gt;

&lt;p&gt;The first version worked for exactly one cycle. When &lt;code&gt;time_rotating&lt;/code&gt; expired, every plan failed with a dependency cycle through the rotation module, the merged credential map and every deployment.&lt;/p&gt;

&lt;p&gt;The cause is the default replacement order, &lt;strong&gt;destroy then create&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;destroying the old &lt;code&gt;deterministic_time&lt;/code&gt; must wait until everything depending on it has been updated,&lt;/li&gt;
&lt;li&gt;those dependents (the deployments) need the new credentials,&lt;/li&gt;
&lt;li&gt;the new credentials need the new phases, which need the new &lt;code&gt;t0&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;and the new &lt;code&gt;deterministic_time&lt;/code&gt; can't be created before the old one is destroyed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's a cycle. The fix is one line per resource: &lt;code&gt;create_before_destroy = true&lt;/code&gt; on &lt;code&gt;terraform_data.expiry&lt;/code&gt;, &lt;code&gt;time_rotating.cycle_timer&lt;/code&gt; and &lt;code&gt;terraform_data.deterministic_time&lt;/code&gt; (it's already in the snippet above). The new timer and &lt;code&gt;t0&lt;/code&gt; are created first, dependents update, and the old instances are destroyed last. For these three resources that's harmless, because they have no side effects outside the state. I wrote this down as an ADR, because "why is create_before_destroy on a timestamp?" is exactly the kind of line someone will helpfully delete a year later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bug 2: the credential must outlive its turn, not just its expiry
&lt;/h2&gt;

&lt;p&gt;The second bug is subtler, and I only caught it while writing this post, by simulating the timeline instead of eyeballing the diagram.&lt;/p&gt;

&lt;p&gt;Many services enforce an expiry themselves: an Entra ID client secret has an &lt;code&gt;end_date&lt;/code&gt;, a GitLab token has an &lt;code&gt;expires_at&lt;/code&gt;. The natural thing is to set that to &lt;code&gt;creation + expires_in&lt;/code&gt;. But look at how long each slot actually &lt;strong&gt;exists&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;phase 2 is created at &lt;code&gt;t0+E-W&lt;/code&gt; and destroyed at &lt;code&gt;t0+2E&lt;/code&gt;: it exists for &lt;code&gt;E + W&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;the phase 1 created at &lt;code&gt;2E-W&lt;/code&gt; lives on into the next cycle, until &lt;code&gt;t0'+E&lt;/code&gt;, where &lt;code&gt;t0'&lt;/code&gt; is the new cycle start: again &lt;code&gt;E + W&lt;/code&gt;, plus however late the run that resets the cycle is.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With a service-side lifetime of only &lt;code&gt;E&lt;/code&gt;, the first switch is fine (phase 1 is created at &lt;code&gt;t0&lt;/code&gt; and expires at &lt;code&gt;t0+E&lt;/code&gt;, &lt;code&gt;W&lt;/code&gt; after consumers have left it). From the second switch on, though, the old credential expires &lt;strong&gt;at the very moment consumers switch away from it&lt;/strong&gt;. The overlap is gone, and because the switch happens at the first scheduled run after the boundary, consumers can even hold an already-expired credential until that run arrives.&lt;/p&gt;

&lt;p&gt;The fix is to give credentials a service-side lifetime of &lt;code&gt;expires_in + rotation_window&lt;/code&gt;, rounded up if the service only has day precision (GitLab does). Then every credential is valid for its whole existence, and the overlap holds on every switch.&lt;/p&gt;

&lt;p&gt;The general lesson: in a scheme like this, "how long is a credential handed out" and "how long must it be valid" are different numbers. Write a 30-line simulation of the phase rules before trusting your diagram.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping the plan readable
&lt;/h2&gt;

&lt;p&gt;One more thing that matters in practice: you want &lt;code&gt;tofu plan&lt;/code&gt; to show exactly which consumers a rotation will touch. That only works if the &lt;em&gt;set&lt;/em&gt; of credentials (service and id) is known at plan time, even when the &lt;em&gt;values&lt;/em&gt; aren't. Two habits help:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build the credential map with &lt;code&gt;for&lt;/code&gt; expressions over the configuration keys, not with &lt;code&gt;merge()&lt;/code&gt; over whole module outputs. &lt;code&gt;merge()&lt;/code&gt; turns the entire result unknown as soon as one argument is unknown.&lt;/li&gt;
&lt;li&gt;Give unknown leaves a concrete type. In OpenTofu 1.13+ &lt;code&gt;convert(value, object({ username = string, secret = string }))&lt;/code&gt; does that.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With that, a plan during a rotation shows "phase 2 credential will be created" &lt;strong&gt;and&lt;/strong&gt; "these four secrets in these three systems will be updated", which is what a reviewer actually wants to see.&lt;/p&gt;

&lt;h2&gt;
  
  
  Operational notes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Run often.&lt;/strong&gt; Rotation only happens when OpenTofu runs, and each switch happens at the first run after its boundary, so that delay comes out of the overlap. Schedule &lt;code&gt;apply&lt;/code&gt; at intervals well under half of &lt;code&gt;W&lt;/code&gt;; a missed run is then still caught up within the overlap.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Secrets live in state.&lt;/strong&gt; Rotated values end up in the state and plan. Use OpenTofu's client-side state and plan encryption, and a backend only the rotation pipeline can read.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Two slots fit some APIs perfectly.&lt;/strong&gt; AWS allows exactly two access keys per IAM user, which is exactly what the two phases need (as long as the user has no other keys).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lifetimes are days to months.&lt;/strong&gt; This is for long-lived credentials that consumers need to hold. For minutes-long, per-request credentials, use dynamic secrets (Vault/OpenBao).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Where this lives
&lt;/h2&gt;

&lt;p&gt;All of this is part of &lt;a href="https://github.com/aklakina/gaean-key" rel="noopener noreferrer"&gt;Gaean Key&lt;/a&gt;, an open-source (MIT) OpenTofu project that rotates credentials from YAML files in Git and pushes them to their consumers. The phase arithmetic, the ADR on the dependency cycle and the extensions are in the repo if you want to read real code instead of snippets. There are also two implementations of the engine, a single root module and a Terramate stack per credential, with a write-up of their trade-offs.&lt;/p&gt;

&lt;p&gt;If you've built rotation for credentials that can't be federated, I'd love to hear how you handled the overlap.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>opentofu</category>
      <category>terraform</category>
      <category>security</category>
    </item>
  </channel>
</rss>
