<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nikola Vlahovic on Medium]]></title>
        <description><![CDATA[Stories by Nikola Vlahovic on Medium]]></description>
        <link>https://medium.com/@nikolavlahovic318?source=rss-bf09af716cd4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*GmeHdxarroF8iKCe</url>
            <title>Stories by Nikola Vlahovic on Medium</title>
            <link>https://medium.com/@nikolavlahovic318?source=rss-bf09af716cd4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 05:11:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nikolavlahovic318/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ Replacing Atlantis — Building a Production-Grade Terraform CI/CD Pipeline in Bitbucket]]></title>
            <link>https://medium.com/@nikolavlahovic318/replacing-atlantis-building-a-production-grade-terraform-ci-cd-pipeline-in-bitbucket-b4926006bacf?source=rss-bf09af716cd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4926006bacf</guid>
            <category><![CDATA[terraforming]]></category>
            <category><![CDATA[bitbucket]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[bitbucket-pipelines]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Nikola Vlahovic]]></dc:creator>
            <pubDate>Sat, 21 Feb 2026 15:00:55 GMT</pubDate>
            <atom:updated>2026-02-21T15:00:55.201Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YhA0ulNU9lD0fnoT8zqd5w.png" /></figure><p>The complete implementation is publicly available on GitHub.<br>If you prefer reviewing the full source code directly, you can find it here:<br> 👉 <a href="https://github.com/VlahovicN/bitbucket-terraform-pipeline">https://github.com/VlahovicN/bitbucket-terraform-pipeline</a></p><h3>The Problem</h3><p>In many DevOps teams, <strong>Atlantis</strong> is the default solution for automating Terraform pull request workflows.</p><p>It provides:</p><ul><li>PR-based plans</li><li>Apply gates</li><li>Comment-driven approvals</li><li>Plan/apply consistency</li></ul><p>But what happens when Atlantis is not an option?</p><p>In our case, Bitbucket authentication constraints made native Atlantis integration impossible.<br> Bitbucket deprecated app passwords and replaced them with API tokens, which are not compatible with Atlantis authentication flow.</p><p>We needed:</p><ul><li>Deterministic plan/apply behavior</li><li>PR-driven promotion</li><li>Multi-environment support (dev → staging → prod)</li><li>Manual approval gates</li><li>Targeted resource deployment</li><li>No static AWS credentials</li></ul><p>All of this — <strong>without external tools</strong>.</p><p>So I designed a production-ready Terraform automation pipeline using only Bitbucket Pipelines and a self-hosted runner.</p><p>This article explains the architecture and design decisions behind it.</p><h3>🏗 Architecture Overview</h3><p>The solution is built around:</p><ul><li>Bitbucket Pipelines</li><li>Self-hosted Linux Shell runner (EC2)</li><li>IAM Role-based authentication</li><li>Artifact-based plan/apply workflow</li><li>Branch-per-environment promotion model</li></ul><p>Environments:</p><pre>dev → staging → prod</pre><p>Each environment has:</p><ul><li>Dedicated backend config</li><li>Dedicated tfvars</li><li>Dedicated branch</li><li>Dedicated state</li></ul><h3>⚙ Why a Self-Hosted Linux Shell Runner?</h3><p>Bitbucket Docker runners are limited to 4GB RAM.</p><p>Terraform with:</p><ul><li>Large state files</li><li>Multiple modules</li><li>Memory intensive providers</li></ul><p>… quickly hits memory limits.</p><p>Instead, we use:</p><ul><li>EC2 instance (t3.medium or larger) with Amazon Linux 2023 AMI</li><li>IAM Role attached via Instance Profile</li><li>Linux Shell runner type</li><li>Storage: 50 GB gp3 EBS volume</li></ul><p>Benefits:</p><ul><li>No static AWS credentials</li><li>Full EC2 resources</li><li>Higher reliability</li><li>Production-safe execution</li><li>Cleaner IAM boundary</li></ul><p>Terraform inherits permissions from the instance profile.</p><p>Zero secrets stored in Bitbucket.</p><h3>🔐 Security Model</h3><p>The runner EC2 instance assumes an IAM role:</p><pre>terraform_role</pre><p>Permissions:</p><ul><li>For demonstration purposes, broad permissions (such as AdministratorAccess) can simplify setup, but production environments should always use scoped, environment-specific IAM policies.</li></ul><p>Trust relationship:</p><pre>ec2.amazonaws.com</pre><p>For EKS-based workflows:</p><p>The IAM role must also be added as an EKS Access Entry in each cluster and associated with proper cluster policies.</p><p>This enables Terraform to manage:</p><ul><li>Kubernetes resources</li><li>Helm releases</li><li>Add-ons</li><li>Cluster-level components</li></ul><h3>🔄 The Core Design: Deterministic Plan/Apply</h3><p>The most important design principle:</p><blockquote><em>Apply must execute the exact plan that was reviewed.</em></blockquote><p>To achieve that, the pipeline:</p><ol><li>Generates a binary plan (tfplan.out)</li><li>Exports it as an artifact</li><li>Apply step consumes that exact artifact</li></ol><p>No re-planning during apply.</p><p>This prevents time-of-apply drift if remote state changes between plan and execution.</p><p>This guarantees:</p><ul><li>No drift between plan and apply</li><li>No surprise changes</li><li>Full review integrity</li></ul><h3>🧪 Pull Request–Based Workflow</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hKJWREbpNMgQtPuCe2JWbQ.png" /></figure><p>The PR pipeline has three logical phases:</p><h3>1️⃣ Terraform Plan</h3><ul><li>Simulates merge:</li></ul><pre>git merge --no-commit --no-ff || true</pre><ul><li>Ensures plan reflects post-merge state</li><li>Generates:<br>plan.txt (human readable)<br>tfplan.out (binary artifact)</li></ul><p>This gives reviewers a realistic preview.</p><h3>2️⃣ Targeted Plan (Optional, Manual)</h3><p>If TF_TARGET variable is set:</p><ul><li>Comma-separated list is converted into -target arguments</li><li>New tfplan.out overrides previous artifact</li></ul><p>If not set:</p><ul><li>Step exits safely</li><li>Original plan remains</li></ul><p>This enables granular infrastructure updates when needed.</p><h3>3️⃣ Terraform Apply (Manual)</h3><p>Always manual.</p><p>Apply uses:</p><ul><li>Targeted plan (if generated), OR</li><li>Original full plan artifact</li></ul><p>This ensures deterministic execution.</p><h3>🚦 Promotion Model</h3><p>Promotion is branch-based:</p><pre>dev → staging → prod</pre><p>Each promotion:</p><ul><li>Requires PR</li><li>Triggers full pipeline</li><li>Requires manual apply</li><li>Must be sequential</li></ul><p>Skipping environments can cause:</p><ul><li>State drift</li><li>Unexpected plans</li><li>Inconsistent resources</li></ul><p>This model guarantees predictable environment promotion.</p><h3>🔧 Custom Manual Pipeline</h3><p>Sometimes PR is not needed.</p><p>For example:</p><ul><li>Inspecting state</li><li>Debugging branch config</li><li>Controlled troubleshooting</li></ul><p>The custom pipeline:</p><ul><li>Runs plan directly on branch</li><li>No merge simulation</li><li>Same artifact consistency</li><li>Apply remains manual</li></ul><p>⚠ Custom Manual Pipeline must NOT be used for environment promotion.</p><h3>🧠 Design Decisions &amp; Trade-offs</h3><h3>Why apply before merge?</h3><p>We intentionally apply before merging the PR.</p><p>Reason:</p><p>We merge only when infrastructure has been successfully applied and validated.</p><p>This prevents broken environment branches.</p><h3>Why simulate merge?</h3><p>Because Terraform plan must reflect:</p><blockquote><strong><em>The real post-merge infrastructure state.</em></strong></blockquote><p>Without merge simulation, plan would be inaccurate.</p><h3>Why artifact-based flow?</h3><p>Because:</p><ul><li>Re-running terraform plan during apply can introduce drift</li><li>Remote state could change between steps</li><li>Targeted plans must override full plans safely</li></ul><p>Artifacts guarantee plan integrity.</p><h3>Why not Atlantis?</h3><p>Bitbucket recently deprecated app passwords and replaced them with API tokens, which are not compatible with Atlantis. This makes native Atlantis integration impossible in environments where Bitbucket is the only Git provider available.</p><h3>🧩 What This Solves</h3><p>This pipeline provides:</p><ul><li>Full Terraform automation</li><li>Multi-environment promotion</li><li>Deterministic plan/apply</li><li>IAM-based security</li><li>Manual safety gates</li><li>Targeted resource control</li><li>Zero static AWS credentials</li><li>No external tools</li></ul><p>All inside Bitbucket.</p><h3>📌 Final Thoughts</h3><p>You don’t always need external tooling to achieve production-grade Terraform automation.</p><p>With the right architectural decisions, Bitbucket Pipelines can provide:</p><ul><li>Safe infrastructure promotion</li><li>Plan consistency</li><li>Controlled deployments</li><li>Enterprise-grade behavior</li></ul><p>If Atlantis is not an option, this approach provides a clean, extensible alternative.</p><h3>🔗 Repository</h3><p>You can find the full implementation here:</p><p>👉 <a href="https://github.com/VlahovicN/bitbucket-terraform-pipeline">https://github.com/VlahovicN/bitbucket-terraform-pipeline</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4926006bacf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>