<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Policy as Code with OPA and Terraform on Cloudy Mountains</title><link>https://cloudymountains.cloud/projects/policy-as-code/</link><description>Recent content in Policy as Code with OPA and Terraform on Cloudy Mountains</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Tue, 22 Sep 2026 10:00:00 +0100</lastBuildDate><atom:link href="https://cloudymountains.cloud/projects/policy-as-code/index.xml" rel="self" type="application/rss+xml"/><item><title>Allow Only Approved AMIs in Terraform with OPA</title><link>https://cloudymountains.cloud/projects/policy-as-code/allowed-amis-opa/</link><pubDate>Mon, 23 Jun 2025 13:00:44 +0100</pubDate><guid>https://cloudymountains.cloud/projects/policy-as-code/allowed-amis-opa/</guid><description>&lt;p>In the &lt;a href="https://cloudymountains.cloud/projects/policy-as-code/enforce-aws-tags-opa/">part 2&lt;/a>, we provided examples of how you can enforce AWS tags to ensure compliance and governance within your organization. In this post, we will extend those concepts by demonstrating how you can enforce specific AMI IDs using OPA policies, helping you maintain control over the AMI images used in your infrastructure.&lt;/p>
&lt;p>It is critical for all organizations to establish a robust AMI building process to ensure that operating systems are consistently patched for security vulnerabilities. By maintaining control over the AMI lifecycle, organizations can enforce compliance, reduce exposure to risks, and ensure that their infrastructure remains secure and up-to-date. This process not only helps in mitigating potential threats but also aligns with best practices for infrastructure management and governance. This process is typically managed by the Security team, who are responsible for building and maintaining approved AMIs. The &amp;ldquo;customers&amp;rdquo; of these AMIs are other teams within the organization, such as Development, Operations, or QA, who rely on these pre-approved images to ensure their workloads meet security and compliance standards.&lt;/p></description></item><item><title>Enforcing AWS Resource Tags with OPA</title><link>https://cloudymountains.cloud/projects/policy-as-code/enforce-aws-tags-opa/</link><pubDate>Fri, 13 Jun 2025 13:00:44 +0100</pubDate><guid>https://cloudymountains.cloud/projects/policy-as-code/enforce-aws-tags-opa/</guid><description>&lt;p>In &lt;a href="https://cloudymountains.cloud/projects/policy-as-code/policy-as-code-opa-terraform/">Part 1&lt;/a> of this series, we covered the basics of Policy as Code and how the shift-left approach helps catch infrastructure mistakes early. In this post, we’re putting theory into practice—specifically, how to enforce AWS tagging strategy using Open Policy Agent (OPA) during Terraform plan phase/stage.&lt;/p>
&lt;p>This isn’t just about tagging hygiene. Good tagging is foundational for tracking cloud costs, understanding ownership, and avoiding dangerous or expensive mistakes.&lt;/p>
&lt;h2 id="why-tags-matter">Why Tags Matter&lt;/h2>
&lt;p>Tags seem trivial—until you’re staring at a $1M AWS bill and have no idea where it came from.&lt;/p></description></item><item><title>Testing Terraform Plans with OPA and Conftest (and Atlantis)</title><link>https://cloudymountains.cloud/projects/policy-as-code/policy-as-code-opa-terraform/</link><pubDate>Thu, 15 May 2025 23:58:44 +0100</pubDate><guid>https://cloudymountains.cloud/projects/policy-as-code/policy-as-code-opa-terraform/</guid><description>&lt;p>As I mentioned in my &lt;a href="https://cloudymountains.cloud/projects/policy-as-code/intro-opa-terraform/">previous blog post&lt;/a>, my first OPA policy was just to catch one simple parameter, if we have in the S3 Terrafom module set the &lt;code>force_destroy = true&lt;/code>.&lt;/p>
&lt;p>Here&amp;rsquo;s a simple OPA policy that will catch this dangerous configuration:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-rego" data-lang="rego">&lt;span class="line">&lt;span class="cl">&lt;span class="kd">package&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">terraform&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="nx">plan&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="n">deny&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="nx">msg&lt;/span>&lt;span class="p">]&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">{&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="c"># Find all resources in the plan&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nx">resource&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">:=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">input&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="nx">resource_changes&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="nx">_&lt;/span>&lt;span class="p">]&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> 
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="c"># Check if it&amp;#39;s an S3 bucket&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nx">resource&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="nx">type&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">==&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s2">&amp;#34;aws_s3_bucket&amp;#34;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> 
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="c"># Look for force_destroy in the configuration&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nx">resource&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="nx">change&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="nx">after&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="nx">force_destroy&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">==&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="kc">true&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> 
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nx">msg&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">:=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nf">sprintf&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;S3 bucket &amp;#39;%s&amp;#39; has force_destroy set to true. This is dangerous as it allows bucket deletion even when not empty.&amp;#34;&lt;/span>&lt;span class="o">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="nx">resource&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="nx">address&lt;/span>&lt;span class="p">])&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="p">}&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This policy works by:&lt;/p></description></item><item><title>How I Learned the Hard Way That Terraform Needs Policy as Code</title><link>https://cloudymountains.cloud/projects/policy-as-code/intro-opa-terraform/</link><pubDate>Sun, 30 Mar 2025 13:00:44 +0100</pubDate><guid>https://cloudymountains.cloud/projects/policy-as-code/intro-opa-terraform/</guid><description>&lt;p>&lt;strong>At a glance&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Context:&lt;/strong> a cloud consultancy engagement, bootstrapping 15 new AWS accounts for a client with Terraform 0.11/0.12.&lt;/li>
&lt;li>&lt;strong>Incident:&lt;/strong> I accidentally deleted the S3 bucket that held the Terraform state for all 15 accounts.&lt;/li>
&lt;li>&lt;strong>Cost:&lt;/strong> all 15 accounts were recreated and provisioned again from scratch. Because everything was code, that took less than two days. The real cost was our reputation with the client.&lt;/li>
&lt;li>&lt;strong>Fix:&lt;/strong> policy as code. OPA policies evaluated against every &lt;code>terraform plan&lt;/code> in CI, failing the pipeline before a dangerous change reaches &lt;code>apply&lt;/code>.&lt;/li>
&lt;li>&lt;strong>Outcome:&lt;/strong> 15 workload pipelines gated, and every pipeline created after them inherited the same checks.&lt;/li>
&lt;/ul>
&lt;h2 id="what-went-wrong">What went wrong?&lt;/h2>
&lt;p>A few years ago, while working for a cloud consultancy company, I made a mistake that I will never forget. I accidentally deleted a Terraform state bucket: the S3 bucket that stored the state for bootstrapping 15 AWS accounts, each packed with resources. To make things worse, it belonged to a very tough client.&lt;/p></description></item><item><title>Shift-Left Cost Checks for Terraform with OPA and Terracost</title><link>https://cloudymountains.cloud/projects/policy-as-code/shift-left-opa-terracost/</link><pubDate>Fri, 31 May 2024 13:00:44 +0100</pubDate><guid>https://cloudymountains.cloud/projects/policy-as-code/shift-left-opa-terracost/</guid><description>&lt;h2 id="introduction">Introduction&lt;/h2>
&lt;p>In the fast-evolving world of cloud computing, managing costs effectively has become a crucial aspect of maintaining a sustainable and profitable business. The &amp;ldquo;Shift left&amp;rdquo; paradigm, originally a concept in software development, emphasizes the importance of addressing potential issues early in the development process. When applied to AWS cost optimization and management, this approach ensures that cost considerations are integrated into the earliest stages of the development lifecycle.&lt;/p>
&lt;p>By shifting cost management left, organizations can avoid unexpected expenses and optimize their cloud spending proactively. This blog post explores the application of the &amp;ldquo;Shift left&amp;rdquo; paradigm to AWS cost optimization, focusing on tools like Open Policy Agent (OPA) policies, Infracost, and Terracost. We will delve into how these tools can help estimate and control costs efficiently, ensuring that your AWS resources are both cost-effective and aligned with your financial goals.&lt;/p></description></item></channel></rss>