<?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 Shehu Yusuf on Medium]]></title>
        <description><![CDATA[Stories by Shehu Yusuf on Medium]]></description>
        <link>https://medium.com/@shehuyusuf?source=rss-607ba1670d2d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*7JIXk9oBoH-RjE2w</url>
            <title>Stories by Shehu Yusuf on Medium</title>
            <link>https://medium.com/@shehuyusuf?source=rss-607ba1670d2d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 16:17:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shehuyusuf/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[How I Built SwiftDeploy: A Tool That Writes Its Own Infrastructure]]></title>
            <link>https://medium.com/@shehuyusuf/how-i-built-swiftdeploy-a-tool-that-writes-its-own-infrastructure-9a3aebe856c4?source=rss-607ba1670d2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a3aebe856c4</guid>
            <dc:creator><![CDATA[Shehu Yusuf]]></dc:creator>
            <pubDate>Wed, 06 May 2026 16:47:08 GMT</pubDate>
            <atom:updated>2026-05-06T16:47:08.445Z</atom:updated>
            <content:encoded><![CDATA[<p><em>From a single YAML file to a fully running, policy-gated, observable deployment stack — here’s everything I learned.</em></p><h3>The Problem With Manual Infrastructure</h3><p>Every developer has been there. You have a service to deploy. You write a docker-compose.yml. Then you write an nginx.conf. Then you forget to update one of them when something changes. Then things break at 2am.</p><p>The real problem isn’t the tools — it’s that you have multiple files that need to stay in sync. Change a port in one place and you have to remember to change it in three others.</p><p>What if there was <strong>one file</strong> that drove everything else?</p><p>That’s exactly what SwiftDeploy solves.</p><h3>Architecture</h3><p>The full stack looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VidYyoGs6RK8zQggL_fQSg.png" /></figure><h3>What Is SwiftDeploy?</h3><p>SwiftDeploy is a command-line tool I built from scratch that:</p><ol><li>Reads a single manifest.yaml file you write</li><li><strong>Generates</strong> all your infrastructure config files automatically</li><li>Deploys your entire stack with one command</li><li>Checks policy rules before allowing any deployment</li><li>Shows you a live dashboard of what’s happening</li><li>Keeps an audit trail of everything</li></ol><p>No cloud required. No AWS. No Terraform. Just Docker on your laptop.</p><h3>Stage 4A — The Engine: Building the Tool That Builds the Stack</h3><h3>The Core Idea: One File to Rule Them All</h3><p>The manifest.yaml is the only file you ever touch:</p><pre>services:<br>  image: swift-deploy-1-node:latest<br>  port: 3000<br>  mode: stable<br>  version: &quot;1.0.0&quot;<br>  restart_policy: unless-stopped<br>  log_volume: swiftdeploy-logs<br>nginx:<br>  image: nginx:latest<br>  port: 8080<br>  proxy_timeout: 30<br>network:<br>  name: swiftdeploy-net<br>  driver_type: bridge</pre><p>That’s it. From this one file, SwiftDeploy generates:</p><ul><li>A complete nginx.conf with proper timeouts, error pages, and security headers</li><li>A docker-compose.yml with non-root containers, health checks, and network isolation</li></ul><p>If the grader deletes your generated files and runs swiftdeploy init, everything comes back perfectly. That&#39;s the test.</p><h3>How Templates Work</h3><p>I used <strong>Jinja2</strong> — a Python templating engine. Think of it like a fill-in-the-blanks document.</p><p>The nginx template looks like this:</p><pre>server {<br>    listen {{ nginx.port }};<br>    ...<br>    upstream swiftdeploy_backend {<br>        server swiftdeploy-service:{{ services.port }};<br>    }<br>}</pre><p>When you run swiftdeploy init, the tool reads manifest.yaml and fills in {{ nginx.port }} with 8080, {{ services.port }} with 3000, and so on. Clean, readable, and reproducible.</p><h3>The API Service</h3><p>The service is a Python HTTP server with three endpoints:</p><ul><li><strong>GET /</strong> — Welcome message with current mode, version, and timestamp</li><li><strong>GET /healthz</strong> — Liveness check with uptime in seconds</li><li><strong>POST /chaos</strong> — Simulates failures (canary mode only)</li></ul><p>The chaos endpoint is what makes this interesting. You can tell the service to:</p><ul><li>Sleep for N seconds before responding (slow mode)</li><li>Return HTTP 500 on X% of requests (error mode)</li><li>Go back to normal (recover)</li></ul><p>This lets you test how your system behaves under real failure conditions.</p><h3>Security by Default</h3><p>Every container in SwiftDeploy follows production security standards:</p><pre>user: &quot;1001:1001&quot;          # Non-root user<br>cap_drop: [ALL]            # Drop all Linux capabilities<br>security_opt:<br>  - no-new-privileges:true # Can&#39;t escalate privileges<br>read_only: true            # Read-only filesystem</pre><p>The service port (3000) is <strong>never exposed</strong> to the host. All traffic must go through Nginx on port 8080. This is called network isolation — even if someone compromises the service container, they can&#39;t reach it directly from outside.</p><h3>The CLI Subcommands</h3><pre>swiftdeploy init      # Generate configs from manifest<br>swiftdeploy validate  # 5 pre-flight checks<br>swiftdeploy deploy    # Init + validate + bring up stack<br>swiftdeploy promote   # Switch canary/stable mode<br>swiftdeploy teardown  # Remove everything</pre><p>The validate command runs 5 checks before any deployment:</p><ol><li>Does manifest.yaml exist and parse correctly?</li><li>Are all required fields present?</li><li>Does the Docker image exist locally?</li><li>Is the Nginx port free?</li><li>Does nginx -t pass on the generated config?</li></ol><p>If any check fails, deployment is blocked with a clear error message.</p><h3>Stage 4B — The Eyes and Brain: Observability and Policy</h3><p>Stage 4A built the engine. Stage 4B adds the intelligence.</p><h3>The Eyes: /metrics in Prometheus Format</h3><p>I added a /metrics endpoint to the API service that exposes real-time data in Prometheus format — the industry standard for metrics collection.</p><p>It tracks:</p><p><strong>Throughput and Errors:</strong></p><pre>http_requests_total{method=&quot;GET&quot;,path=&quot;/&quot;,status_code=&quot;200&quot;} 42<br>http_requests_total{method=&quot;GET&quot;,path=&quot;/&quot;,status_code=&quot;500&quot;} 3</pre><p><strong>Latency (as a histogram):</strong></p><pre>http_request_duration_seconds_bucket{le=&quot;0.1&quot;} 38<br>http_request_duration_seconds_bucket{le=&quot;0.5&quot;} 41<br>http_request_duration_seconds_bucket{le=&quot;+Inf&quot;} 42</pre><p><strong>Application State:</strong></p><pre>app_uptime_seconds 3600.5<br>app_mode 0          # 0=stable, 1=canary<br>chaos_active 0      # 0=none, 1=slow, 2=error</pre><p>Importantly, /metrics is <strong>blocked at the Nginx level</strong> — it returns 403 if you try to access it publicly. Only the CLI can scrape it directly from the container. This is a security best practice.</p><h3>The Brain: Open Policy Agent (OPA)</h3><p>This is where things get interesting. SwiftDeploy now refuses to deploy or promote unless your environment passes specific safety checks.</p><p>But here’s the key design decision: <strong>the CLI itself never makes allow/deny decisions</strong>. All logic lives in OPA — an open-source policy engine that evaluates rules written in a language called Rego.</p><p>Why does this matter? Because it separates <strong>what the tool does</strong> from <strong>what the rules are</strong>. You can change a policy without touching the CLI code.</p><h4>Infrastructure Policy</h4><p>Before deploying, SwiftDeploy checks your host machine:</p><pre>package swiftdeploy.infrastructure<br><br>violations contains msg if {<br>    input.disk_free_gb &lt; data.thresholds.min_disk_free_gb<br>    msg := sprintf(&quot;Disk free %.1fGB is below minimum %.1fGB&quot;, [...])<br>}<br>violations contains msg if {<br>    input.cpu_load_1m &gt; data.thresholds.max_cpu_load<br>    msg := sprintf(&quot;CPU 1m load %.2f exceeds maximum %.2f&quot;, [...])<br>}</pre><p>If your disk is below 10GB or CPU load is above 2.0, deployment is blocked:</p><pre>✘  [OPA:infrastructure] Policy FAILED<br>   • Disk free 4.2GB is below minimum 10.0GB</pre><h4>Canary Safety Policy</h4><p>Before promoting to canary (or back to stable), SwiftDeploy scrapes /metrics, calculates your error rate and P99 latency, and sends them to OPA:</p><pre>package swiftdeploy.canary<br><br>violations contains msg if {<br>    input.error_rate_percent &gt; data.thresholds.max_error_rate_percent<br>    msg := sprintf(&quot;Error rate %.2f%% exceeds maximum %.2f%%&quot;, [...])<br>}<br>violations contains msg if {<br>    input.p99_latency_ms &gt; data.thresholds.max_p99_latency_ms<br>    msg := sprintf(&quot;P99 latency %.0fms exceeds maximum %.0fms&quot;, [...])<br>}</pre><p>Notice that the threshold values (10.0, 2.0, 1.0, 500.0) are <strong>not hardcoded in the Rego files</strong>. They live in a separate data.json file:</p><pre>{<br>  &quot;thresholds&quot;: {<br>    &quot;min_disk_free_gb&quot;: 10.0,<br>    &quot;max_cpu_load&quot;: 2.0,<br>    &quot;max_error_rate_percent&quot;: 1.0,<br>    &quot;max_p99_latency_ms&quot;: 500.0<br>  }<br>}</pre><p>This means you can change thresholds without touching policy logic. Clean separation of concerns.</p><h4>OPA Is Isolated from Public Ingress</h4><p>OPA runs on an internal Docker network. It’s reachable by the CLI on localhost:8181 but <strong>completely invisible to the public Nginx port</strong>. You cannot reach OPA from localhost:8080.</p><p>This satisfies the “no leakage” requirement — the policy engine is an internal tool, not a public API.</p><h3>The Live Dashboard: swiftdeploy status</h3><pre>python3 swiftdeploy status</pre><p>This launches a terminal dashboard that refreshes every 5 seconds:</p><pre>╔══════════════════════════════════════════╗<br>║     SwiftDeploy Live Status Dashboard    ║<br>╚══════════════════════════════════════════╝<br>  Time  : 2026-05-06T10:17:46Z<br>  Mode  : STABLE   Uptime: 3600s<br><br>── Metrics ──────────────────────────────────<br>  Throughput : 2.4 req/s<br>  Error rate : 0.0%<br>  P99 latency: 12.3ms<br>  Chaos      : none<br>  ── Policy Compliance ────────────────────────<br>  [PASS] infrastructure<br>  [PASS] canary<br>  ── Containers ───────────────────────────────<br>  swiftdeploy-nginx:   Up 1 hour (healthy)<br>  swiftdeploy-opa:     Up 1 hour (healthy)<br>  swiftdeploy-service: Up 1 hour (healthy)</pre><p>Every scrape is written to history.jsonl — one JSON object per line, one line per scrape.</p><h3>The Audit Report: swiftdeploy audit</h3><pre>python3 swiftdeploy audit</pre><p>This reads history.jsonl and generates a clean audit_report.md with:</p><ul><li>A timeline table of all events (deploys, promotions, teardowns)</li><li>A violations section (any time OPA blocked something)</li><li>A chaos events section (any time chaos was active during a scrape)</li></ul><h3>What Happened When I Injected Chaos</h3><p>This is where the project gets really fun.</p><p>I promoted to canary mode, then injected an error rate:</p><pre>python3 swiftdeploy promote canary</pre><pre>curl -X POST <a href="http://localhost:8080/chaos">http://localhost:8080/chaos</a> \<br>  -H &quot;Content-Type: application/json&quot; \<br>  -d &#39;{&quot;mode&quot;: &quot;error&quot;, &quot;rate&quot;: 0.9}&#39;</pre><p>The dashboard immediately showed the degradation:</p><pre>Error rate : 89.3%    ← RED<br>  P99 latency: 245.0ms</pre><pre>  [PASS] infrastructure<br>  [FAIL] canary<br>         • Error rate 89.30% exceeds maximum 1.00%</pre><p>When I tried to promote back to stable, OPA blocked it:</p><pre>✘  [OPA:canary] Policy FAILED<br>   • Error rate 89.30% exceeds maximum 1.00%<br>   Promotion BLOCKED by OPA canary policy</pre><p>I had to recover from chaos first:</p><pre>curl -X POST http://localhost:8080/chaos \<br>  -H &quot;Content-Type: application/json&quot; \<br>  -d &#39;{&quot;mode&quot;: &quot;recover&quot;}&#39;</pre><p>Then wait for the error rate to drop below 1%, then promote. That’s real canary safety in action.</p><h3>Lessons Learned</h3><p><strong>1. Declarative is better than imperative.</strong> Writing manifest.yaml once and generating everything from it is so much cleaner than maintaining multiple config files by hand. If something breaks, there&#39;s one place to look.</p><p><strong>2. Separate policy from code.</strong> Putting all allow/deny logic in OPA means you can audit your policies independently, change thresholds without redeploying, and reason about them in isolation. The CLI just asks “is this allowed?” — it doesn’t decide.</p><p><strong>3. Security defaults matter.</strong> Non-root containers, dropped capabilities, read-only filesystems — these should be defaults, not afterthoughts. The spec required them and they make the system meaningfully more secure.</p><p><strong>4. The WSL file mount gotcha.</strong> Docker Desktop on Windows WSL2 cannot bind-mount individual files from the Windows filesystem. The fix was to bake the nginx config directly into a custom nginx image at build time. This cost me hours but taught me a lot about how Docker Desktop works under the hood.</p><p><strong>5. OPA is powerful but needs data.</strong> OPA is great, but it only knows what you tell it. The data.json file that feeds thresholds into the Rego policies is just as important as the policies themselves. Never hardcode environment-specific values in policy logic.</p><h3>How to Replicate This</h3><p><strong>Prerequisites:</strong></p><ul><li>Docker Desktop (with WSL2 on Windows)</li><li>Python 3.10+</li><li>Git</li></ul><p><strong>Steps:</strong></p><pre># Clone the repo<br>git clone https://github.com/Shakurrrr/swiftdeploy<br>cd swiftdeploy<br><br># Build the service image<br>docker build -t swift-deploy-1-node:latest ./app<br># Deploy everything<br>python3 swiftdeploy deploy<br># Open the dashboard<br>python3 swiftdeploy status<br># Promote to canary<br>python3 swiftdeploy promote canary<br># Inject chaos<br>curl -X POST http://localhost:8080/chaos \<br>  -H &quot;Content-Type: application/json&quot; \<br>  -d &#39;{&quot;mode&quot;: &quot;error&quot;, &quot;rate&quot;: 0.5}&#39;<br># Watch the dashboard catch it<br># Then recover<br>curl -X POST http://localhost:8080/chaos \<br>  -H &quot;Content-Type: application/json&quot; \<br>  -d &#39;{&quot;mode&quot;: &quot;recover&quot;}&#39;<br># Generate audit report<br>python3 swiftdeploy audit<br># Tear down<br>python3 swiftdeploy teardown --clean</pre><p><em>Stage 4A &amp; 4B.</em> <em>GitHub: </em><a href="https://github.com/Shakurrrr/swiftdeploy"><em>https://github.com/Shakurrrr/swiftdeploy</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a3aebe856c4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How I Built a System That Detects Suspicious Website Activity in Real-Time]]></title>
            <link>https://medium.com/@shehuyusuf/building-a-real-time-nginx-anomaly-detection-system-with-slack-alerts-hng-stage-3-aa372f89fc05?source=rss-607ba1670d2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa372f89fc05</guid>
            <dc:creator><![CDATA[Shehu Yusuf]]></dc:creator>
            <pubDate>Mon, 27 Apr 2026 08:01:10 GMT</pubDate>
            <atom:updated>2026-05-02T10:54:46.954Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction: Why This Project Matters</h3><p>Imagine you own a website.</p><p>At first, everything works perfectly. Pages load fast, users interact smoothly, and everything feels under control.</p><p>Then one day:</p><ul><li>Your site suddenly slows down</li><li>Traffic spikes unexpectedly</li><li>Some users keep refreshing the same page over and over</li></ul><p>You start wondering:</p><ul><li>Is this real traffic or an attack?</li><li>Who is responsible for this behavior?</li><li>How do I stop it before it crashes my system?</li></ul><p>This is a very real problem in modern web systems. Websites are constantly exposed to bots, automated scripts, and malicious traffic. Without proper monitoring, these issues can quietly build up until they cause serious damage.</p><p>That’s exactly the problem I set out to solve.</p><p>In this project, I built a system that can:</p><ul><li>Monitor incoming traffic in real time</li><li>Detect suspicious behavior automatically</li><li>Alert me instantly when something goes wrong</li><li>And even take action against harmful users</li></ul><h3>The Goal: Building a “Security Guard” for a Website</h3><p>Instead of thinking about this as a complex system, I approached it with a simple idea:</p><p>What if a website had a <strong>security guard</strong>?</p><p>This guard would:</p><ul><li>Watch every visitor coming in</li><li>Notice when someone behaves strangely</li><li>Raise an alarm immediately</li><li>And step in when things get out of control</li></ul><p>That’s exactly what this system does.</p><p>It acts like a <strong>real-time watchdog</strong>, constantly observing activity and reacting when necessary.</p><h3>System Overview (Explained Like a Real-Life Scenario)</h3><p>To make this easy to understand, let’s compare the system to a physical building.</p><h3>1. The Entrance Gate (Nginx)</h3><p>Every building has an entrance.</p><p>In this system, that entrance is handled by a web server called <strong>Nginx</strong>.</p><p>Every visitor (user request) passes through this gate, and their activity is recorded in a log file — like signing a guestbook.</p><p>So now we have:</p><ul><li>A record of every visitor</li><li>A timeline of what they did</li></ul><h3>2. The Watcher (Log Monitoring Service)</h3><p>Now imagine someone sitting beside that guestbook, watching every entry in real time.</p><p>That’s what my Python service does.</p><p>It continuously reads the log file and keeps track of:</p><ul><li>Who is visiting</li><li>How often they visit</li><li>Patterns in their behavior</li></ul><p>This is where things start getting interesting.</p><h3>3. Detecting Suspicious Behavior</h3><p>Not every visitor behaves normally.</p><p>Some patterns are clear red flags, for example:</p><ul><li>One person making hundreds of requests in seconds</li><li>Repeated actions that don’t look human</li><li>Sudden spikes in traffic from a single source</li></ul><p>The system detects these patterns automatically.</p><p>Instead of using overly complex logic, I focused on practical checks like:</p><ul><li>Request frequency over time</li><li>Sudden spikes in activity</li><li>Repeated behavior from the same IP</li></ul><p>If something crosses a certain threshold, it is flagged as suspicious.</p><h3>4. Instant Alerts (Slack Integration)</h3><p>Detecting a problem is not enough — you need to know immediately.</p><p>So I integrated Slack into the system.</p><p>Whenever suspicious activity is detected:</p><ul><li>A message is sent instantly</li><li>It contains the IP address</li><li>It explains why the activity is suspicious</li></ul><p>Example:</p><blockquote><strong><em>Anomaly Detected</em></strong><em><br>IP: 3.87.xxx.xxx<br>Reason: Unusual spike in requests<br>Action: Flagged for monitoring</em></blockquote><p>This ensures I don’t have to constantly watch logs manually.</p><h3>5. Taking Action (Blocking Suspicious Users)</h3><p>In some cases, detection alone isn’t enough.</p><p>If a user clearly behaves like a threat, the system can:</p><ul><li>Flag the IP</li><li>Temporarily block it</li><li>Prevent further requests</li></ul><p>This adds a layer of automated protection.</p><p>Instead of reacting manually, the system responds instantly.</p><h3>6. Monitoring Dashboard</h3><p>To make everything visible, I added a simple dashboard.</p><p>This dashboard shows:</p><ul><li>System status (running or not)</li><li>Number of flagged or blocked IPs</li><li>Overall health of the system</li></ul><p>It provides a quick way to understand what’s happening without digging through logs.</p><h3>Tools and Technologies Used</h3><p>Here’s what powered the system:</p><ul><li><strong>Nginx</strong> → Handles incoming traffic</li><li><strong>Python</strong> → Processes logs and detects anomalies</li><li><strong>Flask</strong> → Powers the dashboard</li><li><strong>Docker</strong> → Runs services in isolated containers</li><li><strong>Slack Webhooks</strong> → Sends real-time alerts</li><li><strong>AWS EC2</strong> → Hosts the entire system in the cloud</li></ul><p>Each tool plays a specific role, working together as a complete pipeline.</p><h3>Testing the System (Simulating an Attack)</h3><p>To confirm everything works, I needed to simulate suspicious behavior.</p><p>So I created a simple test where I sent many rapid requests to the server:</p><pre>for i in {1..200}; do curl http://YOUR_SERVER_IP; done</pre><p>This mimics what an automated script or bot might do.</p><h3>What Happened During Testing</h3><p>The results were exactly what I hoped for:</p><ul><li>The system detected the spike in traffic</li><li>The IP was flagged as suspicious</li><li>A Slack alert was sent instantly</li><li>The dashboard reflected the change</li></ul><p>Everything worked in real time, without manual intervention.</p><h3>Proof of Functionality</h3><p>To validate the system, I included screenshots of:</p><ul><li>The dashboard running in the browser</li><li>Slack alert messages</li><li>Nginx logs showing incoming requests</li><li>Docker containers running all services</li><li>Terminal showing simulated attack</li></ul><p>These serve as evidence that the system is fully functional.</p><h3>Why This Project Is Important</h3><p>In real-world systems, problems don’t always announce themselves.</p><p>Without monitoring:</p><ul><li>Attacks can go unnoticed</li><li>Systems can crash unexpectedly</li><li>Performance can degrade silently</li></ul><p>This project demonstrates how to:</p><ul><li>Detect issues early</li><li>Respond quickly</li><li>Automate protection</li></ul><p>It’s not just about building a system — it’s about building <strong>resilience</strong>.</p><h3>Future Improvements</h3><p>If I were to take this further, I would:</p><ul><li>Introduce smarter detection using machine learning</li><li>Add visual dashboards with charts and graphs</li><li>Scale the system using Kubernetes</li><li>Store historical data for deeper analysis</li></ul><p>This would transform it from a simple monitoring tool into a full security platform.</p><h3>Conclusion</h3><p>This project helped me understand how real-world systems:</p><ul><li>Monitor activity</li><li>Detect unusual patterns</li><li>React to threats automatically</li></ul><p>At its core, this system does four things:</p><p>It watches<br>It detects<br>It alerts<br>It responds</p><p>All in real time.</p><h3>Links</h3><ul><li>GitHub Repo: <a href="https://github.com/Shakurrrr/HNG14-Stage3">https://github.com/Shakurrrr/HNG14-Stage3</a></li><li>Live Dashboard: <a href="http://3.87.179.16:5000/">http://3.87.179.16:5000</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa372f89fc05" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[EdgeOps Platform]]></title>
            <link>https://medium.com/@shehuyusuf/edgeops-platform-d3c8c3b2e0cc?source=rss-607ba1670d2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3c8c3b2e0cc</guid>
            <dc:creator><![CDATA[Shehu Yusuf]]></dc:creator>
            <pubDate>Sat, 10 Jan 2026 19:11:05 GMT</pubDate>
            <atom:updated>2026-01-10T19:11:05.010Z</atom:updated>
            <content:encoded><![CDATA[<p><em>I deployed a lightweight reference application (EdgeOps Dashboard) to validate platform release strategies under real HTTP traffic conditions.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B5RD6lHesF1A2Beg_SuccQ.png" /></figure><h4>Day 1 — Building the Reference Workload and Repository Foundation</h4><h4>Introduction</h4><p>Most DevOps portfolio projects focus on provisioning infrastructure once and stopping there. The difference between infrastructure scripting and platform engineering is operational reliability: safe deployments, progressive delivery, observability, governance, and rollback under real production traffic.</p><p>EdgeOps Platform is a deployment platform I built to demonstrate those platform engineering capabilities in a practical, verifiable way. The platform is designed to support:</p><ul><li>blue/green and canary deployment strategies</li><li>automated rollback triggered by telemetry</li><li>drift detection with automated issue creation</li><li>policy-as-code enforcement for security and cost guardrails</li><li>multi-environment promotion (dev → staging → prod)</li><li>observability across logs, metrics, and traces</li></ul><p>Day 1 focuses on two outcomes:</p><ul><li>building the reference application that will be deployed and validated through EdgeOps Platform</li><li>establishing a repository structure that supports rapid iteration across application, infrastructure, and governance layers</li></ul><p>This reference workload is not meant to be feature-heavy. Its job is to behave deterministically under rollout conditions so we can prove traffic shifting, rollback behavior, and monitoring accuracy.</p><h4>Goals for Day 1</h4><p>By the end of Day 1, my project will have:</p><ul><li>a frontend dashboard (Next.js) that shows environment and API version identity</li><li>a backend API (Node.js Express) with health and version endpoints</li><li>a repository foundation that supports:</li><li>multi-environment Terraform infrastructure</li><li>CI/CD workflows</li><li>policy-as-code integration</li><li>documentation and architecture artifacts</li></ul><h4>Repository Structure</h4><p>A mono repo structure is used during implementation because it reduces integration overhead and allows infrastructure outputs and application deployments to stay tightly coordinated.</p><pre>edgeops-platform/<br>├── apps/<br>│   ├── dashboard/                # Next.js frontend<br>│   └── api/                      # Node.js Express backend<br>├── infra/<br>│   ├── modules/                  # reusable Terraform modules (later)<br>│   ├── envs/<br>│   │   ├── dev/<br>│   │   ├── staging/<br>│   │   └── prod/<br>│   └── README.md<br>├── policies/                     # OPA policies (Day 3)<br>├── scripts/                      # helper scripts (traffic generator, etc.)<br>├── .github/<br>│   └── workflows/<br>│       ├── ci.yml<br>│       └── terraform-plan.yml<br>├── docs/<br>│   ├── architecture.md<br>│   └── medium/<br>│       └── day-01.md<br>├── README.md<br>└── .gitignore</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BCiWGQNzHZDVwg705MqBOA.png" /><figcaption>clear architecture in vscode</figcaption></figure><p>This structure is intentionally aligned with platform engineering workflows:</p><ul><li>application code is deployable by automation</li><li>infrastructure is expressed as modules and per-environment stacks</li><li>policies are versioned alongside infrastructure</li><li>documentation is treated as part of the deliverable</li></ul><h4>Reference Workload Design</h4><p>The reference workload is called <strong>EdgeOps Dashboard</strong> and consists of:</p><ul><li>a Next.js frontend dashboard</li><li>a Node.js Express API backend</li></ul><p>The workload is explicitly designed to support rollout validation.</p><h4>Backend: Node.js Express API</h4><h4>Why this API is required</h4><p>To prove blue/green and canary deployment behavior, the platform needs an endpoint that responds with deployment identity information, including version and deployment role.</p><p>This prevents “invisible” deployments where traffic shifting cannot be measured.</p><h4>API endpoints</h4><p>Two endpoints will be implemented first:</p><ul><li>GET /api/health</li><li>GET /api/version</li></ul><h4>Version identity contract</h4><p>The /api/version response must include:</p><ul><li>service name</li><li>version</li><li>deployment identity (stable, canary, blue, green)</li><li>commit SHA</li><li>environment</li><li>timestamp</li><li>request ID</li></ul><p>Example response:</p><pre>{<br>  &quot;service&quot;: &quot;edgeops-api&quot;,<br>  &quot;version&quot;: &quot;v0.1.0&quot;,<br>  &quot;deployment&quot;: &quot;stable&quot;,<br>  &quot;commit&quot;: &quot;local-dev&quot;,<br>  &quot;environment&quot;: &quot;dev&quot;,<br>  &quot;timestamp&quot;: &quot;2026-01-03T10:00:00Z&quot;,<br>  &quot;request_id&quot;: &quot;abc123&quot;<br>}</pre><h4>API implementation</h4><p>Dependencies:</p><pre>npm init -y<br>npm install express cors dotenv<br>npm install --save-dev nodemon<br>npm install next react react-dom<br>npm install -D typescript @types/react @types/react-dom @types/node<br>npx tsc --init</pre><p>apps/api/src/server.js:</p><pre>const express = require(&quot;express&quot;);<br>const cors = require(&quot;cors&quot;);<br>const crypto = require(&quot;crypto&quot;);<br>const app = express();<br>app.use(cors());<br>app.use(express.json());<br>const SERVICE_NAME = process.env.SERVICE_NAME || &quot;edgeops-api&quot;;<br>const VERSION = process.env.VERSION || &quot;v0.1.0&quot;;<br>const DEPLOYMENT = process.env.DEPLOYMENT || &quot;stable&quot;;<br>const ENVIRONMENT = process.env.ENVIRONMENT || &quot;dev&quot;;<br>const COMMIT = process.env.COMMIT || &quot;local-dev&quot;;<br>function payload() {<br>  return {<br>    service: SERVICE_NAME,<br>    version: VERSION,<br>    deployment: DEPLOYMENT,<br>    commit: COMMIT,<br>    environment: ENVIRONMENT,<br>    timestamp: new Date().toISOString(),<br>    request_id: crypto.randomUUID()<br>  };<br>}<br>app.get(&quot;/api/health&quot;, (req, res) =&gt; {<br>  res.status(200).json({ status: &quot;ok&quot;, ...payload() });<br>});<br>app.get(&quot;/api/version&quot;, (req, res) =&gt; {<br>  res.status(200).json(payload());<br>});<br>const port = process.env.PORT || 3001;<br>app.listen(port, () =&gt; {<br>  console.log(`API running on port ${port}`);<br>});</pre><p>Test locally:</p><pre>curl http://localhost:3001/api/version</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JRboYCLwaio9YqRNeV-57w.png" /><figcaption>server is running</figcaption></figure><h4>Frontend: Next.js EdgeOps Dashboard</h4><h4>Purpose of the dashboard</h4><p>The dashboard provides an immediate validation surface. When we perform canary or blue/green rollouts, the UI will show:</p><ul><li>which environment is active</li><li>the API health status</li><li>the version identity currently being served</li><li>the rollout state (stable vs canary traffic)</li><li>This enables fast and repeatable demonstrations.</li></ul><h4>UI pages implemented on Day 1</h4><ul><li>Dashboard page (/)</li><li>Canary validation page (/canary) as a scaffold</li></ul><h4>Dashboard implementation (simplified)</h4><p>The dashboard reads the backend API and renders health and version identity.</p><p>apps/dashboard/app/page.tsx:</p><pre>&quot;use client&quot;;<br><br>import { useEffect, useMemo, useState } from &quot;react&quot;;<br><br>type ApiStatus = {<br>  status?: string;<br>  service?: string;<br>  version?: string;<br>  deployment?: string;<br>  commit?: string;<br>  environment?: string;<br>  timestamp?: string;<br>  request_id?: string;<br>};<br><br>type FrontendMarker = {<br>  color?: string;<br>};<br><br>function Badge({ text }: { text: string }) {<br>  const base =<br>    &quot;inline-flex items-center rounded-full px-3 py-1 text-xs font-medium ring-1 ring-inset&quot;;<br><br>  if (text === &quot;stable&quot;) {<br>    return (<br>      &lt;span<br>        className={`${base} bg-emerald-50 text-emerald-700 ring-emerald-600/20`}<br>      &gt;<br>        STABLE<br>      &lt;/span&gt;<br>    );<br>  }<br><br>  if (text === &quot;canary&quot;) {<br>    return (<br>      &lt;span className={`${base} bg-amber-50 text-amber-800 ring-amber-600/20`}&gt;<br>        CANARY<br>      &lt;/span&gt;<br>    );<br>  }<br><br>  return (<br>    &lt;span className={`${base} bg-gray-100 text-gray-700 ring-gray-300`}&gt;<br>      {text.toUpperCase()}<br>    &lt;/span&gt;<br>  );<br>}<br><br>............check my github repo for the full code.</pre><p>Check my github repo or the full code below.</p><p><a href="https://github.com/Shakurrrr/edgeops-platform/blob/main/apps/dashboard/app/page.tsx">https://github.com/Shakurrrr/edgeops-platform/blob/main/apps/dashboard/app/page.tsx</a></p><h4>CI Foundation (GitHub Actions)</h4><p>A baseline CI workflow is included to ensure:</p><ul><li>API runs checks (lint/test)</li><li>dashboard builds successfully</li></ul><p>.github/workflows/ci.yml:</p><pre>name: CI<br><br>on:<br>  pull_request:<br>  push:<br>    branches: [ &quot;main&quot; ]<br><br>jobs:<br>  api:<br>    runs-on: ubuntu-latest<br>    defaults:<br>      run:<br>        working-directory: apps/api<br>    steps:<br>      - uses: actions/checkout@v4<br>      - uses: actions/setup-node@v4<br>        with:<br>          node-version: 20<br>      - run: npm ci<br><br>  dashboard:<br>    runs-on: ubuntu-latest<br>    defaults:<br>      run:<br>        working-directory: apps/dashboard<br>    steps:<br>      - uses: actions/checkout@v4<br>      - uses: actions/setup-node@v4<br>        with:<br>          node-version: 20<br>      - run: npm ci<br>      - run: npm run build</pre><p>This workflow establishes the baseline quality checks that later deployment workflows will build on.</p><p><strong>Day 1 outcome: a workload designed for progressive delivery validation.</strong><br> The API exposes stable identity via /api/version, and the dashboard makes rollouts observable by default. The monorepo structure aligns application, infrastructure, and governance so that future changes remain coordinated and automatable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vxXu8r9gEjFXj9n6CCwbKg.png" /></figure><p>This closes the common DevOps portfolio gap where infrastructure exists but cannot be safely operated under release conditions.</p><h4>Day 2 — Deploying the Dev Environment (Terraform Remote State, CloudFront, S3, API Gateway, Lambda)</h4><h4>Overview</h4><p>Day 2 introduces the first production-style platform milestone: provisioning a complete dev environment on AWS using Terraform, with remote state enabled, and deploying the reference workload through an edge-first architecture.</p><p>This environment implements a standard production pattern for modern web platforms:</p><ul><li>the dashboard is hosted in S3 and served globally through CloudFront</li><li>the API is deployed as a serverless Lambda function and exposed through API Gateway</li><li>CloudFront routes /api/* requests to API Gateway and serves all other requests from S3</li></ul><p>This design establishes the data plane that EdgeOps Platform will later use to implement progressive delivery (canary and blue/green), observability, rollback automation, and policy enforcement.</p><h4>Goals for Day 2</h4><p>By the end of Day 2, the following must be true:</p><ul><li>Terraform remote state is enabled using:</li><li>an existing S3 bucket for state storage</li><li>a DynamoDB lock table for safe concurrency</li><li>a complete dev environment is provisioned through Terraform</li><li>the frontend is publicly accessible through CloudFront</li><li>the backend API is publicly accessible through API Gateway</li><li>CloudFront routes requests correctly:</li><li>/ → S3 (dashboard)</li><li>/api/* → API Gateway (API)</li><li>infrastructure outputs are exported for automation workflows</li><li>the foundation exists for progressive delivery (canary and blue/green) introduced later</li></ul><h4>Architecture: Dev Environment</h4><h4>Components</h4><ul><li>S3 bucket for dashboard hosting</li><li>CloudFront distribution as the edge ingress</li><li>API Gateway HTTP API for /api/* routing</li><li>Lambda function for Node.js API runtime</li><li>IAM roles and permissions for Lambda execution</li><li>Terraform remote backend:</li><li>existing state bucket: tf-state-559938827680-devops</li><li>DynamoDB lock table: edgeops-tf-lock</li></ul><h4>Traffic Flow</h4><ul><li>GET / is served by CloudFront using the S3 origin.</li><li>GET /api/* is routed by CloudFront to the API Gateway origin.</li><li>API Gateway invokes the Lambda function.</li><li>Lambda responds with JSON containing version identity (service, version, deployment, environment, request_id).</li></ul><h4>Implementation Steps</h4><p>Day 2 is split into two Terraform stacks:</p><h4>1. Bootstrap stack</h4><ul><li>creates the Terraform DynamoDB lock table</li><li>remote state bucket already exists and will be reused</li></ul><h4>2. Dev environment stack</h4><ul><li>provisions S3 + CloudFront + API Gateway + Lambda</li><li>outputs service endpoints for validation and automation</li></ul><h4>Step 1 — Bootstrap Terraform Remote State (Lock Table Only)</h4><p>Because we are reusing an existing state bucket, the bootstrap stack only creates the DynamoDB lock table.</p><p>Create the folder structure:</p><pre>infra/bootstrap/<br>├── main.tf<br>├── variables.tf<br>└── outputs.tf</pre><h4>infra/bootstrap/main.tf</h4><pre>terraform {<br>  required_version = &quot;&gt;= 1.5.0&quot;<br>}<br>provider &quot;aws&quot; {<br>  region = var.aws_region<br>}<br>resource &quot;aws_dynamodb_table&quot; &quot;tf_lock&quot; {<br>  name         = var.lock_table_name<br>  billing_mode = &quot;PAY_PER_REQUEST&quot;<br>  hash_key     = &quot;LockID&quot;<br>attribute {<br>    name = &quot;LockID&quot;<br>    type = &quot;S&quot;<br>  }<br>}</pre><h4>infra/bootstrap/variables.tf</h4><pre>variable &quot;aws_region&quot; {<br>  type    = string<br>  default = &quot;us-east-1&quot;<br>}<br>variable &quot;lock_table_name&quot; {<br>  type    = string<br>  default = &quot;edgeops-tf-lock&quot;<br>}</pre><h4>infra/bootstrap/outputs.tf</h4><pre>output &quot;lock_table_name&quot; {<br>  value = aws_dynamodb_table.tf_lock.name<br>}</pre><p>Run:</p><pre>cd infra/bootstrap<br>terraform init<br>terraform apply</pre><p>Expected output:</p><ul><li>DynamoDB lock table created: edgeops-tf-lock</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/1*ecNcq6ZM36VbJPF7zj2CKw.png" /></figure><h4>Step 2 — Create the Dev Environment Terraform Stack</h4><p>Create this structure:</p><pre>infra/envs/dev/<br>├── main.tf<br>├── variables.tf<br>├── outputs.tf<br>├── iam.tf<br>├── lambda.tf<br>├── apigw.tf<br>├── s3.tf<br>└── cloudfront.tf</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*41BcqIqwoEvgPzm2q4N_Rw.png" /></figure><h4>Step 2.1 — Configure Remote Backend (Dev)</h4><p>Edit infra/envs/dev/main.tf:</p><pre>terraform {<br>  backend &quot;s3&quot; {<br>    bucket         = &quot;tf-state-559938827680-devops&quot;<br>    key            = &quot;edgeops/dev/terraform.tfstate&quot;<br>    region         = &quot;us-east-1&quot;<br>    dynamodb_table = &quot;edgeops-tf-lock&quot;<br>    encrypt        = true<br>  }<br>required_version = &quot;&gt;= 1.5.0&quot;<br>}<br>provider &quot;aws&quot; {<br>  region = var.aws_region<br>}<br>data &quot;aws_caller_identity&quot; &quot;current&quot; {}</pre><p>Then run:</p><pre>cd infra/envs/dev<br>terraform init</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SDYFdHCgO9disn-rPypt5g.png" /></figure><h4>Step 3 — Make the Node.js API Lambda-Compatible</h4><p>The API was originally implemented as a local Express server. Lambda requires a handler function. The correct approach is to wrap Express using serverless-http.</p><p>Install dependency:</p><pre>cd apps/api<br>npm install serverless-http</pre><p>Update apps/api/src/server.js:</p><pre>const express = require(&quot;express&quot;);<br>const cors = require(&quot;cors&quot;);<br>const crypto = require(&quot;crypto&quot;);<br>const serverless = require(&quot;serverless-http&quot;);<br><br>const app = express();<br>app.use(cors());<br>app.use(express.json());<br><br>const SERVICE_NAME = process.env.SERVICE_NAME || &quot;edgeops-api&quot;;<br>const VERSION = process.env.VERSION || &quot;v0.1.0&quot;;<br>const DEPLOYMENT = process.env.DEPLOYMENT || &quot;stable&quot;;<br>const ENVIRONMENT = process.env.ENVIRONMENT || &quot;dev&quot;;<br>const COMMIT = process.env.COMMIT || &quot;local-dev&quot;;<br><br>function payload() {<br>  return {<br>    service: SERVICE_NAME,<br>    version: VERSION,<br>    deployment: DEPLOYMENT,<br>    commit: COMMIT,<br>    environment: ENVIRONMENT,<br>    timestamp: new Date().toISOString(),<br>    request_id: crypto.randomUUID(),<br>  };<br>}<br><br>/**<br> * Structured logging helper (JSON logs for CloudWatch queryability)<br> */<br>function logEvent(eventType, data) {<br>  console.log(<br>    JSON.stringify({<br>      event_type: eventType,<br>      ...data,<br>    })<br>  );<br>}<br><br>app.get(&quot;/api/health&quot;, (req, res) =&gt; {<br>  const p = { status: &quot;ok&quot;, ...payload() };<br><br>  logEvent(&quot;api_health&quot;, {<br>    path: req.path,<br>    method: req.method,<br>    user_agent: req.headers[&quot;user-agent&quot;],<br>    ...p,<br>  });<br><br>  res.status(200).json(p);<br>});<br><br>app.get(&quot;/api/version&quot;, (req, res) =&gt; {<br>  const p = payload();<br><br>  logEvent(&quot;api_version&quot;, {<br>    path: req.path,<br>    method: req.method,<br>    user_agent: req.headers[&quot;user-agent&quot;],<br>    ...p,<br>  });<br><br>  res.status(200).json(p);<br>});<br><br>if (require.main === module) {<br>  const port = process.env.PORT || 3001;<br>  app.listen(port, () =&gt; console.log(`API running on port ${port}`));<br>}<br><br>module.exports.handler = serverless(app);</pre><p>This ensures the same code runs both locally and in AWS Lambda.</p><h4>Step 4 — Package the API as a Lambda ZIP Artifact</h4><p>We deploy the API as a zip artifact that Terraform uploads to Lambda.</p><p>Create the packaging script:</p><p>scripts/package-api.sh</p><pre>#!/usr/bin/env bash<br>set -euo pipefail<br>rm -rf dist<br>mkdir -p dist<br>pushd apps/api &gt;/dev/null<br>npm install --omit=dev<br>zip -r ../../dist/api.zip . -x &quot;*.git*&quot; -x &quot;node_modules/.cache/*&quot;<br>popd &gt;/dev/null<br>echo &quot;Created dist/api.zip&quot;</pre><p>Make it executable:</p><pre>chmod +x scripts/package-api.sh</pre><p>Run:</p><pre>./scripts/package-api.sh</pre><p>This generates:</p><ul><li>dist/api.zip</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/309/1*B7c3wnoclQSBebhtWRCv8g.png" /></figure><p>Terraform will deploy this file into Lambda.</p><h4>Step 5 — Provision Dev API Infrastructure (IAM + Lambda + API Gateway)</h4><p>This section provisions:</p><ul><li>Lambda function running the Node.js API</li><li>IAM role for Lambda execution</li><li>API Gateway HTTP API routing /api/* requests to Lambda</li></ul><p>Terraform files:</p><ul><li>infra/envs/dev/iam.tf</li><li>infra/envs/dev/lambda.tf</li><li>infra/envs/dev/apigw.tf</li></ul><p>These will define the serverless backend stack for the dev environment.</p><h4>Step 6 — Provision Dev Frontend Infrastructure (S3 + CloudFront)</h4><p>This section provisions:</p><ul><li>S3 bucket for Next.js build artifacts</li><li>CloudFront distribution with:</li><li>default behavior: S3 origin</li><li>/api/* behavior: API Gateway origin</li><li>origin access control (OAC) so the S3 bucket remains private</li><li>correct caching policies for frontend and API</li></ul><p>Terraform files:</p><ul><li>infra/envs/dev/s3.tf</li><li>infra/envs/dev/cloudfront.tf</li></ul><h4>1) infra/envs/dev/variables.tf</h4><p>let’s use this exact version:</p><pre>variable &quot;environment&quot; {<br>  description = &quot;The deployment environment&quot;<br>  type        = string<br>  default     = &quot;dev&quot;<br>}<br>variable &quot;aws_region&quot; {<br>  description = &quot;AWS region for deployment&quot;<br>  type        = string<br>  default     = &quot;us-east-1&quot;<br>}<br>variable &quot;project&quot; {<br>  description = &quot;Project name&quot;<br>  type        = string<br>  default     = &quot;edgeops&quot;<br>}</pre><h4>2) infra/envs/dev/main.tf</h4><pre>terraform {<br>  backend &quot;s3&quot; {<br>    bucket         = &quot;tf-state-559938827680-devops&quot;<br>    key            = &quot;edgeops/dev/terraform.tfstate&quot;<br>    region         = &quot;us-east-1&quot;<br>    dynamodb_table = &quot;edgeops-tf-lock&quot;<br>    encrypt        = true<br>  }<br>required_version = &quot;&gt;= 1.5.0&quot;<br>  required_providers {<br>    aws = {<br>      source  = &quot;hashicorp/aws&quot;<br>      version = &quot;~&gt; 6.20&quot;<br>    }<br>  }<br>}<br>provider &quot;aws&quot; {<br>  region = var.aws_region<br>}<br>data &quot;aws_caller_identity&quot; &quot;current&quot; {}</pre><h4>3) infra/envs/dev/s3.tf (Dashboard Bucket)</h4><pre>resource &quot;aws_s3_bucket&quot; &quot;dashboard&quot; {<br>  bucket = &quot;${var.project}-${var.environment}-dashboard-${data.aws_caller_identity.current.account_id}&quot;<br>}<br>resource &quot;aws_s3_bucket_versioning&quot; &quot;dashboard&quot; {<br>  bucket = aws_s3_bucket.dashboard.id<br>  versioning_configuration {<br>    status = &quot;Enabled&quot;<br>  }<br>}<br>resource &quot;aws_s3_bucket_public_access_block&quot; &quot;dashboard&quot; {<br>  bucket                  = aws_s3_bucket.dashboard.id<br>  block_public_acls       = true<br>  block_public_policy     = true<br>  ignore_public_acls      = true<br>  restrict_public_buckets = true<br>}</pre><h4>4) infra/envs/dev/iam.tf (Lambda Execution Role)</h4><pre>resource &quot;aws_iam_role&quot; &quot;lambda_exec&quot; {<br>  name = &quot;${var.project}-${var.environment}-lambda-exec&quot;<br>  assume_role_policy = jsonencode({<br>    Version = &quot;2012-10-17&quot;,<br>    Statement = [{<br>      Action = &quot;sts:AssumeRole&quot;,<br>      Effect = &quot;Allow&quot;,<br>      Principal = { Service = &quot;lambda.amazonaws.com&quot; }<br>    }]<br>  })<br>}<br>resource &quot;aws_iam_role_policy_attachment&quot; &quot;lambda_basic&quot; {<br>  role       = aws_iam_role.lambda_exec.name<br>  policy_arn = &quot;arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole&quot;<br>}</pre><h4>5) infra/envs/dev/lambda.tf (API Lambda Function)</h4><p>This assumes you generated the ZIP at dist/api.zip.</p><pre>resource &quot;aws_lambda_function&quot; &quot;api&quot; {<br>  function_name = &quot;${var.project}-${var.environment}-api&quot;<br>  role          = aws_iam_role.lambda_exec.arn<br>  handler       = &quot;src/server.handler&quot;<br>  runtime       = &quot;nodejs20.x&quot;<br>  assume_role_policy = jsonencode({<br>    Version = &quot;2012-10-17&quot;,<br>    Statement = [{<br>      Action = &quot;sts:AssumeRole&quot;,<br>      Effect = &quot;Allow&quot;,<br>      Principal = { Service = &quot;lambda.amazonaws.com&quot; }<br>    }]<br>  })<br>}<br>resource &quot;aws_iam_role_policy_attachment&quot; &quot;lambda_basic&quot; {<br>  role       = aws_iam_role.lambda_exec.name<br>  policy_arn = &quot;arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole&quot;<br>}</pre><h4>6) infra/envs/dev/apigw.tf (API Gateway HTTP API)</h4><pre>resource &quot;aws_apigatewayv2_api&quot; &quot;api&quot; {<br>  name          = &quot;${var.project}-${var.environment}-http-api&quot;<br>  protocol_type = &quot;HTTP&quot;<br>}<br>resource &quot;aws_apigatewayv2_integration&quot; &quot;lambda&quot; {<br>  api_id                 = aws_apigatewayv2_api.api.id<br>  integration_type       = &quot;AWS_PROXY&quot;<br>  integration_uri        = aws_lambda_function.api.arn<br>  payload_format_version = &quot;2.0&quot;<br>}<br>resource &quot;aws_apigatewayv2_route&quot; &quot;proxy&quot; {<br>  api_id    = aws_apigatewayv2_api.api.id<br>  route_key = &quot;ANY /api/{proxy+}&quot;<br>  target    = &quot;integrations/${aws_apigatewayv2_integration.lambda.id}&quot;<br>}<br>resource &quot;aws_apigatewayv2_route&quot; &quot;root_health&quot; {<br>  api_id    = aws_apigatewayv2_api.api.id<br>  route_key = &quot;GET /api/health&quot;<br>  target    = &quot;integrations/${aws_apigatewayv2_integration.lambda.id}&quot;<br>}<br>resource &quot;aws_apigatewayv2_stage&quot; &quot;default&quot; {<br>  api_id      = aws_apigatewayv2_api.api.id<br>  name        = &quot;$default&quot;<br>  auto_deploy = true<br>}<br>resource &quot;aws_lambda_permission&quot; &quot;apigw&quot; {<br>  statement_id  = &quot;AllowAPIGatewayInvoke&quot;<br>  action        = &quot;lambda:InvokeFunction&quot;<br>  function_name = aws_lambda_function.api.function_name<br>  principal     = &quot;apigateway.amazonaws.com&quot;<br>  source_arn    = &quot;${aws_apigatewayv2_api.api.execution_arn}/*/*&quot;<br>}</pre><h4>7) infra/envs/dev/cloudfront.tf (CloudFront with /api/* to API Gateway)</h4><p>This is the most important file for Day 2.</p><pre>resource &quot;aws_cloudfront_origin_access_control&quot; &quot;oac&quot; {<br>  name                              = &quot;${var.project}-${var.environment}-oac&quot;<br>  description                       = &quot;OAC for S3 dashboard bucket&quot;<br>  origin_access_control_origin_type = &quot;s3&quot;<br>  signing_behavior                  = &quot;always&quot;<br>  signing_protocol                  = &quot;sigv4&quot;<br>}<br>resource &quot;aws_cloudfront_distribution&quot; &quot;cdn&quot; {<br>  enabled             = true<br>  default_root_object = &quot;index.html&quot;<br>  origin {<br>    domain_name              = aws_s3_bucket.dashboard.bucket_regional_domain_name<br>    origin_id                = &quot;s3-dashboard&quot;<br>    origin_access_control_id = aws_cloudfront_origin_access_control.oac.id<br>  }<br>  origin {<br>    domain_name = replace(aws_apigatewayv2_api.api.api_endpoint, &quot;https://&quot;, &quot;&quot;)<br>    origin_id   = &quot;api-gateway&quot;<br>    custom_origin_config {<br>      http_port              = 80<br>      https_port             = 443<br>      origin_protocol_policy = &quot;https-only&quot;<br>      origin_ssl_protocols   = [&quot;TLSv1.2&quot;]<br>    }<br>  }<br>  default_cache_behavior {<br>    target_origin_id       = &quot;s3-dashboard&quot;<br>    viewer_protocol_policy = &quot;redirect-to-https&quot;<br>    allowed_methods = [&quot;GET&quot;, &quot;HEAD&quot;, &quot;OPTIONS&quot;]<br>    cached_methods  = [&quot;GET&quot;, &quot;HEAD&quot;, &quot;OPTIONS&quot;]<br>    forwarded_values {<br>      query_string = false<br>      cookies {<br>        forward = &quot;none&quot;<br>      }<br>    }<br>  }<br>  ordered_cache_behavior {<br>    path_pattern           = &quot;/api/*&quot;<br>    target_origin_id       = &quot;api-gateway&quot;<br>    viewer_protocol_policy = &quot;redirect-to-https&quot;<br>    allowed_methods = [&quot;GET&quot;, &quot;HEAD&quot;, &quot;OPTIONS&quot;, &quot;PUT&quot;, &quot;POST&quot;, &quot;PATCH&quot;, &quot;DELETE&quot;]<br>    cached_methods  = [&quot;GET&quot;, &quot;HEAD&quot;]<br>    forwarded_values {<br>      query_string = true<br>      headers      = [&quot;Authorization&quot;, &quot;Origin&quot;]<br>      cookies {<br>        forward = &quot;all&quot;<br>      }<br>    }<br>  }<br>  restrictions {<br>    geo_restriction {<br>      restriction_type = &quot;none&quot;<br>    }<br>  }<br>  viewer_certificate {<br>    cloudfront_default_certificate = true<br>  }<br>}</pre><h4>8) Add S3 Bucket Policy Allowing CloudFront OAC</h4><p>Add this at the end of s3.tf:</p><pre>resource &quot;aws_s3_bucket_policy&quot; &quot;dashboard_policy&quot; {<br>  bucket = aws_s3_bucket.dashboard.id<br>  policy = jsonencode({<br>    Version = &quot;2012-10-17&quot;,<br>    Statement = [<br>      {<br>        Sid = &quot;AllowCloudFrontServicePrincipalReadOnly&quot;,<br>        Effect = &quot;Allow&quot;,<br>        Principal = { Service = &quot;cloudfront.amazonaws.com&quot; },<br>        Action = [&quot;s3:GetObject&quot;],<br>        Resource = &quot;${aws_s3_bucket.dashboard.arn}/*&quot;,<br>        Condition = {<br>          StringEquals = {<br>            &quot;AWS:SourceArn&quot; = aws_cloudfront_distribution.cdn.arn<br>          }<br>        }<br>      }<br>    ]<br>  })<br>}</pre><p>Without this, CloudFront will return AccessDenied on the dashboard.</p><h4>9) infra/envs/dev/outputs.tf</h4><pre>output &quot;dashboard_bucket_name&quot; {<br>  value = aws_s3_bucket.dashboard.bucket<br>}<br>output &quot;cloudfront_domain_name&quot; {<br>  value = aws_cloudfront_distribution.cdn.domain_name<br>}<br>output &quot;api_gateway_url&quot; {<br>  value = aws_apigatewayv2_api.api.api_endpoint<br>}</pre><h4>Step 7 — Apply Terraform and Deploy the Environment</h4><p>Once Terraform files are complete, run:</p><pre>cd infra/envs/dev<br>terraform apply<br>terraform apply</pre><p>Expected outputs:</p><ul><li>CloudFront distribution domain name</li><li>API Gateway invoke URL</li><li>S3 bucket name</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IU6ipaaD3fEV2ufufBthFw.png" /></figure><h4>Deploy the Dashboard Build into S3</h4><p>Build the dashboard:</p><pre>cd apps\dashboard<br>npm install<br>npm run build<br>npm run export</pre><p>Upload the exported site to S3:</p><pre>aws s3 sync .\out s3://edgeops-dev-dashboard-559938827680 --delete</pre><p>Where YOUR_BUCKET_NAME is from terraform output dashboard_bucket_name.</p><h4>Validation Commands</h4><h4>API directly</h4><pre>curl https://9ejp8g49lk.execute-api.us-east-1.amazonaws.com/api/version</pre><h4>API through CloudFront</h4><pre>curl https://d30tfelv6kj27h.cloudfront.net/api/version</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OrITJ2Kv34UZCcp6lvn_TA.png" /></figure><h4>Dashboard</h4><p>Open in browser:</p><pre>https://d30tfelv6kj27h.cloudfront.net</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xb9nyaOA8Gb1BKnBupgkpw.png" /></figure><h4>Summary: Dev Environment Successfully Deployed</h4><p>CloudFront serves the dashboard from S3 and routes /api/* to API Gateway, which invokes a Lambda-hosted API. Remote state and locking make the environment safe to operate collaboratively. Most importantly, the platform now has a realistic “production shape” that later supports canary and blue/green without migrating to Kubernetes or redesigning the network edge.</p><h4>What Was Achieved</h4><ul><li>A Terraform-managed dev environment with remote state and locking.</li><li>A static frontend hosted in S3 and delivered globally through CloudFront.</li><li>A serverless backend deployed to AWS Lambda and exposed via API Gateway.</li><li>Edge-based routing using CloudFront behaviors:</li><li>/ routed to the S3 dashboard origin.</li><li>/api/* routed to the API Gateway origin.</li><li>End-to-end validation confirming:</li><li>API accessibility via API Gateway.</li><li>API accessibility through CloudFront routing.</li><li>Successful dashboard delivery via CloudFront.</li></ul><p>This setup mirrors a common real-world edge-first architecture used for modern web platforms, where static content and APIs are deployed independently but unified behind a single global entry point.</p><h4>Day 3 — CI/CD, Policy Enforcement, and Infrastructure Governance</h4><p>With the dev environment live and validated, Day 3 transitions the EdgeOps Platform from <strong>manually operated infrastructure</strong> to a <strong>governed, automated internal platform</strong>.</p><p>At this stage, infrastructure is no longer applied directly from a local machine.<br> Instead, GitHub Actions becomes the control plane responsible for:</p><ul><li>validating changes</li><li>enforcing platform standards</li><li>detecting drift</li><li>and safely applying infrastructure updates</li></ul><p>This is a critical shift from “infrastructure deployment” to <strong>platform operations</strong>.</p><h4>Day 3 Objectives</h4><p>By the end of Day 3, the following must be true:</p><ul><li>All Terraform execution is performed via GitHub Actions.</li><li>Pull requests automatically trigger Terraform validation and planning.</li><li>Infrastructure policies are enforced before any apply is allowed.</li><li>Drift detection runs on a schedule and reports unmanaged changes.</li><li>Every infrastructure change has an audit trail.</li></ul><h4>Architecture Evolution (Day 3)</h4><p>The EdgeOps Platform control plane now consists of:</p><ul><li>GitHub repository as the source of truth</li><li>GitHub Actions as the execution engine</li><li>Terraform for infrastructure definition</li><li>OPA / Conftest for policy enforcement</li><li>Scheduled workflows for drift detection</li></ul><p>This mirrors how internal platform teams operate infrastructure at scale.</p><h4>Repository Structure (Updated)</h4><p>Day 3 introduces CI/CD and policy directories.</p><pre>edgeops-platform/<br>├── apps/<br>│   ├── api/<br>│   └── dashboard/<br>├── infra/<br>│   ├── bootstrap/<br>│   └── envs/<br>│       ├── dev/<br>│       ├── staging/<br>│       └── prod/<br>├── policies/<br>│   ├── terraform/<br>│   │   ├── deny-public-s3.rego<br>│   │   ├── require-tags.rego<br>│   │   └── deny-expensive-resources.rego<br>├── .github/<br>│   └── workflows/<br>│       ├── terraform-plan.yml<br>│       ├── terraform-apply-dev.yml<br>│       └── drift-detection.yml<br>└── docs/<br>    └── medium/<br>        ├── day-01.md<br>        ├── day-02.md<br>        └── day-03.md</pre><h4>Step 1 — Terraform Validation and Planning Workflow</h4><p>The first workflow ensures every infrastructure change is validated before merge.</p><h4>Workflow Purpose</h4><ul><li>Run Terraform formatting and validation</li><li>Generate a Terraform plan</li><li>Enforce policies using OPA</li><li>Prevent invalid or unsafe infrastructure from being merged</li></ul><h4>.github/workflows/terraform-plan.yml</h4><pre>name: Terraform Plan (Dev)<br><br>on:<br>  pull_request:<br>    paths:<br>      - &quot;infra/**&quot;<br>      - &quot;policies/**&quot;<br><br>permissions:<br>  id-token: write<br>  contents: read<br><br>jobs:<br>  terraform-plan:<br>    runs-on: ubuntu-latest<br><br>    permissions:<br>      contents: read<br>      pull-requests: write<br><br>    env:<br>      AWS_REGION: us-east-1<br><br>    steps:<br>      - name: Checkout repository<br>        uses: actions/checkout@v4<br><br>      - name: Setup Terraform<br>        uses: hashicorp/setup-terraform@v3<br>        with:<br>          terraform_version: 1.5.0<br><br>      - name: Configure AWS credentials<br>        uses: aws-actions/configure-aws-credentials@v4<br>        with:<br>          aws-region: us-east-1<br>          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}<br>          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}<br>          <br>      - name: Terraform Init<br>        working-directory: infra/envs/dev<br>        run: terraform init<br><br>      - name: Terraform Format<br>        working-directory: infra/envs/dev<br>        run: terraform fmt -check<br><br>      - name: Terraform Validate<br>        working-directory: infra/envs/dev<br>        run: terraform validate<br><br>      - name: Terraform Plan<br>        working-directory: infra/envs/dev<br>        run: terraform plan -out=tfplan.binary<br><br>      - name: Convert Plan to JSON<br>        working-directory: infra/envs/dev<br>        run: terraform show -json tfplan.binary &gt; tfplan.json<br><br>      - name: Install Conftest (OPA)<br>        run: |<br>          curl -L https://github.com/open-policy-agent/conftest/releases/download/v0.52.0/conftest_0.52.0_Linux_x86_64.tar.gz | tar xz<br>          sudo mv conftest /usr/local/bin<br><br>      - name: Run Policy Checks<br>        working-directory: infra/envs/dev<br>        run: conftest test tfplan.json -p ../../../policies/terraform<br><br>      - name: Upload Plan Artifact<br>        uses: actions/upload-artifact@v4<br>        with:<br>          name: terraform-plan-dev<br>          path: infra/envs/dev/tfplan.json</pre><p>This workflow ensures:</p><ul><li>Only valid Terraform reaches the main branch.</li><li>Plans are generated consistently.</li><li>Infrastructure changes are reviewable before apply.</li></ul><h4>Step 2 — Policy-as-Code with OPA / Conftest</h4><p>To operate infrastructure as a platform, standards must be enforced automatically.</p><p>EdgeOps uses <strong>OPA (Open Policy Agent)</strong> with <strong>Conftest</strong> to validate Terraform plans.</p><h4>Step 2.1 — Install Conftest in CI</h4><p>Add this step to the plan workflow <strong>after plan generation</strong>:</p><pre>- name: Install Conftest<br>        run: |<br>          curl -L https://github.com/open-policy-agent/conftest/releases/download/v0.52.0/conftest_0.52.0_Linux_x86_64.tar.gz | tar xz<br>          sudo mv conftest /usr/local/bin</pre><h4>Step 2.2 — Policy: Deny Public S3 Buckets</h4><p>policies/terraform/deny-public-s3.rego</p><pre>package terraform.security</pre><pre>deny[msg] {<br>  resource := input.resource_changes[_]<br>  resource.type == &quot;aws_s3_bucket&quot;<br>  resource.change.after.acl == &quot;public-read&quot;<br>  msg := &quot;Public S3 buckets are not allowed&quot;<br>}</pre><h4>Step 2.3 — Policy: Require Resource Tags</h4><p>policies/terraform/require-tags.rego</p><pre>package terraform.tags</pre><pre>deny[msg] {<br>  resource := input.resource_changes[_]<br>  not resource.change.after.tags<br>  msg := sprintf(&quot;Resource %s is missing required tags&quot;, [resource.name])<br>}</pre><h4>Step 2.4 — Policy: Deny Expensive Instance Types</h4><p>policies/terraform/deny-expensive-resources.rego</p><pre>package terraform.cost</pre><pre>deny[msg] {<br>  resource := input.resource_changes[_]<br>  resource.type == &quot;aws_instance&quot;<br>  resource.change.after.instance_type == &quot;m5.4xlarge&quot;<br>  msg := &quot;Instance type m5.4xlarge is not allowed&quot;<br>}</pre><h4>Step 2.5 — Run Policy Checks in CI</h4><p>Add to the workflow:</p><pre>- name: Run Policy Checks<br>        working-directory: infra/envs/dev<br>        run: conftest test tfplan.json -p ../../../policies/terraform</pre><p>If any policy fails, the pull request is blocked automatically.</p><h4>Step 3 — Automated Apply for Dev Environment</h4><p>Only validated and policy-compliant changes are allowed to deploy.</p><h4>.github/workflows/terraform-apply-dev.yml</h4><pre>name: Terraform Apply (Dev)<br><br>on:<br>  push:<br>    branches:<br>      - main<br>    paths:<br>      - &quot;infra/**&quot;<br>  workflow_dispatch:<br><br>jobs:<br>  terraform-apply:<br>    runs-on: ubuntu-latest<br><br>    environment:<br>      name: dev<br><br>    steps:<br>      - name: Checkout repository<br>        uses: actions/checkout@v4<br><br>      - name: Debug secrets presence (safe)<br>        shell: bash<br>        run: |<br>          if [ -z &quot;${{ secrets.AWS_ACCESS_KEY_ID }}&quot; ]; then<br>            echo &quot;AWS_ACCESS_KEY_ID is EMPTY&quot;<br>            exit 1<br>          else<br>            echo &quot;AWS_ACCESS_KEY_ID is present&quot;<br>          fi<br><br>          if [ -z &quot;${{ secrets.AWS_SECRET_ACCESS_KEY }}&quot; ]; then<br>            echo &quot;AWS_SECRET_ACCESS_KEY is EMPTY&quot;<br>            exit 1<br>          else<br>            echo &quot;AWS_SECRET_ACCESS_KEY is present&quot;<br>          fi<br><br>      - name: Configure AWS credentials<br>        uses: aws-actions/configure-aws-credentials@v4<br>        with:<br>          aws-region: us-east-1<br>          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}<br>          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}<br><br>      - name: Verify AWS identity<br>        run: aws sts get-caller-identity<br><br>      - name: Setup Terraform<br>        uses: hashicorp/setup-terraform@v3<br>        with:<br>          terraform_version: 1.7.5<br><br>      - name: Terraform Init<br>        working-directory: infra/envs/dev<br>        run: terraform init<br><br>      - name: Terraform Apply<br>        working-directory: infra/envs/dev<br>        run: terraform apply -auto-approve</pre><p>This establishes:</p><ul><li>automatic deployment to dev on merge</li><li>full audit trail via GitHub</li><li>zero manual applies</li></ul><h4>Step 4 — Drift Detection Workflow</h4><p>Infrastructure drift is inevitable. The platform must detect it automatically.</p><h4>.github/workflows/drift-detection.yml</h4><pre>name: Terraform Drift Detection</pre><pre>on:</pre><pre>schedule:</pre><pre>- cron: &quot;0 3 * * *&quot;</pre><pre>workflow_dispatch:</pre><pre>jobs:</pre><pre>drift-detection:</pre><pre>runs-on: ubuntu-latest</pre><pre># If your secrets are stored under Environment &quot;dev&quot;, this is required.</pre><pre>environment:</pre><pre>name: dev</pre><pre>steps:</pre><pre>- name: Checkout repository</pre><pre>uses: actions/checkout@v4</pre><pre>- name: Debug secrets presence (safe)</pre><pre>shell: bash</pre><pre>run: |</pre><pre>if [ -z &quot;${{ secrets.AWS_ACCESS_KEY_ID }}&quot; ]; then</pre><pre>echo &quot;AWS_ACCESS_KEY_ID is EMPTY&quot;</pre><pre>exit 1</pre><pre>else</pre><pre>echo &quot;AWS_ACCESS_KEY_ID is present&quot;</pre><pre>fi</pre><pre>if [ -z &quot;${{ secrets.AWS_SECRET_ACCESS_KEY }}&quot; ]; then</pre><pre>echo &quot;AWS_SECRET_ACCESS_KEY is EMPTY&quot;</pre><pre>exit 1</pre><pre>else</pre><pre>echo &quot;AWS_SECRET_ACCESS_KEY is present&quot;</pre><pre>fi</pre><pre>- name: Configure AWS credentials</pre><pre>uses: aws-actions/configure-aws-credentials@v4</pre><pre>with:</pre><pre>aws-region: us-east-1</pre><pre>aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}</pre><pre>aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}</pre><pre>- name: Verify AWS identity</pre><pre>run: aws sts get-caller-identity</pre><pre>- name: Setup Terraform</pre><pre>uses: hashicorp/setup-terraform@v3</pre><pre>- name: Terraform Init</pre><pre>working-directory: infra/envs/dev</pre><pre>run: terraform init</pre><pre>- name: Terraform Plan (Drift Check)</pre><pre>id: drift</pre><pre>working-directory: infra/envs/dev</pre><pre>shell: bash</pre><pre>run: |</pre><pre>set +e</pre><pre>terraform plan -detailed-exitcode -no-color</pre><pre>code=$?</pre><pre>echo &quot;exit_code=$code&quot; &gt;&gt; &quot;$GITHUB_OUTPUT&quot;</pre><pre>exit 0</pre><pre>- name: Report result</pre><pre>shell: bash</pre><pre>run: |</pre><pre>code=&quot;${{ steps.drift.outputs.exit_code }}&quot;</pre><pre>if [ &quot;$code&quot; = &quot;0&quot; ]; then</pre><pre>echo &quot;No drift detected.&quot;</pre><pre>exit 0</pre><pre>elif [ &quot;$code&quot; = &quot;2&quot; ]; then</pre><pre>echo &quot;Drift detected (plan exit code 2).&quot;</pre><pre>exit 0</pre><pre>else</pre><pre>echo &quot;Terraform plan failed (exit code $code).&quot;</pre><pre>exit 1</pre><pre>fi</pre><p>Exit codes:</p><ul><li>0 → no drift</li><li>2 → drift detected</li><li>1 → error</li></ul><p>This workflow can later be extended to:</p><ul><li>open GitHub issues</li><li>notify Slack</li><li>trigger remediation pipelines</li></ul><h4>Day 3 Summary</h4><p><strong>A governed control plane with policy enforcement and drift visibility.</strong><br>Pull requests generate Terraform plans and run OPA/Conftest checks before merge. Applies happen automatically on main, creating a complete audit trail. Drift detection runs on a schedule, proving that EdgeOps is intended to remain authoritative even when manual changes occur. This is the operational line between scripting and platform engineering.</p><h4>Day 4 — Canary Deployments and Progressive Traffic Routing (CloudFront + Lambda Aliases)</h4><p>Day 4 introduces the first “platform-grade” delivery capability in EdgeOps: <strong>canary releases with traffic splitting and safe rollback</strong>.</p><p>The goal is not just to deploy a new version, but to deploy it safely by:</p><ul><li>running two versions of the API simultaneously (stable + canary)</li><li>routing a defined percentage of traffic to canary</li><li>verifying behavior through deterministic responses</li><li>enabling fast rollback by shifting traffic back to stable</li></ul><p>This is the core of progressive delivery.</p><h4>Day 4 Objectives</h4><p>By the end of Day 4, the following must be true:</p><ul><li>The API is deployed as <strong>two active versions</strong>:</li><li>stable version (v1)</li><li>canary version (v2)</li><li>A <strong>traffic split</strong> exists:</li><li>example: 90% stable, 10% canary</li><li>Traffic split is adjustable through Terraform variables.</li><li>Canary behavior is observable through /api/version responses.</li><li>Rollback is immediate by setting canary weight back to 0%.</li></ul><h4>Day 4 Objectives</h4><p>By the end of Day 4, the following must be true:</p><ul><li>The API is deployed as <strong>two active versions</strong>:</li><li>stable version (v1)</li><li>canary version (v2)</li><li>A <strong>traffic split</strong> exists:</li><li>example: 90% stable, 10% canary</li><li>Traffic split is adjustable through Terraform variables.</li><li>Canary behavior is observable through /api/version responses.</li><li>Rollback is immediate by setting canary weight back to 0%.</li></ul><h4>Day 4 Design Choice: Where Traffic Splitting Happens</h4><p>There are multiple ways to implement canary delivery. For this project we implement it at the <strong>Lambda alias routing level</strong> because it is:</p><ul><li>production-realistic</li><li>low cost (no Kubernetes required)</li><li>supports weighted routing natively</li><li>easy to automate and roll back</li></ul><h4>Routing Structure</h4><ul><li>API Gateway integrates with a Lambda <strong>alias</strong> (stable alias).</li><li>The stable alias uses <strong>weighted routing</strong> to send a fraction of requests to the canary version.</li></ul><p>This means:</p><ul><li>API Gateway calls “stable”</li><li>Lambda decides whether to serve stable or canary based on configured weights</li></ul><h4>Architecture: Day 4 Canary Model</h4><h4>Components</h4><ul><li>Lambda function: edgeops-dev-api</li><li>Lambda versions:</li><li>version 1 = stable build</li><li>version 2 = canary build</li><li>Lambda alias:</li><li>alias stable points to version 1</li><li>alias routes additional % to version 2</li><li>API Gateway integration targets the alias ARN</li></ul><h4>Traffic Flow</h4><ol><li>CloudFront routes /api/* to API Gateway.</li><li>API Gateway invokes Lambda alias stable.</li><li>Lambda alias routes traffic:</li></ol><ul><li>N% to stable version</li><li>M% to canary version</li></ul><p>4. /api/version identifies which version served the request.</p><h4>Step 1 — Update the API to Return Version Identity</h4><p>We must ensure the API always returns clear identity fields so traffic splitting can be verified.</p><p>Update the API handler so it returns:</p><ul><li>environment</li><li>deployment type (stable/canary)</li><li>version string</li><li>request id</li></ul><h4>Example response format</h4><pre>{<br>  &quot;service&quot;: &quot;edgeops-api&quot;,<br>  &quot;environment&quot;: &quot;dev&quot;,<br>  &quot;deployment&quot;: &quot;canary&quot;,<br>  &quot;version&quot;: &quot;v0.2.0&quot;,<br>  &quot;request_id&quot;: &quot;abc123&quot;<br>}</pre><h4>Step 2 — Prepare Two Builds (Stable and Canary)</h4><p>We will build and deploy two different ZIP packages:</p><ul><li>stable build: v0.1.0 (current)</li><li>canary build: v0.2.0 (new)</li></ul><h4>2.1 Add a version marker to the API</h4><pre>import { VERSION, DEPLOYMENT, ENVIRONMENT, SERVICE_NAME } from &quot;./version&quot;;</pre><pre>export const handler = async () =&gt; {<br>  return {<br>    statusCode: 200,<br>    headers: { &quot;content-type&quot;: &quot;application/json&quot; },<br>    body: JSON.stringify({<br>      service: SERVICE_NAME,<br>      environment: ENVIRONMENT,<br>      deployment: DEPLOYMENT,<br>      version: VERSION,<br>      request_id: crypto.randomUUID()<br>    })<br>  };<br>};</pre><h4>Step 3 — Terraform Changes: Publish Lambda Versions</h4><p>Terraform must:</p><ul><li>deploy Lambda function code</li><li>publish versions</li><li>create alias with routing configuration</li></ul><p>We introduce:</p><ul><li>stable_version</li><li>canary_enabled</li><li>canary_weight</li></ul><h4>Step 4 — Add Canary Variables</h4><p>In infra/envs/dev/variables.tf add:</p><pre>variable &quot;canary_weight&quot; {<br>  description = &quot;Percent of traffic routed to canary (0-1 range). Example: 0.1 = 10%&quot;<br>  type        = number<br>  default     = 0.1<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D8XZUSod4nrB1Fc0ZJ3Ouw.png" /></figure><h4>Step 5 — Modify Lambda Terraform for Versioning and Alias Routing</h4><p>Edit infra/envs/dev/lambda.tf:</p><h4>5.1 Publish a Lambda version for stable</h4><p>Set publish = true so Terraform creates a numbered version each time code changes.</p><pre>resource &quot;aws_lambda_function&quot; &quot;api&quot; {<br>  function_name = &quot;edgeops-dev-api&quot;<br>  role          = aws_iam_role.lambda_exec.arn<br>  handler       = &quot;src/server.handler&quot;<br>  runtime       = &quot;nodejs20.x&quot;</pre><pre>  filename         = &quot;../../../dist/api.zip&quot;<br>  source_code_hash = filebase64sha256(&quot;../../../dist/api.zip&quot;)</pre><pre>  publish = true</pre><pre>  environment {<br>    variables = {<br>      SERVICE_NAME = &quot;edgeops-api&quot;<br>      VERSION      = &quot;v0.1.0&quot;<br>      DEPLOYMENT   = &quot;stable&quot;<br>      ENVIRONMENT  = &quot;dev&quot;<br>      COMMIT       = &quot;local&quot;<br>    }<br>  }<br>}</pre><h4>5.2 Create a stable alias pointing to the published version</h4><pre>resource &quot;aws_lambda_alias&quot; &quot;stable&quot; {<br>  name             = &quot;stable&quot;<br>  function_name    = aws_lambda_function.api.function_name<br>  function_version = aws_lambda_function.api.version<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e8yUJQLikS5Rr7KUzOOk7A.png" /></figure><h4>Step 6 — Create a Canary Version</h4><p>Canary requires a separate published version. We do this by deploying a second Lambda function resource that uses the same function name but changes environment variables. However, Lambda does not allow two resources with the same function name, so the correct approach is:</p><ul><li>update the code + env vars</li><li>publish a new version</li><li>route a portion of traffic to the new version using alias routing</li><li>This means the “canary version” is just the next published version.</li></ul><h4>Step 7 — Add Weighted Routing to the Alias</h4><p>Update the alias to include routing config:</p><pre>resource &quot;aws_lambda_alias&quot; &quot;stable&quot; {<br>  name             = &quot;stable&quot;<br>  function_name    = aws_lambda_function.api.function_name<br>  function_version = aws_lambda_function.api.version</pre><pre>  routing_config {<br>    additional_version_weights = {<br>      aws_lambda_function.api.version = var.canary_weight<br>    }<br>  }<br>}</pre><p>This is not correct yet because it routes to itself. We must route to a different version.</p><p>Therefore we need:</p><ul><li>stable version = previous version number</li><li>canary version = latest version number</li></ul><p>Terraform must track these separately.</p><h4>Step 8 — Correct Approach: Two Explicit Versions in Terraform</h4><p>We implement two deployments:</p><ul><li>stable artifact zip: dist/api-stable.zip</li><li>canary artifact zip: dist/api-canary.zip</li></ul><p>Then publish two distinct versions and route between them.</p><h4>Step 9 — Create Stable and Canary ZIP Artifacts</h4><p>Update scripts:</p><p>scripts/package-api-stable.ps1</p><pre>$ErrorActionPreference = &quot;Stop&quot;<br>if (Test-Path &quot;.\dist&quot;) { Remove-Item &quot;.\dist&quot; -Recurse -Force }<br>New-Item -ItemType Directory -Path &quot;.\dist&quot; | Out-Null</pre><pre>Set-Location &quot;.\apps\api&quot;<br>$env:VERSION=&quot;v0.1.0&quot;<br>$env:DEPLOYMENT=&quot;stable&quot;<br>npm install --omit=dev<br>Compress-Archive -Path * -DestinationPath &quot;..\..\dist\api-stable.zip&quot; -Force<br>Set-Location &quot;..\..&quot;</pre><p>scripts/package-api-canary.ps1</p><pre>$ErrorActionPreference = &quot;Stop&quot;<br>New-Item -ItemType Directory -Force -Path &quot;.\dist&quot; | Out-Null</pre><pre>Set-Location &quot;.\apps\api&quot;<br>$env:VERSION=&quot;v0.2.0&quot;<br>$env:DEPLOYMENT=&quot;canary&quot;<br>npm install --omit=dev<br>Compress-Archive -Path * -DestinationPath &quot;..\..\dist\api-canary.zip&quot; -Force<br>Set-Location &quot;..\..&quot;</pre><p>Run both:</p><pre>powershell -ExecutionPolicy Bypass -File .\scripts\package-api-stable.ps1<br>powershell -ExecutionPolicy Bypass -File .\scripts\package-api-canary.ps1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CvyOFYc2PYR9xjlTh11DFQ.png" /></figure><h4>Step 10 — Terraform: Deploy Stable and Canary Versions</h4><p>In lambda.tf:</p><h4>10.1 Deploy stable code</h4><pre>resource &quot;aws_lambda_function&quot; &quot;api&quot; {<br>  function_name = &quot;edgeops-dev-api&quot;<br>  role          = aws_iam_role.lambda_exec.arn<br>  handler       = &quot;src/server.handler&quot;<br>  runtime       = &quot;nodejs20.x&quot;</pre><pre>  filename         = &quot;../../../dist/api-stable.zip&quot;<br>  source_code_hash = filebase64sha256(&quot;../../../dist/api-stable.zip&quot;)</pre><pre>  publish = true</pre><pre>  environment {<br>    variables = {<br>      SERVICE_NAME = &quot;edgeops-api&quot;<br>      VERSION      = &quot;v0.1.0&quot;<br>      DEPLOYMENT   = &quot;stable&quot;<br>      ENVIRONMENT  = &quot;dev&quot;<br>    }<br>  }<br>}</pre><h4>10.2 Publish canary version by updating function and republishing (Terraform limitation)</h4><p>Terraform cannot publish two versions with different code for the same function in the same apply step without an explicit workflow.</p><p>Therefore, the correct platform approach is:</p><ul><li>stable is current deployed version</li><li>canary is next deployed version</li><li>alias routes to previous version (stable) and latest version (canary)</li></ul><p>We implement this by:</p><ul><li>stable alias pinned to a known version number (stored as variable)</li><li>canary version is the newly published version</li></ul><h4>Step 11 — Introduce a Stable Version Pin Variable</h4><p>In variables.tf add:</p><pre>variable &quot;stable_version&quot; {<br>  description = &quot;Pinned stable Lambda version&quot;<br>  type        = string<br>}</pre><p>In terraform.tfvars set:</p><pre>stable_version = &quot;1&quot;<br>canary_weight  = 0.1</pre><h4>Step 12 — Update Alias Routing Correctly</h4><pre>resource &quot;aws_lambda_alias&quot; &quot;stable&quot; {<br>  name             = &quot;stable&quot;<br>  function_name    = aws_lambda_function.api.function_name<br>  function_version = var.stable_version</pre><pre>  routing_config {<br>    additional_version_weights = {<br>      aws_lambda_function.api.version = var.canary_weight<br>    }<br>  }<br>}</pre><p>Meaning:</p><ul><li>alias stable points to pinned stable version (e.g., version 1)</li><li>traffic is routed additionally to latest published version (canary)</li></ul><h4>Step 13 — Update API Gateway Integration to Use Alias ARN</h4><p>Edit apigw.tf integration:</p><pre>resource &quot;aws_apigatewayv2_integration&quot; &quot;lambda&quot; {<br>  api_id                 = aws_apigatewayv2_api.api.id<br>  integration_type       = &quot;AWS_PROXY&quot;<br>  integration_uri        = aws_lambda_alias.stable.arn<br>  payload_format_version = &quot;2.0&quot;<br>}</pre><p>Also update Lambda permission to allow alias invoke:</p><pre>resource &quot;aws_lambda_permission&quot; &quot;apigw&quot; {<br>  statement_id  = &quot;AllowAPIGatewayInvoke&quot;<br>  action        = &quot;lambda:InvokeFunction&quot;<br>  function_name = aws_lambda_function.api.function_name<br>  principal     = &quot;apigateway.amazonaws.com&quot;<br>  source_arn    = &quot;${aws_apigatewayv2_api.api.execution_arn}/*/*&quot;<br>}</pre><h4>Step 14 — Apply Canary Configuration</h4><p>Run:</p><pre>cd infra\envs\dev<br>terraform apply</pre><h4>Step 15 — Validate Canary Traffic Split</h4><p>Run 50 requests and count stable vs canary responses.</p><p>PowerShell loop:</p><pre>$stable = 0<br>$canary = 0<br>$errors = 0<br><br>for ($i=0; $i -lt 50; $i++) {<br>  try {<br>    $resp = Invoke-RestMethod -Uri &quot;https://d30tfelv6kj27h.cloudfront.net/api/version&quot; -ErrorAction Stop<br>    if ($resp.deployment -eq &quot;stable&quot;) { $stable++ }<br>    elseif ($resp.deployment -eq &quot;canary&quot;) { $canary++ }<br>    else { $errors++ }<br>  } catch {<br>    $errors++<br>  }<br>}<br><br>Write-Host &quot;stable=$stable canary=$canary errors=$errors&quot;</pre><p>Expected:</p><ul><li>stable should be roughly 45</li><li>canary should be roughly 5 (10%)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0pWHJPtfbnKXCp2VV3arew.png" /></figure><h4>Step 16 — Rollback Procedure</h4><p>Rollback is simply:</p><ul><li>set canary weight to 0</li><li>apply</li></ul><p>Update terraform.tfvars:</p><pre>canary_weight = 0.0</pre><p>Then:</p><pre>terraform apply</pre><h4>Day 4 Summary</h4><p><strong>Configurable canary releases with observable behavior and immediate rollback.</strong><br> API Gateway targets a Lambda alias, and that alias routes a percentage of traffic to a newer version. Canary behavior is verifiable via deterministic /api/version identity responses under real traffic. Rollback becomes a configuration flip (weight → 0) rather than an emergency redeploy, which is the operational standard for safe delivery.</p><h4>Day 5 — Blue/Green Deployments for the Dashboard and Automated Rollback Controls</h4><p>Day 5 extends EdgeOps Platform from canary API delivery to <strong>blue/green deployments for the frontend dashboard</strong>. The dashboard is currently served from a single S3 origin behind CloudFront. This works, but it does not support safe releases.</p><p>A blue/green model ensures that:</p><ul><li>a new dashboard version can be deployed without replacing the current one</li><li>traffic can be switched instantly between two immutable deployments</li><li>rollback is immediate (switch back to the previous origin)</li><li>deployments become repeatable and reversible, which is a core platform capability</li></ul><h4>Day 5 Objectives</h4><p>By the end of Day 5, the following must be true:</p><ul><li>Two S3 buckets exist for the dashboard:</li><li>blue (current stable)</li><li>green (new release candidate)</li><li>CloudFront routes dashboard traffic (/) to either blue or green based on a single configuration value.</li><li>A deployment procedure exists:</li><li>upload build to inactive bucket</li><li>flip CloudFront origin to point to the new bucket</li><li>validate release</li><li>Rollback procedure exists:</li><li>flip CloudFront origin back immediately</li><li>The process is automated through GitHub Actions.</li></ul><h4>Architecture: Blue/Green Dashboard Delivery</h4><h4>Components</h4><ul><li>edgeops-dev-dashboard-blue-* S3 bucket</li><li>edgeops-dev-dashboard-green-* S3 bucket</li><li>CloudFront distribution:</li><li>default behavior → “active dashboard origin”</li><li>Origin Access Control (OAC) attached to both buckets</li><li>Bucket policies allowing CloudFront access</li></ul><h4>Traffic Flow</h4><ol><li>A user requests /.</li><li>CloudFront serves the dashboard from the currently active S3 bucket (blue or green).</li><li>/api/* continues to route to API Gateway as established in Day 2 and Day 4.</li></ol><h4>Step 1 — Extend Terraform to Provision Two Dashboard Buckets</h4><p>Edit:</p><p>infra/envs/dev/s3.tf</p><p>Replace your single dashboard bucket resource with <strong>two buckets</strong>.</p><h4>1.1 Blue bucket</h4><pre>resource &quot;aws_s3_bucket&quot; &quot;dashboard_blue&quot; {<br>  bucket = &quot;${var.project}-${var.environment}-dashboard-blue-${data.aws_caller_identity.current.account_id}&quot;<br>}<br>resource &quot;aws_s3_bucket_versioning&quot; &quot;dashboard_blue&quot; {<br>  bucket = aws_s3_bucket.dashboard_blue.id<br>  versioning_configuration {<br>    status = &quot;Enabled&quot;<br>  }<br>}<br>resource &quot;aws_s3_bucket_public_access_block&quot; &quot;dashboard_blue&quot; {<br>  bucket                  = aws_s3_bucket.dashboard_blue.id<br>  block_public_acls       = true<br>  block_public_policy     = true<br>  ignore_public_acls      = true<br>  restrict_public_buckets = true<br>}</pre><h4>1.2 Green bucket</h4><pre>resource &quot;aws_s3_bucket&quot; &quot;dashboard_green&quot; {<br>  bucket = &quot;${var.project}-${var.environment}-dashboard-green-${data.aws_caller_identity.current.account_id}&quot;<br>}<br>resource &quot;aws_s3_bucket_versioning&quot; &quot;dashboard_green&quot; {<br>  bucket = aws_s3_bucket.dashboard_green.id<br>  versioning_configuration {<br>    status = &quot;Enabled&quot;<br>  }<br>}<br>resource &quot;aws_s3_bucket_public_access_block&quot; &quot;dashboard_green&quot; {<br>  bucket                  = aws_s3_bucket.dashboard_green.id<br>  block_public_acls       = true<br>  block_public_policy     = true<br>  ignore_public_acls      = true<br>  restrict_public_buckets = true<br>}</pre><h4>Step 2 — Allow CloudFront OAC Access to Both Buckets</h4><p>Still in s3.tf, add two bucket policies.</p><h4>2.1 Blue bucket policy</h4><pre>resource &quot;aws_s3_bucket_policy&quot; &quot;dashboard_blue_policy&quot; {<br>  bucket = aws_s3_bucket.dashboard_blue.id<br>  policy = jsonencode({<br>    Version = &quot;2012-10-17&quot;,<br>    Statement = [<br>      {<br>        Sid = &quot;AllowCloudFrontReadBlue&quot;,<br>        Effect = &quot;Allow&quot;,<br>        Principal = { Service = &quot;cloudfront.amazonaws.com&quot; },<br>        Action = [&quot;s3:GetObject&quot;],<br>        Resource = &quot;${aws_s3_bucket.dashboard_blue.arn}/*&quot;,<br>        Condition = {<br>          StringEquals = {<br>            &quot;AWS:SourceArn&quot; = aws_cloudfront_distribution.cdn.arn<br>          }<br>        }<br>      }<br>    ]<br>  })<br>}</pre><h4>2.2 Green bucket policy</h4><pre>resource &quot;aws_s3_bucket_policy&quot; &quot;dashboard_green_policy&quot; {<br>  bucket = aws_s3_bucket.dashboard_green.id<br>  policy = jsonencode({<br>    Version = &quot;2012-10-17&quot;,<br>    Statement = [<br>      {<br>        Sid = &quot;AllowCloudFrontReadGreen&quot;,<br>        Effect = &quot;Allow&quot;,<br>        Principal = { Service = &quot;cloudfront.amazonaws.com&quot; },<br>        Action = [&quot;s3:GetObject&quot;],<br>        Resource = &quot;${aws_s3_bucket.dashboard_green.arn}/*&quot;,<br>        Condition = {<br>          StringEquals = {<br>            &quot;AWS:SourceArn&quot; = aws_cloudfront_distribution.cdn.arn<br>          }<br>        }<br>      }<br>    ]<br>  })<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LE0hQ0zjlIP-V03qTQUrQQ.png" /></figure><h4>Step 3 — Add a Variable to Select the Active Dashboard Color</h4><p>Edit:</p><p>infra/envs/dev/variables.tf</p><p>Add:</p><pre>variable &quot;active_dashboard_color&quot; {<br>  description = &quot;Which dashboard environment CloudFront should serve: blue or green&quot;<br>  type        = string<br>  default     = &quot;blue&quot;<br>  validation {<br>    condition     = contains([&quot;blue&quot;, &quot;green&quot;], var.active_dashboard_color)<br>    error_message = &quot;active_dashboard_color must be either &#39;blue&#39; or &#39;green&#39;&quot;<br>  }<br>}</pre><h4>Step 4 — Update CloudFront to Support Blue/Green Origin Switching</h4><p>Edit:</p><p>infra/envs/dev/cloudfront.tf</p><p>Replace the current S3 origin with two origins and use origin_id switching.</p><h4>4.1 Define two S3 origins</h4><pre>origin {<br>  domain_name              = aws_s3_bucket.dashboard_blue.bucket_regional_domain_name<br>  origin_id                = &quot;s3-dashboard-blue&quot;<br>  origin_access_control_id = aws_cloudfront_origin_access_control.oac.id<br>}<br>origin {<br>  domain_name              = aws_s3_bucket.dashboard_green.bucket_regional_domain_name<br>  origin_id                = &quot;s3-dashboard-green&quot;<br>  origin_access_control_id = aws_cloudfront_origin_access_control.oac.id<br>}</pre><h4>4.2 Set default_cache_behavior target origin dynamically</h4><p>Terraform cannot directly do if/else inside strings, but we can use a local.</p><p>Add to cloudfront.tf at the top:</p><pre>locals {<br>  active_dashboard_origin_id = var.active_dashboard_color == &quot;blue&quot; ? &quot;s3-dashboard-blue&quot; : &quot;s3-dashboard-green&quot;<br>}</pre><p>Then set:</p><pre>default_cache_behavior {<br>  target_origin_id       = local.active_dashboard_origin_id<br>  viewer_protocol_policy = &quot;redirect-to-https&quot;<br>  allowed_methods = [&quot;GET&quot;, &quot;HEAD&quot;, &quot;OPTIONS&quot;]<br>  cached_methods  = [&quot;GET&quot;, &quot;HEAD&quot;, &quot;OPTIONS&quot;]<br>  forwarded_values {<br>    query_string = false<br>    cookies {<br>      forward = &quot;none&quot;<br>    }<br>  }<br>}</pre><p>Note: /api/* behavior remains unchanged.</p><h4>Step 5 — Apply Terraform Changes</h4><p>Run:</p><pre>cd infra/envs/dev<br>terraform apply</pre><p>Expected:</p><ul><li>2 dashboard buckets exist</li><li>CloudFront has two S3 origins</li><li>the default behavior points to the currently active origin</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xAeEVdoaW2zMzY2Ve0NaCQ.png" /></figure><h4>Step 6 — Deploy Blue Version (Stable)</h4><p>Build the dashboard:</p><pre>cd apps\dashboard<br>npm install<br>npm run build<br>npm run export</pre><p>Upload to blue bucket:</p><pre>aws s3 sync .\out s3://edgeops-dev-dashboard-blue-559938827680 --delete</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dUo49DcmUe6mEq6PiqPbqQ.png" /></figure><p>Validate:</p><ul><li>open CloudFront URL</li><li>confirm dashboard loads successfully</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/1*S2NRD3Wd-IMsTW3bBmpWqw.png" /></figure><h4>Step 7 — Deploy Green Version (Release Candidate)</h4><p>Make a visible change to the dashboard (example: show “Green Release Candidate” label).</p><p>Rebuild:</p><pre>npm run build<br>npm run export</pre><p>Upload to green bucket:</p><pre>aws s3 sync .\out s3://edgeops-dev-dashboard-green-559938827680 --delete</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qjVlyFj0RoYQhGOsPdGzcA.png" /></figure><p>At this point green is deployed but not active.</p><h4>Step 8 — Flip Traffic to Green (Blue → Green Cutover)</h4><p>Edit infra/envs/dev/terraform.tfvars:</p><pre>active_dashboard_color = &quot;green&quot;</pre><p>Apply:</p><pre>cd infra/envs/dev<br>terraform apply</pre><p>Validation:</p><ul><li>open CloudFront URL</li><li>confirm the green version is now visible</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/1*bNs4j8cT1le6Mosx2Ta2_g.png" /></figure><p>We can also see from the image below in the AWS cloudfront console we have a green deployment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m8DLyciOb7qi62pJI6UC0w.png" /></figure><h4>Step 9 — Rollback (Green → Blue)</h4><p>Rollback is a configuration flip.</p><p>Set:</p><pre>active_dashboard_color = &quot;blue&quot;</pre><p>Apply:</p><pre>terraform apply</pre><p>CloudFront will begin serving blue again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*030CX5moUWAPxjoYucJE7Q.png" /></figure><h4>Step 10 — Automation via GitHub Actions (Frontend Blue/Green Deploy)</h4><p>We now automate dashboard deployment to the inactive bucket and then cutover.</p><h4>10.1 Workflow: Deploy dashboard build to inactive bucket</h4><p>Create:</p><p>.github/workflows/dashboard-deploy.yml</p><pre>name: Deploy Dashboard (Blue/Green)<br><br>on:<br>  workflow_dispatch:<br>    inputs:<br>      target:<br>        description: &quot;Deploy target color (blue or green)&quot;<br>        required: true<br>        default: &quot;green&quot;<br><br>jobs:<br>  deploy-dashboard:<br>    runs-on: ubuntu-latest<br><br>    steps:<br>      - name: Checkout repository<br>        uses: actions/checkout@v4<br><br>      - name: Setup Node.js<br>        uses: actions/setup-node@v4<br>        with:<br>          node-version: &quot;20&quot;<br><br>      - name: Install dependencies<br>        working-directory: apps/dashboard<br>        run: npm install<br><br>      # Next.js 14+ with next.config.js output: &quot;export&quot;<br>      # &quot;next export&quot; is removed, so build produces apps/dashboard/out automatically.<br>      - name: Build (Static Export)<br>        working-directory: apps/dashboard<br>        run: npm run build<br><br>      # Write frontend marker so the UI can display BLUE/GREEN automatically<br>      - name: Write frontend color marker<br>        run: |<br>          echo &quot;{\&quot;color\&quot;:\&quot;${{ inputs.target }}\&quot;}&quot; &gt; apps/dashboard/out/frontend.json<br><br>      - name: Configure AWS credentials<br>        uses: aws-actions/configure-aws-credentials@v4<br>        with:<br>          aws-region: us-east-1<br>          role-to-assume: ${{ secrets.AWS_ROLE_ARN }}<br><br>      - name: Sync to S3<br>        run: |<br>          if [ &quot;${{ inputs.target }}&quot; = &quot;blue&quot; ]; then<br>            aws s3 sync apps/dashboard/out s3://${{ secrets.DASHBOARD_BLUE_BUCKET }} --delete<br>          else<br>            aws s3 sync apps/dashboard/out s3://${{ secrets.DASHBOARD_GREEN_BUCKET }} --delete<br>          fi</pre><p>This workflow:</p><ul><li>builds the dashboard</li><li>uploads it to the selected bucket</li></ul><p>Cutover is handled by a separate Terraform change.</p><h4>Day 5 Summary</h4><p><strong>Blue/green frontend delivery with instant cutover and rollback.</strong><br>Two S3 buckets (blue and green) are provisioned behind CloudFront, and a single configuration value selects the active origin. Deployment becomes: upload to inactive bucket → switch origin → validate → rollback if needed. This mirrors real internal platforms where reversibility and low-risk releases matter more than raw deployment speed.</p><h4>Day 6 — Observability, Confidence Checks, and Rollback Controls</h4><p>So far, the EdgeOps Platform already supports:</p><ul><li>Canary deployments for the API using Lambda weighted alias routing</li><li>Blue/green deployments for the frontend using dual S3 origins behind CloudFront</li></ul><p>Day 6 focuses on making these deployment strategies <strong>operationally safe</strong>. Rather than introducing autonomous rollback prematurely, the goal is to ensure that every release can be evaluated using <strong>objective signals</strong>, with rollback controls that are fast, auditable, and reliable.</p><p>This mirrors how many internal platform teams operate in practice.</p><h4>Day 6 Objectives</h4><p>By the end of Day 6, the following must be true:</p><ul><li>The API emits structured logs with request-level correlation.</li><li>CloudWatch Logs capture all API traffic consistently.</li><li>CloudWatch metrics and alarms exist for:</li><li>API Gateway 5XX errors</li><li>API Gateway latency</li><li>Lambda execution errors</li><li>A documented confidence check process exists for canary releases.</li><li>Rollback can be executed in minutes through configuration change.</li><li>A GitHub Actions workflow exists to perform rollback safely and audibly.</li></ul><h4>Observability Model</h4><p>EdgeOps adopts a <strong>minimal but production-valid observability model</strong>.</p><h4>Logs</h4><ul><li>Emitted directly by the Lambda runtime</li><li>Stored in CloudWatch Logs</li><li>Structured as JSON for queryability</li></ul><h4>Metrics</h4><ul><li>API Gateway metrics:</li><li>5XXError</li><li>Latency</li><li>Lambda metrics:</li><li>Errors</li><li>Duration</li><li>Throttles</li></ul><h4>Alarms</h4><ul><li>Detect service degradation</li><li>Serve as <strong>decision signals</strong>, not autonomous actions</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DxrhC58z0hfTNb42c2FPHQ.png" /></figure><p>This keeps the platform lightweight while remaining realistic.</p><h4>Part A — API Observability (Structured Logging and Correlation)</h4><h4>Step 1 — Adding Structured Logging</h4><p>The API already returns deterministic identity fields:</p><ul><li>service</li><li>version</li><li>deployment (stable / canary)</li><li>environment</li><li>request_id</li></ul><p>We extend this by emitting <strong>one structured log entry per request</strong>.</p><p>Add a logging helper to apps/api/src/server.js:</p><pre>function logEvent(eventType, data) {<br>  console.log(JSON.stringify({<br>    event_type: eventType,<br>    ...data<br>  }));<br>}</pre><p>Update /api/health:</p><pre>app.get(&quot;/api/health&quot;, (req, res) =&gt; {<br>  const p = { status: &quot;ok&quot;, ...payload() };<br>  logEvent(&quot;api_health&quot;, {<br>    path: req.path,<br>    method: req.method,<br>    user_agent: req.headers[&quot;user-agent&quot;],<br>    ...p<br>  });<br>  res.status(200).json(p);<br>});</pre><p>Update /api/version:</p><pre>app.get(&quot;/api/version&quot;, (req, res) =&gt; {<br>  const p = payload();<br>  logEvent(&quot;api_version&quot;, {<br>    path: req.path,<br>    method: req.method,<br>    user_agent: req.headers[&quot;user-agent&quot;],<br>    ...p<br>  });<br>  res.status(200).json(p);<br>});</pre><p>Every request now produces a log entry containing:</p><ul><li>deployment identity (stable or canary)</li><li>version</li><li>environment</li><li>request_id</li></ul><p>This enables accurate post-deployment analysis.</p><h4>Step 2 — Rebuild and Deploy the API</h4><p>Repackage and deploy:</p><pre>cd edgeops-platform<br>Remove-Item -Recurse -Force .\dist -ErrorAction SilentlyContinue<br>New-Item -ItemType Directory .\dist | Out-Null<br>cd apps\api<br>npm install --omit=dev<br>Compress-Archive -Path * -DestinationPath ..\..\dist\api.zip -Force<br>cd ..\..\infra\envs\dev<br>terraform apply</pre><p>This publishes a new Lambda version with logging enabled.</p><h4>Part B — CloudWatch Metrics and Alarms</h4><h4>Step 3 — Create CloudWatch Alarms</h4><p>Create infra/envs/dev/alarms.tf.</p><h4>API Gateway 5XX Errors</h4><pre>resource &quot;aws_cloudwatch_metric_alarm&quot; &quot;apigw_5xx&quot; {<br>  alarm_name          = &quot;edgeops-dev-apigw-5xx&quot;<br>  comparison_operator = &quot;GreaterThanThreshold&quot;<br>  evaluation_periods  = 1<br>  metric_name         = &quot;5XXError&quot;<br>  namespace           = &quot;AWS/ApiGateway&quot;<br>  period              = 60<br>  statistic           = &quot;Sum&quot;<br>  threshold           = 5<br>  dimensions = {<br>    ApiName = aws_apigatewayv2_api.api.name<br>  }<br>}</pre><h4>API Gateway Latency</h4><pre>resource &quot;aws_cloudwatch_metric_alarm&quot; &quot;apigw_latency&quot; {<br>  alarm_name          = &quot;edgeops-dev-apigw-latency&quot;<br>  comparison_operator = &quot;GreaterThanThreshold&quot;<br>  evaluation_periods  = 1<br>  metric_name         = &quot;Latency&quot;<br>  namespace           = &quot;AWS/ApiGateway&quot;<br>  period              = 60<br>  statistic           = &quot;Average&quot;<br>  threshold           = 100<br>  dimensions = {<br>    ApiName = aws_apigatewayv2_api.api.name<br>  }<br>}</pre><h4>Lambda Errors</h4><pre>resource &quot;aws_cloudwatch_metric_alarm&quot; &quot;lambda_errors&quot; {<br>  alarm_name          = &quot;edgeops-dev-lambda-errors&quot;<br>  comparison_operator = &quot;GreaterThanThreshold&quot;<br>  evaluation_periods  = 1<br>  metric_name         = &quot;Errors&quot;<br>  namespace           = &quot;AWS/Lambda&quot;<br>  period              = 60<br>  statistic           = &quot;Sum&quot;<br>  threshold           = 1<br>  dimensions = {<br>    FunctionName = aws_lambda_function.api.function_name<br>  }<br>}</pre><p>Apply:</p><pre>terraform apply</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/979/1*mtThgVjy2rYi8WuActMGqA.png" /></figure><h4>Part C — Confidence Checks</h4><p>A confidence check defines the minimum evidence required to proceed with a rollout.</p><h4>Canary Confidence Criteria</h4><ul><li>/api/health returns HTTP 200</li><li>/api/version returns expected deployment identity</li><li>Canary traffic reflects configured weight</li><li>CloudWatch alarms remain in OK state</li><li>Logs show both stable and canary traffic</li></ul><h4>Traffic Verification</h4><pre>$stable = 0<br>$canary = 0<br>$other  = 0<br>$errors = 0<br><br>for ($i=0; $i -lt 100; $i++) {<br>  try {<br>    $resp = Invoke-RestMethod -Uri &quot;https://$cf/api/version&quot; -TimeoutSec 10<br>    if ($resp.deployment -eq &quot;stable&quot;) { $stable++ }<br>    elseif ($resp.deployment -eq &quot;canary&quot;) { $canary++ }<br>    else { $other++ }<br>  } catch {<br>    $errors++<br>  }<br>}<br><br>&quot;stable=$stable canary=$canary other=$other errors=$errors&quot;</pre><p>This provides <strong>objective confirmation</strong> of routing behavior.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4rzRX63hfWwAKA39PxDbMA.png" /></figure><h4>Part D — Rollback Controls</h4><h4>Step 7 — Canonical Rollback Procedure</h4><p>Rollback is a <strong>configuration change</strong>, not a redeploy.</p><p>Edit terraform.tfvars:</p><pre>canary_weight = 0.0</pre><p>Apply:</p><pre>terraform apply</pre><p>Traffic immediately returns to stable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*0LxxqUZZvaASS9faIFX-Kw.png" /></figure><h4>Step 8 — GitHub Actions Rollback Workflow</h4><p>Create .github/workflows/rollback-canary.yml:</p><pre>name: Rollback Canary (Dev)<br>on:<br>  workflow_dispatch:<br>jobs:<br>  rollback:<br>    runs-on: ubuntu-latest<br>    environment: dev<br>    steps:<br>      - uses: actions/checkout@v4<br>      - uses: hashicorp/setup-terraform@v3<br>      - name: Terraform Init<br>        working-directory: infra/envs/dev<br>        run: terraform init<br>      - name: Set canary weight to zero<br>        working-directory: infra/envs/dev<br>        run: |<br>          sed -i &#39;s/canary_weight *= *.*/canary_weight = 0.0/&#39; terraform.tfvars || true<br>      - name: Terraform Apply<br>        working-directory: infra/envs/dev<br>        run: terraform apply -auto-approve</pre><p>This provides:</p><ul><li>one-click rollback</li><li>auditability</li><li>zero manual console actions</li></ul><h4>Day 6 Summary</h4><p><strong>Releases backed by telemetry, not hope.</strong><br>The API emits structured logs with request-level correlation, CloudWatch captures traffic signals, and alarms provide decision thresholds for errors and latency. Confidence checks make canary validation repeatable, and rollback is available as a controlled workflow with an audit trail. At this point, EdgeOps supports progressive delivery with the minimum operational safety required for real usage.</p><h4>Day 7 — Drift Detection Automation, Cost Guardrails, and Final Platform Demo</h4><p>Day 7 completes the EdgeOps Platform by adding the final operational capabilities that make it behave like a real internal platform tool:</p><ul><li><strong>drift detection</strong> with automatic reporting</li><li><strong>cost guardrails</strong> suitable for lab environments</li><li><strong>final documentation and demo readiness</strong> so the project is portfolio-grade and reproducible</li></ul><p>The focus is to harden the platform so it remains safe, predictable, and explainable under change.</p><h4>Day 7 Objectives</h4><p>By the end of Day 7, the following must be true:</p><ul><li>Drift detection runs on a schedule and identifies unmanaged infrastructure changes.</li><li>Drift detection automatically opens a GitHub issue with actionable details.</li><li>Cost guardrails exist:</li><li>budgets and alerts for the account scope used for the lab</li><li>optional scheduled shutdown guidance for non-prod components</li><li>A final demo script exists to showcase:</li><li>canary rollout and rollback for the API</li><li>blue/green cutover and rollback for the dashboard</li><li>policy enforcement (if enabled)</li><li>drift detection workflow results</li><li>The repository contains a clear “how to run” section for reviewers.</li></ul><h4>Part A — Drift Detection with Automatic GitHub Issue Creation</h4><h4>Step 1 — Create a Drift Detection Workflow That Produces a Plan Artifact</h4><p>Create (or update) the drift workflow file:</p><p>.github/workflows/drift-detection.yml</p><pre>name: Terraform Drift Detection<br><br>on:<br>  schedule:<br>    - cron: &quot;0 3 * * *&quot;<br>  workflow_dispatch:<br><br>permissions:<br>  contents: read<br>  # issues: write   # enable this only if you add issue creation later<br><br>jobs:<br>  drift-detection:<br>    runs-on: ubuntu-latest<br><br>    environment:<br>      name: dev<br><br>    steps:<br>      - name: Checkout repository<br>        uses: actions/checkout@v4<br><br>      - name: Configure AWS credentials<br>        uses: aws-actions/configure-aws-credentials@v4<br>        with:<br>          aws-region: us-east-1<br>          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}<br>          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}<br><br>      - name: Verify AWS identity<br>        run: aws sts get-caller-identity<br><br>      - name: Setup Terraform<br>        uses: hashicorp/setup-terraform@v3<br><br>      - name: Terraform Init<br>        working-directory: infra/envs/dev<br>        run: terraform init<br><br>      - name: Terraform Plan (Drift Check)<br>        id: drift<br>        working-directory: infra/envs/dev<br>        shell: bash<br>        run: |<br>          set +e<br>          terraform plan -lock-timeout=5m -detailed-exitcode -no-color -out=tfplan.binary<br>          code=$?<br>          echo &quot;exit_code=$code&quot; &gt;&gt; &quot;$GITHUB_OUTPUT&quot;<br>          exit 0<br><br>      - name: Convert Plan to JSON<br>        if: always()<br>        working-directory: infra/envs/dev<br>        run: terraform show -json tfplan.binary &gt; tfplan.json<br><br>      - name: Upload Drift Plan Artifact<br>        if: always()<br>        uses: actions/upload-artifact@v4<br>        with:<br>          name: drift-plan<br>          path: infra/envs/dev/tfplan.json<br><br>      - name: Report result<br>        shell: bash<br>        run: |<br>          code=&quot;${{ steps.drift.outputs.exit_code }}&quot;<br>          if [ &quot;$code&quot; = &quot;0&quot; ]; then<br>            echo &quot;No drift detected.&quot;<br>            exit 0<br>          elif [ &quot;$code&quot; = &quot;2&quot; ]; then<br>            echo &quot;Drift detected (plan exit code 2).&quot;<br>            exit 0<br>          else<br>            echo &quot;Terraform plan failed (exit code $code).&quot;<br>            exit 1<br>          fi</pre><h4>Interpretation of Exit Codes</h4><p>Terraform drift uses exit codes:</p><ul><li>0 → no drift</li><li>2 → drift detected</li><li>1 → error</li></ul><p>We capture the output regardless of exit code so that we can post details into a GitHub issue.</p><h4>Step 2 — Create a GitHub Issue Automatically When Drift Is Detected</h4><p>We will create an issue when the exit code indicates drift (2).</p><p>Append to the workflow:</p><pre>- name: Create GitHub Issue if Drift Detected<br>        if: ${{ steps.drift_plan.outputs.exitcode == &#39;2&#39; }}<br>        uses: actions/github-script@v7<br>        with:<br>          script: |<br>            const title = &quot;Drift detected in dev environment&quot;;<br>            const body = [<br>              &quot;Terraform has detected infrastructure drift in `infra/envs/dev`.&quot;,<br>              &quot;&quot;,<br>              &quot;Actions:&quot;,<br>              &quot;- Review the workflow run artifacts (drift-plan) for the Terraform plan JSON.&quot;,<br>              &quot;- Reconcile drift by updating Terraform code or reverting manual changes in AWS.&quot;,<br>              &quot;&quot;,<br>              &quot;Notes:&quot;,<br>              &quot;- Drift is detected when `terraform plan -detailed-exitcode` returns exit code 2.&quot;,<br>              &quot;- This issue is created automatically by the drift detection workflow.&quot;<br>            ].join(&quot;\n&quot;);<br><br>       const issues = await github.rest.issues.listForRepo({<br>              owner: context.repo.owner,<br>              repo: context.repo.repo,<br>              state: &quot;open&quot;,<br>              labels: &quot;drift&quot;<br>            }).catch(() =&gt; ({ data: [] }));<br>            const alreadyOpen = issues.data.some(i =&gt; i.title === title);<br>            if (!alreadyOpen) {<br>              await github.rest.issues.create({<br>                owner: context.repo.owner,<br>                repo: context.repo.repo,<br>                title,<br>                body,<br>                labels: [&quot;drift&quot;]<br>              });<br>            }</pre><p>This ensures:</p><ul><li>drift is visible without checking logs manually</li><li>a tracked audit trail exists in GitHub</li><li>duplicate drift issues are avoided</li></ul><h4>Step 3 — Validation Procedure for Drift Detection</h4><p>To validate drift detection end-to-end, apply a harmless manual change in AWS.</p><p>Recommended drift test:</p><ul><li>go to the CloudFront distribution created by Terraform</li><li>change a non-critical setting (example: comment field)</li><li>save changes</li></ul><p>Then run workflow manually:</p><ul><li>GitHub → Actions → “Drift Detection (Dev)” → Run workflow</li></ul><p>Expected:</p><ul><li>workflow run exit code indicates drift</li><li>a GitHub issue is created labeled drift</li><li>plan JSON artifact is attached to the workflow run</li></ul><p>After validation, revert the manual change and re-run drift detection to confirm it returns clean.</p><h4>Part B — Cost Guardrails (Budget + Alerts)</h4><p>A world-class platform does not only deploy safely; it also controls cost. In lab environments, budgets help prevent unintended spend.</p><h4>Step 4 — Configure an AWS Budget (Terraform)</h4><p>Create a file:</p><p>infra/envs/dev/budget.tf</p><pre>resource &quot;aws_budgets_budget&quot; &quot;monthly&quot; {<br>  name         = &quot;edgeops-dev-monthly-budget&quot;<br>  budget_type  = &quot;COST&quot;<br>  limit_amount = &quot;25&quot;<br>  limit_unit   = &quot;USD&quot;<br>  time_unit    = &quot;MONTHLY&quot;<br>cost_filter {<br>    name   = &quot;Service&quot;<br>    values = [&quot;Amazon CloudFront&quot;, &quot;Amazon API Gateway&quot;, &quot;AWS Lambda&quot;, &quot;Amazon Simple Storage Service&quot;]<br>  }<br>}</pre><p>This budget provides:</p><ul><li>a predictable monthly cap for lab usage</li><li>service-scoped visibility for the platform services</li></ul><p>Apply:</p><pre>cd infra\envs\dev<br>terraform apply</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ftm5MVD_Cx2ig2vgnB_qsA.png" /></figure><h4>Notes on Notifications</h4><p>Budget notifications (email or SNS) require additional configuration.<br> If your AWS account is restricted or you do not want to configure SNS, keep the budget definition as visibility-only.</p><p>If notifications are allowed, we can add an SNS topic and a budget notification in a follow-up step.</p><h4>Part C — Optional Shutdown Automation (Lab Safety)</h4><p>For serverless platforms, “shutdown” is less direct, but we can still reduce spend.</p><p>Low-risk options include:</p><ul><li>setting canary weight to 0 by default</li><li>using minimal retention periods for logs</li><li>disabling CloudFront distribution temporarily (manual)</li><li>adding a scheduled workflow that can flip active dashboard back to blue or disable dev deploy jobs</li></ul><p>For this project, the budget plus discipline on artifact deployments is sufficient. If you want additional automation, a scheduled workflow can flip:</p><ul><li>canary_weight = 0.0</li><li>active_dashboard_color = &quot;blue&quot;</li></ul><p>and apply nightly for dev.</p><h4>Part D — Final Platform Demo Script</h4><p>The demo proves platform capabilities clearly and quickly.</p><h4>Step 5 — Demo Checklist (What I Demonstrated)</h4><h4>1. Baseline state</h4><ul><li>Let us open dashboard through CloudFront</li><li>clearly show /api/version returns stable</li></ul><p>Commands:</p><pre>curl https://d30tfelv6kj27h.cloudfront.net//api/version</pre><h4>2. Canary rollout (API)</h4><ul><li>set canary_weight = 0.1</li><li>apply terraform</li><li>run traffic sampling script</li><li>show stable vs canary split</li></ul><h4>3. Canary rollback (API)</h4><ul><li>trigger GitHub Actions rollback workflow</li><li>confirm canary responses drop to zero</li></ul><h4>4. Blue/green cutover (Dashboard)</h4><ul><li>deploy change to inactive bucket (green)</li><li>flip active_dashboard_color = &quot;green&quot;</li><li>confirm UI changed</li></ul><h4>5. Blue/green rollback (Dashboard)</h4><ul><li>set active_dashboard_color = &quot;blue&quot;</li><li>apply</li><li>confirm rollback is immediate</li></ul><h4>6. Drift detection</h4><ul><li>run drift workflow manually</li><li>show GitHub issue creation (if drift is injected) or show “no drift” run</li></ul><h4>Step 6 — Create a Demo Script Document</h4><p>Create:</p><p>docs/demo-script.md</p><pre># EdgeOps Platform Demo Script<br>## 1. Show platform entry point<br>- Open CloudFront URL for dashboard<br>- Confirm dashboard loads<br>## 2. Show API baseline<br>- curl /api/version<br>- Confirm stable deployment<br>## 3. Canary rollout (10%)<br>- Set canary_weight=0.1<br>- terraform apply<br>- Run sampling script and show distribution<br>## 4. Rollback<br>- Trigger GitHub Actions rollback workflow<br>- Re-run sampling script and show canary=0<br>## 5. Blue/green dashboard cutover<br>- Deploy to green bucket<br>- Flip active_dashboard_color=green<br>- Confirm updated UI<br>## 6. Dashboard rollback<br>- Flip active_dashboard_color=blue<br>- Confirm previous UI is restored<br>## 7. Drift detection<br>- Trigger drift workflow<br>- Show issue creation (or show clean drift-free run)</pre><h4>Day 7 Summary</h4><p>By the end of Day 7, EdgeOps Platform includes operational maturity features that move it beyond a deployment demo into a real platform-style system:</p><ul><li>drift detection with GitHub issue creation</li><li>cost guardrails suitable for lab constraints</li><li>a repeatable demo script that shows:</li><li>canary rollout and rollback</li><li>blue/green cutover and rollback</li><li>governance and operational visibility</li></ul><p>This completes the EdgeOps Platform as a portfolio-grade internal platform engineering project.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3c8c3b2e0cc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Modern Terraform Project Like a Pro (Phase 2)]]></title>
            <link>https://medium.com/@shehuyusuf/building-a-modern-terraform-project-like-a-pro-phase-2-f8616921ba6c?source=rss-607ba1670d2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8616921ba6c</guid>
            <dc:creator><![CDATA[Shehu Yusuf]]></dc:creator>
            <pubDate>Thu, 01 Jan 2026 23:58:31 GMT</pubDate>
            <atom:updated>2026-01-01T23:58:31.213Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Evolving a secure edge-first platform into a domain-backed, API-enabled, governed production system.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E5rmSIUf5lg6kJvWcMiZdA.png" /></figure><p>So far, we have established a strong foundation:</p><p>Phase 1 established a strong operational foundation for safe infrastructure delivery:</p><ul><li>Remote Terraform state management using S3 with DynamoDB locking</li><li>Environment isolation across development and production</li><li>Cost governance using AWS Budgets</li><li>Secure edge delivery using CloudFront and a private S3 origin (OAC)</li><li>CI/CD for frontend deployments using GitHub Actions</li><li>Least-privilege IAM practices for automation</li></ul><p>Phase 2 builds on this foundation without rewriting it. The objective is to evolve the project into a true platform: domain-backed, API-enabled, and governed with production-grade discipline. This is how platform teams work: they build stable layers first, then extend them incrementally while preserving correctness and reducing blast radius.</p><p>This is what platform teams do:</p><blockquote><em>They build stable layers, then extend them incrementally.</em></blockquote><h4>Phase 2 Mental Model</h4><p>Phase 1 demonstrated that:</p><ul><li>Infrastructure can be deployed safely and repeatably</li><li>CI/CD can ship content with minimal manual intervention</li><li>The edge layer can remain secure, low-cost, and scalable</li></ul><p>Phase 2 expands those guarantees by proving that:</p><ul><li>The platform can evolve without breaking core assumptions</li><li>Changes can be promoted with controlled blast radius</li><li>Infrastructure changes can be reviewed and approved</li><li>Drift can be detected and handled deliberately</li><li>Policies can enforce standards automatically</li></ul><p>The principle guiding Phase 2 is simple:</p><blockquote><strong><em>Add layers. Don’t replace foundations.</em></strong></blockquote><h4>Step 7.1 — Adding a Custom Domain (Route 53 + ACM)</h4><h4>Why Domain Comes First</h4><p>Everything else depends on the platform having a stable identity. CloudFront default domains work for prototypes, but production systems require:</p><ul><li>Predictable URLs</li><li>TLS certificates you control</li><li>Repeatable, versioned infrastructure configuration</li><li>DNS that can be promoted and managed through IaC</li></ul><p>Domain management is also one of the most common sources of production mistakes. Failures typically occur when:</p><ul><li>Certificates are created in the wrong region</li><li>CloudFront is updated before DNS validation completes</li><li>Distributions are accidentally replaced instead of updated</li></ul><p>This step solves those problems cleanly and safely.</p><h4>Design Decisions</h4><ul><li>Route 53 is used for tight integration with AWS edge and certificate validation</li><li>ACM certificates are created in us-east-1 (required for CloudFront)</li><li>DNS validation is performed entirely through Terraform</li><li>CloudFront is updated in-place to avoid downtime</li><li>Existing hosted zones are supported to reflect enterprise DNS realities</li></ul><h4>Terraform Implementation (Modular Approach)</h4><p>This is implemented using small modules to preserve blast radius control.</p><h4>Create modules/acm/main.tf</h4><pre>variable &quot;domain_name&quot; {<br>  type = string<br>}</pre><pre>resource &quot;aws_acm_certificate&quot; &quot;cert&quot; {<br>  domain_name       = var.domain_name<br>  validation_method = &quot;DNS&quot;<br>}</pre><pre>output &quot;certificate_arn&quot; {<br>  value = aws_acm_certificate.cert.arn<br>}</pre><pre>output &quot;domain_validation_options&quot; {<br>  value = aws_acm_certificate.cert.domain_validation_options<br>}</pre><h4>Create modules/dns/main.tf (Optional zone creation)</h4><p>Real platforms often <strong>do not own</strong> DNS zones. They reference them.</p><p>So we support both:</p><p>create new zone<br>or reference existing zone</p><pre>variable &quot;zone_name&quot; {<br>  type = string<br>}</pre><pre>variable &quot;create_zone&quot; {<br>  type    = bool<br>  default = false<br>}</pre><pre>resource &quot;aws_route53_zone&quot; &quot;zone&quot; {<br>  count = var.create_zone ? 1 : 0<br>  name  = var.zone_name<br>}</pre><pre>data &quot;aws_route53_zone&quot; &quot;existing&quot; {<br>  count = var.create_zone ? 0 : 1<br>  name  = var.zone_name<br>}</pre><pre>output &quot;zone_id&quot; {<br>  value = var.create_zone ? aws_route53_zone.zone[0].zone_id : data.aws_route53_zone.existing[0].zone_id<br>}</pre><h4>Create modules/acm_validation/main.tf (DNS records + validation wait)</h4><p>This is the critical Terraform piece many engineers skip.</p><p>We don’t just create DNS records — we also force Terraform to <strong>wait</strong> until ACM is actually validated.</p><pre>variable &quot;zone_id&quot; {<br>  type = string<br>}</pre><pre>variable &quot;certificate_arn&quot; {<br>  type = string<br>}</pre><pre>variable &quot;validation_options&quot; {<br>  type = any<br>}</pre><pre>resource &quot;aws_route53_record&quot; &quot;validation&quot; {<br>  for_each = {<br>    for dvo in var.validation_options : dvo.domain_name =&gt; {<br>      name  = dvo.resource_record_name<br>      type  = dvo.resource_record_type<br>      value = dvo.resource_record_value<br>    }<br>  }</pre><pre>  zone_id = var.zone_id<br>  name    = each.value.name<br>  type    = each.value.type<br>  records = [each.value.value]<br>  ttl     = 60<br>}</pre><pre>resource &quot;aws_acm_certificate_validation&quot; &quot;cert_validation&quot; {<br>  certificate_arn         = var.certificate_arn<br>  validation_record_fqdns = [for record in aws_route53_record.validation : record.fqdn]<br>}</pre><pre>output &quot;validated_certificate_arn&quot; {<br>  value = aws_acm_certificate_validation.cert_validation.certificate_arn<br>}</pre><h4>Update CloudFront (in modules/edge_frontend/main.tf)</h4><p>Add support for:</p><ul><li>custom domain alias</li><li>ACM certificate ARN</li></ul><pre>variable &quot;custom_domain&quot; {<br>  type    = string<br>  default = null<br>}</pre><pre>variable &quot;acm_certificate_arn&quot; {<br>  type    = string<br>  default = null<br>}</pre><p>Then update:</p><pre>aliases = var.custom_domain != null ? [var.custom_domain] : []</pre><pre>viewer_certificate {<br>  cloudfront_default_certificate = var.acm_certificate_arn == null<br>  acm_certificate_arn            = var.acm_certificate_arn<br>  ssl_support_method             = var.acm_certificate_arn == null ? null : &quot;sni-only&quot;<br>  minimum_protocol_version       = var.acm_certificate_arn == null ? null : &quot;TLSv1.2_2021&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vVURW-65-YDqa7yquXWLnw.png" /></figure><h4>Add Route 53 Alias Record → CloudFront</h4><p>In envs/dev/dns.tf:</p><pre>resource &quot;aws_route53_record&quot; &quot;cdn_alias&quot; {<br>  zone_id = module.dns.zone_id<br>  name    = &quot;app&quot;<br>  type    = &quot;A&quot;</pre><pre>  alias {<br>    name                   = module.edge_frontend.cloudfront_domain_name<br>    zone_id                = module.edge_frontend.cloudfront_hosted_zone_id<br>    evaluate_target_health = false<br>  }<br>}</pre><h4>Step 7.2 — Deploying an API Gateway + Lambda Behind CloudFront</h4><p>The platform adds backend capability without changing the cost model. The design remains:</p><ul><li>Fully serverless</li><li>No NAT Gateway</li><li>No EC2</li><li>No always-on compute cost</li></ul><p>The end-user experience becomes:</p><ul><li>/ routes to S3 frontend</li><li>/api/* routes to API Gateway</li><li>API Gateway triggers Lambda</li></ul><h4>Lambda Module (modules/lambda)</h4><p>You’ll be able to hit:</p><p><a href="https://d3wtrfsoyfqlb.cloudfront.net/api/health">https://d3wtrfsoyfqlb.cloudfront.net/api/health</a></p><p>…and CloudFront will route the request like this:</p><pre>CloudFront<br>  ├── /        → S3 (frontend)<br>  └── /api/*   → API Gateway (HTTP API)<br>                   ↓<br>                 Lambda</pre><h4>7.2.1 Create Module: modules/lambda (Auto-packaged, no manual zip)</h4><h4>Create folder:</h4><pre>modules/lambda/<br>  main.tf<br>  src/<br>    index.js</pre><h4>modules/lambda/src/index.js</h4><pre>exports.handler = async () =&gt; {<br>  return {<br>    statusCode: 200,<br>    headers: { &quot;content-type&quot;: &quot;application/json&quot; },<br>    body: JSON.stringify({ status: &quot;ok&quot;, time: new Date().toISOString() }),<br>  };<br>};</pre><h4>modules/lambda/main.tf</h4><pre>variable &quot;function_name&quot; {<br>  type = string<br>}</pre><pre>resource &quot;aws_iam_role&quot; &quot;lambda_exec&quot; {<br>  name = &quot;${var.function_name}-role&quot;</pre><pre>  assume_role_policy = jsonencode({<br>    Version = &quot;2012-10-17&quot;<br>    Statement = [{<br>      Action = &quot;sts:AssumeRole&quot;<br>      Effect = &quot;Allow&quot;<br>      Principal = { Service = &quot;lambda.amazonaws.com&quot; }<br>    }]<br>  })<br>}</pre><pre>resource &quot;aws_iam_role_policy_attachment&quot; &quot;basic&quot; {<br>  role       = aws_iam_role.lambda_exec.name<br>  policy_arn  = &quot;arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole&quot;<br>}</pre><pre>data &quot;archive_file&quot; &quot;lambda_zip&quot; {<br>  type        = &quot;zip&quot;<br>  source_dir  = &quot;${path.module}/src&quot;<br>  output_path = &quot;${path.module}/lambda.zip&quot;<br>}</pre><pre>resource &quot;aws_lambda_function&quot; &quot;api&quot; {<br>  function_name    = var.function_name<br>  role             = aws_iam_role.lambda_exec.arn<br>  handler          = &quot;index.handler&quot;<br>  runtime          = &quot;nodejs18.x&quot;</pre><pre>  filename         = data.archive_file.lambda_zip.output_path<br>  source_code_hash = data.archive_file.lambda_zip.output_base64sha256<br>}</pre><pre>output &quot;invoke_arn&quot; {<br>  value = aws_lambda_function.api.invoke_arn<br>}</pre><pre>output &quot;function_arn&quot; {<br>  value = aws_lambda_function.api.arn<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6zzRDEhG9ENWlp7_w_VMmw.png" /></figure><h4>7.2.2 Create Module: modules/api_gateway</h4><h4>Create folder:</h4><pre>modules/api_gateway/<br>  main.tf</pre><h4>modules/api_gateway/main.tf</h4><pre>variable &quot;lambda_invoke_arn&quot; {<br>  type = string<br>}</pre><pre>variable &quot;lambda_function_arn&quot; {<br>  type = string<br>}</pre><pre>resource &quot;aws_apigatewayv2_api&quot; &quot;http_api&quot; {<br>  name          = &quot;modern-api&quot;<br>  protocol_type = &quot;HTTP&quot;<br>}</pre><pre>resource &quot;aws_apigatewayv2_integration&quot; &quot;lambda&quot; {<br>  api_id           = aws_apigatewayv2_api.http_api.id<br>  integration_type = &quot;AWS_PROXY&quot;<br>  integration_uri  = var.lambda_invoke_arn<br>}</pre><pre>resource &quot;aws_apigatewayv2_route&quot; &quot;health&quot; {<br>  api_id    = aws_apigatewayv2_api.http_api.id<br>  route_key = &quot;GET /health&quot;<br>  target    = &quot;integrations/${aws_apigatewayv2_integration.lambda.id}&quot;<br>}</pre><pre>resource &quot;aws_apigatewayv2_stage&quot; &quot;default&quot; {<br>  api_id      = aws_apigatewayv2_api.http_api.id<br>  name        = &quot;$default&quot;<br>  auto_deploy = true<br>}</pre><pre>resource &quot;aws_lambda_permission&quot; &quot;apigw&quot; {<br>  statement_id  = &quot;AllowExecutionFromAPIGateway&quot;<br>  action        = &quot;lambda:InvokeFunction&quot;<br>  function_name = var.lambda_function_arn<br>  principal     = &quot;apigateway.amazonaws.com&quot;<br>  source_arn    = &quot;${aws_apigatewayv2_api.http_api.execution_arn}/*/*&quot;<br>}</pre><pre>output &quot;api_endpoint&quot; {<br>  value = aws_apigatewayv2_api.http_api.api_endpoint<br>}</pre><pre>output &quot;api_domain&quot; {<br>  value = replace(aws_apigatewayv2_api.http_api.api_endpoint, &quot;https://&quot;, &quot;&quot;)<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*52OeWHDStTFfwrX87TLYZQ.png" /></figure><p>We also output api_domain for CloudFront origin.</p><h4>7.2.3 Wire Modules in envs/dev</h4><h4>Create: envs/dev/api.tf</h4><pre>module &quot;lambda&quot; {<br>  source        = &quot;../../modules/lambda&quot;<br>  function_name = &quot;modern-api-dev&quot;<br>}</pre><pre>module &quot;api_gateway&quot; {<br>  source              = &quot;../../modules/api_gateway&quot;<br>  lambda_invoke_arn   = module.lambda.invoke_arn<br>  lambda_function_arn = module.lambda.function_arn<br>}</pre><h4>7.2.4 Update CloudFront Module to Support /api/*</h4><p>We extend modules/edge_frontend/main.tf WITHOUT breaking Phase 1.</p><h4>Add variable</h4><p>In modules/edge_frontend/variables.tf:</p><pre>variable &quot;api_origin_domain&quot; {<br>  type        = string<br>  default     = null<br>  description = &quot;API Gateway domain (without https://) used as CloudFront origin for /api/*&quot;<br>}</pre><h4>Update CloudFront distribution in modules/edge_frontend/main.tf</h4><p>Inside aws_cloudfront_distribution add a second origin:</p><pre>dynamic &quot;origin&quot; {<br>  for_each = var.api_origin_domain != null ? [1] : []<br>  content {<br>    domain_name = var.api_origin_domain<br>    origin_id   = &quot;api-origin&quot;</pre><pre>    custom_origin_config {<br>      http_port              = 80<br>      https_port             = 443<br>      origin_protocol_policy = &quot;https-only&quot;<br>      origin_ssl_protocols   = [&quot;TLSv1.2&quot;]<br>    }<br>  }<br>}</pre><p>Then add cache behavior:</p><pre>dynamic &quot;ordered_cache_behavior&quot; {<br>  for_each = var.api_origin_domain != null ? [1] : []<br>  content {<br>    path_pattern           = &quot;/api/*&quot;<br>    target_origin_id       = &quot;api-origin&quot;<br>    viewer_protocol_policy = &quot;redirect-to-https&quot;</pre><pre>    allowed_methods = [&quot;GET&quot;, &quot;HEAD&quot;, &quot;OPTIONS&quot;]<br>    cached_methods  = [&quot;GET&quot;, &quot;HEAD&quot;]</pre><pre>    forwarded_values {<br>      query_string = true<br>      headers      = [&quot;Origin&quot;, &quot;Authorization&quot;]<br>      cookies {<br>        forward = &quot;none&quot;<br>      }<br>    }</pre><pre>    min_ttl     = 0<br>    default_ttl = 0<br>    max_ttl     = 0<br>  }<br>}</pre><p>This ensures:</p><ul><li>API is never cached</li><li>headers needed for auth are forwarded</li><li>still low cost</li></ul><h4>7.2.5 Pass API Domain into edge_frontend module call (envs/dev)</h4><p>Where you call module &quot;edge_frontend&quot; in envs/dev, add:</p><pre>api_origin_domain = module.api_gateway.api_domain</pre><p>So it becomes:</p><pre>module &quot;edge_frontend&quot; {<br>  source = &quot;../../modules/edge_frontend&quot;</pre><pre>  name = local.frontend_bucket_name<br>  tags = local.tags</pre><pre>  api_origin_domain = module.api_gateway.api_domain<br>}</pre><h4>7.2.6 Terraform Apply (Execution)</h4><p>Run:</p><pre>terraform -chdir=envs/dev init<br>terraform -chdir=envs/dev apply</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HvzxgEbncLZ-UVdBBMzMtA.png" /></figure><h4>Step 7.3 Verification Checkpoint (Terminal)</h4><p>First, we need to add an output block to output.tf file.</p><p>Add this:</p><pre>##############################################<br># Frontend Outputs<br>##############################################</pre><pre>output &quot;cloudfront_domain_name&quot; {<br>  description = &quot;CloudFront domain name for the frontend&quot;<br>  value       = module.edge_frontend.cloudfront_domain_name<br>}</pre><pre>output &quot;cloudfront_distribution_id&quot; {<br>  description = &quot;CloudFront distribution ID&quot;<br>  value       = module.edge_frontend.cloudfront_distribution_id<br>}</pre><pre>##############################################<br># Backend API Outputs<br>##############################################</pre><pre>output &quot;api_endpoint&quot; {<br>  description = &quot;Direct API Gateway endpoint&quot;<br>  value       = module.api_gateway.api_endpoint<br>}</pre><pre>output &quot;api_domain&quot; {<br>  description = &quot;API Gateway domain without https:// for CloudFront origin&quot;<br>  value       = replace(module.api_gateway.api_endpoint, &quot;https://&quot;, &quot;&quot;)<br>}</pre><h4>Step 7.2.3 — Ensure your modules export the config</h4><p>After apply, run:</p><pre>terraform -chdir=envs/dev output</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F2hiWhzC97ZL8jXA4NAomA.png" /></figure><p>Copy your API endpoint (it will look like <a href="https://xxxx.execute-api...)/">https://xxxx.execute-api...)</a></p><p>Then test:</p><pre>curl.exe -s https://&lt;API_ENDPOINT&gt;/health</pre><p>Expected:</p><pre>{&quot;status&quot;:&quot;ok&quot;,&quot;time&quot;:&quot;2025-12-27T...Z&quot;}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SRTCMUkaf0_I4rvf8M3cbg.png" /></figure><h4>2) Verify CloudFront routes /api/health</h4><pre>curl.exe -s https://d3wtrfsoyfqlb.cloudfront.net/api/health</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ub0kreusR6pwkx2gZiSZ1A.png" /></figure><h4>Step 7.4 — GitHub Actions CI/CD Workflows (Plan + Apply + Approval + Drift)</h4><h4>Enable the following files inside</h4><pre>.github/workflows/</pre><p>Inside that folder, create <strong>4 files</strong>:</p><p>terraform-plan.yml<br> terraform-apply-dev.yml<br> terraform-apply-prod.yml<br> drift-detect.yml</p><h4>7.4.1 Terraform Plan Workflow (Runs on PR)</h4><p>File: <strong>.github/workflows/terraform-plan.yml</strong></p><pre>name: Terraform Plan (PR)</pre><pre>on:<br>  pull_request:<br>    branches: [main]<br>    paths:<br>      - &quot;modules/**&quot;<br>      - &quot;envs/**&quot;</pre><pre>jobs:<br>  plan:<br>    runs-on: ubuntu-latest</pre><pre>    steps:<br>      - name: Checkout<br>        uses: actions/checkout@v4</pre><pre>      - name: Setup Terraform<br>        uses: hashicorp/setup-terraform@v3</pre><pre>      - name: Terraform Init (Dev)<br>        run: terraform -chdir=envs/dev init</pre><pre>      - name: Terraform Plan (Dev)<br>        run: terraform -chdir=envs/dev plan</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5eeA0Q_hLrRZiJCQywemdw.png" /></figure><p>This ensures every PR gets a full infra impact check.<br>No infra changes happen here.</p><h4>7.4.2 Terraform Apply Dev Workflow (Auto apply on merge)</h4><p>File: <strong>.github/workflows/terraform-apply-dev.yml</strong></p><pre>name: Terraform Apply Dev</pre><pre>on:<br>  push:<br>    branches: [main]</pre><pre>jobs:<br>  apply:<br>    runs-on: ubuntu-latest</pre><pre>    steps:<br>      - name: Checkout<br>        uses: actions/checkout@v4</pre><pre>      - name: Setup Terraform<br>        uses: hashicorp/setup-terraform@v3</pre><pre>      - name: Terraform Init (Dev)<br>        run: terraform -chdir=envs/dev init</pre><pre>      - name: Terraform Apply (Dev)<br>        run: terraform -chdir=envs/dev apply -auto-approve</pre><p>Every merge into main deploys to dev automatically.<br> Controlled, consistent deployment.</p><h4>7.4.3 Terraform Apply Prod Workflow (Manual Approval)</h4><p>File: <strong>.github/workflows/terraform-apply-prod.yml</strong></p><pre>name: Terraform Apply Prod (Manual)</pre><pre>on:<br>  workflow_dispatch:</pre><pre>jobs:<br>  apply:<br>    runs-on: ubuntu-latest<br>    environment: prod</pre><pre>    steps:<br>      - name: Checkout<br>        uses: actions/checkout@v4</pre><pre>      - name: Setup Terraform<br>        uses: hashicorp/setup-terraform@v3</pre><pre>      - name: Terraform Init (Prod)<br>        run: terraform -chdir=envs/prod init</pre><pre>      - name: Terraform Apply (Prod)<br>        run: terraform -chdir=envs/prod apply -auto-approve</pre><p>workflow_dispatch means you manually run it.<br> environment: prod enforces GitHub approval gates.</p><h4>7.5 Drift Detection Workflow (Runs daily)</h4><p>File: <strong>.github/workflows/drift-detect.yml</strong></p><pre>name: Drift Detection</pre><pre>on:<br>  schedule:<br>    - cron: &quot;0 6 * * *&quot;<br>  workflow_dispatch:</pre><pre>jobs:<br>  drift:<br>    runs-on: ubuntu-latest</pre><pre>    steps:<br>      - name: Checkout<br>        uses: actions/checkout@v4</pre><pre>      - name: Setup Terraform<br>        uses: hashicorp/setup-terraform@v3</pre><pre>      - name: Terraform Init<br>        run: terraform -chdir=envs/dev init</pre><pre>      - name: Drift Plan Check<br>        run: terraform -chdir=envs/dev plan -detailed-exitcode</pre><h4>Phase B— Policy-as-Code (OPA + Conftest)</h4><p>Now we add security enforcement at PR time.</p><p>We will enforce:</p><p>“No public S3 buckets”<br>“CloudFront must redirect to HTTPS”<br>“Lambda must not run with wildcard IAM”</p><h4>Step B.1 Create this folder:</h4><pre>policy/</pre><p>Inside create file:</p><p>policy/terraform.rego</p><h4>policy/terraform.rego</h4><pre>package terraform.policies</pre><pre>deny[msg] {<br>  input.resource_changes[_].type == &quot;aws_s3_bucket_public_access_block&quot;<br>  input.resource_changes[_].change.after.block_public_acls == false<br>  msg := &quot;S3 Public ACLs must be blocked&quot;<br>}</pre><pre>deny[msg] {<br>  input.resource_changes[_].type == &quot;aws_s3_bucket_public_access_block&quot;<br>  input.resource_changes[_].change.after.block_public_policy == false<br>  msg := &quot;S3 Public Bucket Policies must be blocked&quot;<br>}</pre><pre>deny[msg] {<br>  input.resource_changes[_].type == &quot;aws_cloudfront_distribution&quot;<br>  input.resource_changes[_].change.after.default_cache_behavior.viewer_protocol_policy != &quot;redirect-to-https&quot;<br>  msg := &quot;CloudFront must redirect viewers to HTTPS&quot;<br>}</pre><p>This blocks insecure infrastructure <strong>before it reaches apply</strong>.</p><h4>Step B.2 Add Conftest Check to PR Plan Workflow</h4><p>Update:</p><p>📌 .github/workflows/terraform-plan.yml</p><p>Add these steps after terraform plan:</p><pre>- name: Install Conftest<br>        run: |<br>          wget https://github.com/open-policy-agent/conftest/releases/latest/download/conftest_0.56.0_Linux_x86_64.tar.gz<br>          tar xzf conftest_0.56.0_Linux_x86_64.tar.gz<br>          sudo mv conftest /usr/local/bin/</pre><pre>      - name: Terraform Plan JSON Export<br>        run: |<br>          terraform -chdir=envs/dev plan -out=tfplan<br>          terraform -chdir=envs/dev show -json tfplan &gt; plan.json</pre><pre>      - name: Conftest Policy Check<br>        run: conftest test plan.json -p policy/</pre><p>Now PR checks include policy enforcement.</p><h4>Now lets push everything to github</h4><p>Run exactly these:</p><pre>git add .<br>git commit -m &quot;Phase 2: add API + workflows + edge routing&quot;<br>git push</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OFtKFTuRtrVNGq3U-YSr0A.png" /></figure><p>We have an error because <strong>GitHub Actions has NO AWS credentials</strong>, so Terraform cannot access the S3 backend or DynamoDB lock table.</p><h4>All we need to do is add AWS Auth beforeTerraform init in our terraform apply workflow:</h4><pre>- name: Configure AWS Credentials<br>  uses: aws-actions/configure-aws-credentials@v4<br>  with:<br>    aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}<br>    aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}<br>    aws-region: ${{ secrets.AWS_REGION }}</pre><h4>Attach policy to GitHub Actions IAM user</h4><h4>Create policy: TerraformBackendAccess</h4><p>Go to:</p><p>IAM → Policies → Create Policy → JSON<br> Paste this:</p><pre>{<br>  &quot;Version&quot;: &quot;2012-10-17&quot;,<br>  &quot;Statement&quot;: [<br>    {<br>      &quot;Sid&quot;: &quot;TerraformStateBucketAccess&quot;,<br>      &quot;Effect&quot;: &quot;Allow&quot;,<br>      &quot;Action&quot;: [<br>        &quot;s3:ListBucket&quot;<br>      ],<br>      &quot;Resource&quot;: &quot;arn:aws:s3:::tf-state-559938827680-devops&quot;<br>    },<br>    {<br>      &quot;Sid&quot;: &quot;TerraformStateObjectAccess&quot;,<br>      &quot;Effect&quot;: &quot;Allow&quot;,<br>      &quot;Action&quot;: [<br>        &quot;s3:GetObject&quot;,<br>        &quot;s3:PutObject&quot;,<br>        &quot;s3:DeleteObject&quot;<br>      ],<br>      &quot;Resource&quot;: &quot;arn:aws:s3:::tf-state-559938827680-devops/*&quot;<br>    },<br>    {<br>      &quot;Sid&quot;: &quot;TerraformLockTableAccess&quot;,<br>      &quot;Effect&quot;: &quot;Allow&quot;,<br>      &quot;Action&quot;: [<br>        &quot;dynamodb:GetItem&quot;,<br>        &quot;dynamodb:PutItem&quot;,<br>        &quot;dynamodb:DeleteItem&quot;,<br>        &quot;dynamodb:DescribeTable&quot;<br>      ],<br>      &quot;Resource&quot;: &quot;arn:aws:dynamodb:us-east-1:559938827680:table/terraform-locks&quot;<br>    }<br>  ]<br>}</pre><p>Then:</p><p>IAM → Users → your GitHub actions user → Attach this policy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6pnJOMorT1uKlm4TUuMnjw.png" /></figure><h4>After fixing IAM policy</h4><p>Click:</p><p>Actions → Terraform Apply Dev → Re-run jobs</p><p>Terraform Init should now succeed <br> Apply should now run.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uf_p4RSq5KwdK9haD8EvHQ.png" /></figure><h4>Step 7.6 — Production Promotion (Manual Apply + Approval Gates)</h4><p>At this stage, the platform is ready for production promotion.<br> In real infrastructure teams, production deployments must be:</p><ul><li>Explicitly triggered</li><li>Reviewed and approved</li><li>Isolated from development state and development secrets</li><li>Fully traceable through CI logs</li><li>Re-runnable and recoverable</li></ul><p>Terraform can deploy to production the same way it deploys to development, but governance is intentionally added to reduce risk and enforce control.</p><h4>Why “Manual Apply” Matters</h4><p>Development environments can deploy automatically on merge, but production should require:</p><ul><li>A conscious decision</li><li>Human review</li><li>A clear approval boundary</li></ul><p>This is exactly what <strong>GitHub Environments</strong> provide:</p><ul><li>Protected deployments</li><li>Required reviewers</li><li>Environment-level secrets</li></ul><h4>What Was Implemented</h4><p>The production deployment workflow was designed with governance built in:</p><ul><li>A Terraform Apply Prod (Manual) workflow was created</li><li>It was scoped to the prod environment</li><li>AWS credentials were stored as prod environment secrets</li><li>Required reviewers were optionally added for approval</li></ul><p>This means that even if someone triggers the workflow manually, it cannot proceed without authorization.</p><h4>GitHub Environment Setup (Prod)</h4><p>Navigate to:</p><p><strong>GitHub → Settings → Environments → prod</strong></p><p>Then configure the following:</p><p><strong>Environment Secrets</strong></p><ul><li>AWS_ACCESS_KEY_ID</li><li>AWS_SECRET_ACCESS_KEY</li><li>AWS_REGION</li><li>ALERT_EMAIL</li></ul><p><strong>Required Reviewers</strong></p><ul><li>Add your GitHub account (or your team members) as required reviewers</li></ul><p><strong>Deployment Restrictions (Optional)</strong></p><ul><li>Restrict deployments to main only</li></ul><p>This creates a clear, enforceable production boundary — effectively a “production wall” — ensuring production cannot be modified without explicit intent and approval.</p><p>This gives our infrastructure a real “production wall”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q-KFmtYyrOpVQ80hV4f-5A.png" /></figure><h4>Terraform Apply Prod Workflow (Manual)</h4><p>.github/workflows/terraform-apply.prod.yml</p><pre>name: Terraform Apply Prod (Manual)</pre><pre>on:<br>  workflow_dispatch:</pre><pre>concurrency:<br>  group: terraform-prod<br>  cancel-in-progress: false</pre><pre>jobs:<br>  apply:<br>    runs-on: ubuntu-latest<br>    timeout-minutes: 60<br>    environment: prod</pre><pre>    env:<br>      TF_INPUT: &quot;false&quot;<br>      TF_IN_AUTOMATION: &quot;true&quot;</pre><pre>    steps:<br>      - uses: actions/checkout@v4</pre><pre>      - uses: hashicorp/setup-terraform@v3</pre><pre>      - uses: aws-actions/configure-aws-credentials@v4<br>        with:<br>          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}<br>          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}<br>          aws-region: ${{ secrets.AWS_REGION }}</pre><pre>      - name: Create terraform.tfvars (Prod)<br>        run: |<br>          cat &lt;&lt;EOF &gt; envs/prod/terraform.tfvars<br>          alert_email = &quot;${{ secrets.ALERT_EMAIL }}&quot;<br>          enable_dns  = false<br>          domain_name = &quot;&quot;<br>          EOF</pre><pre>      - name: Terraform init (Prod)<br>        run: terraform -chdir=envs/prod init -input=false -lock-timeout=10m</pre><pre>      - name: Terraform validate (Prod)<br>        run: terraform -chdir=envs/prod validate</pre><pre>      - name: Terraform apply (Prod)<br>        run: terraform -chdir=envs/prod apply -lock-timeout=10m -auto-approve -input=false -no-color -var-file=terraform.tfvars</pre><p>This is important because:</p><ul><li>prod terraform.tfvars is generated dynamically</li><li>no prod secrets are committed to git</li><li>CI can run safely without risking accidental leaks</li></ul><h4>Production Apply Execution (Proof)</h4><p>When triggered in GitHub Actions, the workflow:</p><p>sets up Terraform<br> configures AWS credentials<br> initializes the prod backend<br> applies infra changes<br> exits successfully</p><p>This is the final proof that the system is now production-ready.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DtAuRl87bjJlKW5cY7JbFQ.png" /></figure><h4>Phase 2 Outcome</h4><p>By the end of Phase 2, the project supports:</p><ul><li>A secure edge frontend</li><li>A serverless backend API behind CloudFront</li><li>Reliable Terraform automation with state locking</li><li>Dev auto-apply workflow</li><li>Prod approval-based deployment workflow</li><li>Drift detection and operational tooling for state locks</li><li>Optional policy enforcement for infra standards</li></ul><p>This is no longer a demo repository. It mirrors the structure and discipline used in modern platform engineering teams.</p><h4>Challenges Encountered and How They Were Resolved</h4><h4>1. Terraform State Locking Issues (DynamoDB ConditionalCheckFailedException)</h4><p>Terraform repeatedly failed to acquire state locks due to incomplete previous operations.</p><p><strong>Resolution</strong></p><ul><li>Added concurrency control in GitHub Actions to prevent parallel applies</li><li>Added -lock-timeout to Terraform commands</li><li>Created a manual Terraform force-unlock workflow for safe operational recovery</li><li>Cleaned up lock records when orphaned</li></ul><h4>2. Terraform Apply Hanging on Missing Variables (var.alert_email)</h4><p>GitHub Actions stalled waiting for required variables because .tfvars were not being loaded.</p><p><strong>Resolution</strong></p><ul><li>Changed workflows to explicitly pass var files (-var-file=terraform.tfvars)</li><li>Introduced terraform.tfvars.example for documentation while generating sensitive .tfvars dynamically in CI for prod</li></ul><h4>3. CloudFront /api/* Routing Returning 403 Errors</h4><p>CloudFront forwarded traffic to API Gateway but routing failed due to improper cache behavior configuration and header handling.</p><p><strong>Resolution</strong></p><ul><li>Introduced ordered cache behavior /api/*</li><li>Ensured correct forwarded headers</li><li>Removed unsupported Host header injection (CloudFront rejects it)</li></ul><h4>4. Workflow Failures Due to Merge Conflicts and YAML Errors</h4><p>Terraform workflows failed to run due to invalid YAML caused by unresolved conflict markers.</p><p><strong>Resolution</strong></p><ul><li>Cleaned workflow YAML, removed merge markers, and validated file syntax</li><li>Re-tested workflows through GitHub Actions logs</li></ul><h4>5. Terraform Formatting Failures (terraform fmt -check)</h4><p>Terraform validation failed due to formatting drift across module files.</p><p><strong>Resolution</strong></p><ul><li>Added terraform fmt -check -recursive as a quality gate</li><li>Ran terraform fmt -recursive locally and enforced consistent formatting in PR workflows</li></ul><h4>6. AWS AssumeRole Access Denied Errors</h4><p>Terraform could not assume execution roles due to missing IAM permissions.</p><p><strong>Resolution</strong></p><ul><li>Updated IAM trust relationships and attached sts:AssumeRole permissions</li><li>Ensured the role and GitHub Actions principal were aligned properly</li><li>Verified with successful workflow execution</li></ul><h4>Final Result</h4><p>The project now contains the required maturity patterns for a real-world infrastructure platform:</p><ul><li>Safe state management</li><li>Controlled deployments</li><li>Environment-based governance</li><li>Production approval gates</li><li>Drift detection</li><li>Policy enforcement</li><li>Operational unlock tooling</li></ul><p>These are the exact practices that make Terraform production-grade.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8616921ba6c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Modern Terraform Project Like a Pro]]></title>
            <link>https://medium.com/@shehuyusuf/building-a-modern-terraform-project-like-a-pro-f90ca8e18da2?source=rss-607ba1670d2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f90ca8e18da2</guid>
            <dc:creator><![CDATA[Shehu Yusuf]]></dc:creator>
            <pubDate>Sat, 20 Dec 2025 22:34:41 GMT</pubDate>
            <atom:updated>2025-12-20T22:34:41.334Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>A production-ready, cost-controlled AWS project using Terraform — designed the way real platform teams work.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tlgedsEGraiXqf1PRT8K8g.png" /></figure><h4>Why This Article Exists</h4><p>I see engineers struggle with Terraform all the time.<br> Not because Terraform is bad — but because it’s often <strong>used the wrong way from day one</strong>.</p><p>Most Terraform pain comes down to these <strong>five mistakes</strong>:</p><ul><li>Treating Terraform like Python or JavaScript instead of a <strong>declarative system</strong></li><li>Writing one giant monolithic configuration instead of <strong>modular design</strong></li><li>Keeping state files locally or committing them to Git</li><li>Not planning for team collaboration early (remote state, CI/CD)</li><li>Hard-coding values instead of using variables and abstractions</li></ul><p>This article walks through building a <strong>modern Terraform project from scratch</strong>, deliberately avoiding every one of those mistakes — while also keeping AWS costs low (important when your credits are limited).</p><p>By the end, you’ll have:</p><ul><li>A production-grade Terraform structure</li><li>Remote state with locking</li><li>Cost guardrails from day one</li><li>A project you can confidently publish, share, and extend</li></ul><h4>Project Goal</h4><p>We’re going to build a <strong>realistic, modern AWS platform</strong> using Terraform — not a toy example.</p><h4>Architecture (High Level)</h4><ul><li><strong>CloudFront</strong> — secure edge delivery (HTTPS)</li><li><strong>API Gateway (HTTP API)</strong> — low-cost API entry point</li><li><strong>AWS Lambda</strong> — serverless compute</li><li><strong>DynamoDB (on-demand)</strong> — serverless data storage</li><li><strong>S3 + DynamoDB</strong> — Terraform remote state &amp; locking</li><li><strong>AWS Budgets</strong> — cost control from day one</li><li><strong>GitHub Actions</strong> — CI/CD for Terraform</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xnXGUc_UWC9YsYzv6r-XVA.png" /></figure><h4>Why This Project Stands Out</h4><p>Most Terraform tutorials:</p><ul><li>Use EC2 + NAT Gateways (expensive)</li><li>Ignore cost controls</li><li>Skip CI/CD</li><li>Collapse everything into one folder</li></ul><p>This project:</p><ul><li>Uses <strong>serverless, low-cost AWS services</strong></li><li>Is <strong>safe for small budgets</strong></li><li>Matches how <strong>real teams structure Terraform</strong></li><li>Is <strong>publishable and portfolio-ready</strong></li></ul><h4>Step 1: Create the Repository Structure (Do This Before Writing Any Terraform)</h4><p>The biggest Terraform mistake happens <strong>before the first resource is written</strong>:<br> starting without structure.</p><h4>Production-Grade Folder Layout</h4><p>Create a new repository and add the following structure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Iseva7SbjUkgiHPW6C2I6Q.png" /></figure><h4>Why This Structure Matters</h4><ul><li><strong>Modules</strong> contain reusable, opinionated infrastructure patterns</li><li><strong>envs/</strong> defines environments explicitly (dev ≠ prod)</li><li><strong>One state file per environment</strong></li><li>CI/CD workflows are built in from the start</li></ul><p>This single decision prevents:</p><ul><li>Monolithic Terraform files</li><li>Accidental production changes</li><li>State corruption</li><li>Unreviewed infrastructure changes</li></ul><h4>Step 2: Bootstrap Remote State (Never Use Local State for Real Projects)</h4><p>Terraform <strong>needs state</strong> to understand what it manages.<br> Local state is fine for learning — <strong>not for real projects</strong>.</p><p>We’ll create:</p><ul><li>An <strong>S3 bucket</strong> for remote state</li><li>A <strong>DynamoDB table</strong> for state locking</li></ul><p>This is a <strong>one-time bootstrap step</strong>.</p><h4>Create the State Backend Module</h4><p><strong>modules/state_backend/main.tf</strong></p><pre>terraform {<br>  required_version = &quot;&gt;= 1.6.0&quot;<br>  required_providers {<br>    aws = {<br>      source  = &quot;hashicorp/aws&quot;<br>      version = &quot;~&gt; 5.0&quot;<br>    }<br>  }<br>}</pre><pre>variable &quot;region&quot; {<br>  type    = string<br>  default = &quot;us-east-1&quot;<br>}</pre><pre>variable &quot;state_bucket_name&quot; {<br>  type = string<br>}</pre><pre>variable &quot;lock_table_name&quot; {<br>  type    = string<br>  default = &quot;terraform-locks&quot;<br>}</pre><pre>provider &quot;aws&quot; {<br>  region = var.region<br>}</pre><pre>resource &quot;aws_s3_bucket&quot; &quot;state&quot; {<br>  bucket = var.state_bucket_name<br>}</pre><pre>resource &quot;aws_s3_bucket_versioning&quot; &quot;state&quot; {<br>  bucket = aws_s3_bucket.state.id<br>  versioning_configuration {<br>    status = &quot;Enabled&quot;<br>  }<br>}</pre><pre>resource &quot;aws_s3_bucket_server_side_encryption_configuration&quot; &quot;state&quot; {<br>  bucket = aws_s3_bucket.state.id<br>  rule {<br>    apply_server_side_encryption_by_default {<br>      sse_algorithm = &quot;AES256&quot;<br>    }<br>  }<br>}</pre><pre>resource &quot;aws_s3_bucket_public_access_block&quot; &quot;state&quot; {<br>  bucket                  = aws_s3_bucket.state.id<br>  block_public_acls       = true<br>  block_public_policy     = true<br>  ignore_public_acls      = true<br>  restrict_public_buckets = true<br>}</pre><pre>resource &quot;aws_dynamodb_table&quot; &quot;locks&quot; {<br>  name         = var.lock_table_name<br>  billing_mode = &quot;PAY_PER_REQUEST&quot;<br>  hash_key     = &quot;LockID&quot;</pre><pre>  attribute {<br>    name = &quot;LockID&quot;<br>    type = &quot;S&quot;<br>  }<br>}</pre><h4>Bootstrap the Backend</h4><p>Create a temporary folder:</p><pre>bootstrap/<br>└── main.tf</pre><p><strong>bootstrap/main.tf</strong></p><pre>module &quot;state&quot; {<br>  source            = &quot;../modules/state_backend&quot;<br>  state_bucket_name = &quot;your-unique-terraform-state-bucket&quot;<br>}</pre><p>Run:</p><pre>cd bootstrap<br>terraform init<br>terraform fmt<br>terraform validate<br>terraform apply</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tGh41WO_LBV46IUAFZ-rWA.png" /></figure><p>This error has <strong>nothing to do with Terraform</strong> and <strong>everything to do with IAM</strong>.</p><p>Terraform:</p><ul><li>Authenticated successfully</li><li>Generated a correct execution plan</li><li>Attempted to create AWS resources</li></ul><p>AWS then said:</p><blockquote><em>“This IAM user does not have permission to create S3 buckets or DynamoDB tables.”</em></blockquote><p>That’s <strong>good security</strong>, not a problem.</p><h4>Why This Happens in Real Companies</h4><p>In real environments:</p><ul><li>Engineers <strong>do not get full admin access</strong></li><li>IAM users are intentionally restricted</li><li>Bootstrap resources (state, locking) often require elevated permissions</li></ul><p><strong>So let’s fix it:</strong></p><p>Create:</p><ul><li>an <strong>S3 bucket</strong> (remote state)</li><li>a <strong>DynamoDB table</strong> (state locking)</li></ul><p>Your IAM identity must allow:</p><h4>Required AWS Permissions</h4><pre>{<br>  &quot;Effect&quot;: &quot;Allow&quot;,<br>  &quot;Action&quot;: [<br>    &quot;s3:CreateBucket&quot;,<br>    &quot;s3:PutBucketVersioning&quot;,<br>    &quot;s3:PutBucketEncryption&quot;,<br>    &quot;s3:PutBucketPublicAccessBlock&quot;,<br>    &quot;dynamodb:CreateTable&quot;<br>  ],<br>  &quot;Resource&quot;: &quot;*&quot;<br>}</pre><p>First apply a temporary bootstrap policy</p><h4>Step 1: Go to IAM Console</h4><ul><li>Open <strong>IAM → Users</strong></li><li>Select your user: devops-user</li><li>Go to <strong>Permissions → Add permissions</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-5QFN1XBboKmPBHDxmG6dg.png" /></figure><h4>Step 2: Attach One of These (Choose ONE)</h4><h4>Easiest (Temporary)</h4><p>Attach AWS managed policy:</p><pre>AdministratorAccess</pre><blockquote><em>Use </em><strong><em>only for bootstrap</em></strong><em>, then remove it.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sq2Io2mVsgT-SwsKXO1f7g.png" /></figure><h4>Step 3: Retry Terraform Apply</h4><pre>cd bootstrap<br>terraform apply</pre><p>Enter:</p><pre>yes</pre><h4>Expected Successful Output</h4><p>Terraform should now create:</p><ul><li>S3 bucket for state</li><li>DynamoDB table for locks</li></ul><p>You’ll see:</p><pre>Apply complete! Resources: 5 added, 0 changed, 0 destroyed.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0QUdYGPdrk-sYyuy6RUiMw.png" /></figure><p>Leaving AdministratorAccess attached is <strong>dangerous and unnecessary</strong>.</p><p>In real production environments:</p><ul><li>Terraform does <strong>not</strong> run as admin forever</li><li>Elevated permissions are used <strong>only for bootstrapping</strong></li><li>Long-lived admin access is a security anti-pattern</li><li>Our current setup is already <strong>better than most tutorials online</strong> — now let’s finish it properly by removing <strong>AdministativeAccess.</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VnyAvVSk-FnzdemG0fd86w.png" /></figure><h4>Step 3: Add Cost Control Immediately (Before Anything Else)</h4><p>Cost control should never be an afterthought.</p><p>We’ll enforce a <strong>monthly AWS budget</strong> using Terraform itself.</p><h4>Create the Budget Module</h4><p><strong>modules/budgets/main.tf</strong></p><pre>variable &quot;monthly_limit_usd&quot; {<br>  type = number<br>}</pre><pre>variable &quot;email&quot; {<br>  type = string<br>}</pre><pre>resource &quot;aws_budgets_budget&quot; &quot;monthly&quot; {<br>  name         = &quot;terraform-monthly-budget&quot;<br>  budget_type  = &quot;COST&quot;<br>  limit_amount = tostring(var.monthly_limit_usd)<br>  limit_unit   = &quot;USD&quot;<br>  time_unit    = &quot;MONTHLY&quot;<br>}</pre><pre>resource &quot;aws_budgets_budget_notification&quot; &quot;alert&quot; {<br>  budget_name         = aws_budgets_budget.monthly.name<br>  notification_type  = &quot;ACTUAL&quot;<br>  comparison_operator = &quot;GREATER_THAN&quot;<br>  threshold           = 80<br>  threshold_type      = &quot;PERCENTAGE&quot;</pre><pre>  subscriber_email_addresses = [var.email]<br>}</pre><p>Terraform successfully creates the AWS Budget, but budget visibility in the AWS console depends on account type, billing configuration, and AWS UI behavior. In some accounts, budgets are active and enforce alerts even when the console does not list them. Terraform and the AWS Budgets API are the source of truth.</p><h4>Step 4: Configure the Development Environment (Team-Ready Terraform)</h4><p>With the bootstrap phase complete, we now move into the <strong>actual working phase</strong> of the project.</p><p>This is an important mental shift.</p><blockquote><em>From this point forward, </em><strong><em>we never run Terraform from the root or bootstrap directories again</em></strong><em>.</em></blockquote><p>Instead, we work from <strong>explicit environment folders</strong>.<br> This mirrors how Terraform is used in real companies.</p><h4>Why Environments Matter</h4><p>In production systems:</p><ul><li>dev is allowed to break</li><li>prod is not</li><li>state must never be shared between them</li><li>access, blast radius, and lifecycle differ</li></ul><p>Terraform enforces this separation <strong>through state</strong>.</p><p>That’s why we created:</p><pre>envs/dev<br>envs/prod</pre><p>Each environment:</p><ul><li>has its own backend</li><li>has its own state file</li><li>can be deployed independently</li></ul><h4>4.1 Configure the Remote Backend for dev</h4><p>Now that our S3 bucket and DynamoDB table exist, we tell Terraform <strong>where the dev state lives</strong>.</p><p>Create or verify the following file:</p><h4>envs/dev/backend.tf</h4><pre>terraform {<br>  backend &quot;s3&quot; {<br>    bucket         = &quot;tf-state-559938827680-devops&quot;<br>    key            = &quot;modern/dev/terraform.tfstate&quot;<br>    region         = &quot;us-east-1&quot;<br>    dynamodb_table = &quot;terraform-locks&quot;<br>    encrypt        = true<br>  }<br>}</pre><h4>What this does</h4><ul><li>Stores dev state in S3 (not locally)</li><li>Locks state with DynamoDB (no concurrent applies)</li><li>Ensures encryption at rest</li><li>Isolates dev from prod by using a unique key</li></ul><p>This single file prevents:</p><ul><li>state corruption</li><li>accidental prod changes</li><li>“works on my laptop” Terraform</li></ul><h4>Common Beginner Mistakes (Avoided Here)</h4><ul><li>Using one state file for everything</li><li>Running Terraform from random folders</li><li>Sharing .tfstate files manually</li><li>Letting multiple engineers apply at once</li></ul><h4>4.2 Define Provider Configuration and Default Tags</h4><p>Next, we define <strong>how Terraform talks to AWS</strong> for this environment.</p><h4>envs/dev/main.tf</h4><pre>terraform {<br>  required_version = &quot;&gt;= 1.6.0&quot;</pre><pre>  required_providers {<br>    aws = {<br>      source  = &quot;hashicorp/aws&quot;<br>      version = &quot;~&gt; 5.0&quot;<br>    }<br>  }<br>}</pre><pre>provider &quot;aws&quot; {<br>  region = var.region</pre><pre>  default_tags {<br>    tags = {<br>      project    = var.project<br>      env        = var.env<br>      owner      = var.owner<br>      managed-by = &quot;terraform&quot;<br>    }<br>  }<br>}</pre><h4>Why default tags matter in real systems</h4><p>In real AWS accounts:</p><ul><li>cost tracking relies on tags</li><li>security teams audit tags</li><li>cleanup scripts depend on tags</li><li>humans debug infra using tags</li></ul><p>By enforcing tags at the provider level:</p><ul><li>no resource can “forget” to tag itself</li><li>consistency is guaranteed</li></ul><p>This is a <strong>professional Terraform pattern</strong>.</p><h4>4.3 Define Environment Variables Explicitly</h4><p>Terraform should never rely on hard-coded values.</p><p>Create:</p><h4>envs/dev/variables.tf</h4><pre>variable &quot;region&quot; {<br>  type        = string<br>  description = &quot;AWS region for this environment&quot;<br>}</pre><pre>variable &quot;project&quot; {<br>  type        = string<br>  description = &quot;Project name used for tagging and naming&quot;<br>}</pre><pre>variable &quot;env&quot; {<br>  type        = string<br>  description = &quot;Environment name (dev or prod)&quot;<br>}</pre><pre>variable &quot;owner&quot; {<br>  type        = string<br>  description = &quot;Owner or team name&quot;<br>}</pre><pre>variable &quot;alert_email&quot; {<br>  type        = string<br>  description = &quot;Email address for cost and budget alerts&quot;<br>}</pre><pre>variable &quot;monthly_budget_usd&quot; {<br>  type        = number<br>  description = &quot;Monthly AWS budget limit for this environment&quot;<br>}</pre><h4>4.4 Set Dev Values Using terraform.tfvars</h4><p>Now we provide <strong>actual values</strong> for the dev environment.</p><h4>envs/dev/terraform.tfvars</h4><pre>region             = &quot;us-east-1&quot;<br>project            = &quot;terraform-modern-aws&quot;<br>env                = &quot;dev&quot;<br>owner              = &quot;devops&quot;<br>alert_email        = &quot;your-email@example.com&quot;<br>monthly_budget_usd = 25</pre><h4>Why this file matters</h4><ul><li>Keeps configuration clean</li><li>Allows different values per environment</li><li>Avoids hard-coding</li><li>Works cleanly with CI/CD later</li></ul><h4>4.5 Initialize Terraform in the Dev Environment</h4><p>This is a <strong>critical discipline rule</strong>:</p><blockquote><em>Terraform commands must be run </em><strong><em>from the environment directory</em></strong><em>.</em></blockquote><p>Run:</p><pre>cd envs/dev<br>terraform init<br>terraform fmt<br>terraform validate<br>terraform plan</pre><h4>What should happen</h4><ul><li>Terraform configures the <strong>remote S3 backend</strong></li><li>State is no longer local</li><li>The plan shows expected resources only (e.g. budget)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tU-T3LRB8zvTHrjD9Ocjvw.png" /></figure><p>That tells us:</p><ul><li>State is stored in <strong>S3</strong></li><li>Locking is enforced via <strong>DynamoDB</strong></li><li>Concurrent applies are prevented</li></ul><p>This confirms our backend setup is <strong>working correctly</strong>.</p><h4>Important Clarification</h4><p>At this stage:</p><ul><li>Terraform is correctly managing infrastructure</li><li>State is safely stored remotely</li><li>Locking is enforced</li><li>Cost guardrails exist</li></ul><p>Even if:</p><ul><li>AWS Budgets UI is unreliable</li><li>Console views lag</li><li>Some services are hidden</li></ul><p>Terraform and AWS APIs remain the <strong>source of truth</strong>.</p><p>This is how real platforms operate.</p><h4>Step 5: Build the Edge Layer (CloudFront + ACM) — Modern HTTPS Front Door (Near-Zero Idle Cost)</h4><p>Now that our dev environment is stable (remote state, locking, IAM, and cost guardrails), we’ll build the first piece of <em>real</em> production infrastructure:</p><blockquote><em>A secure HTTPS “front door” using </em><strong><em>CloudFront</em></strong><em> (CDN) + </em><strong><em>ACM</em></strong><em> (TLS certificates)</em></blockquote><p>This is a modern pattern because:</p><ul><li>CloudFront gives you a global edge endpoint (fast, scalable, stable)</li><li>ACM gives you free public TLS certificates</li><li>There’s <strong>no always-on compute cost</strong> like ALBs/EC2</li></ul><h4>Why this matters in production</h4><p>In real systems, “the edge” is where you enforce:</p><ul><li>HTTPS everywhere</li><li>caching and performance</li><li>security headers</li><li>predictable entry points for clients</li></ul><p>CloudFront is also a “blast radius reducer”: your backend can change without clients ever seeing new URLs.</p><h4>Common beginner mistakes</h4><ul><li>Using an ALB for everything (can be overkill + cost)</li><li>Skipping HTTPS setup until late (then it becomes painful)</li><li>Trying to create a certificate in the wrong region</li><li>Making an S3 website bucket public (security anti-pattern)</li></ul><h4>Our Step 5 plan will have two phases</h4><h4>Phase A (works without owning a domain)</h4><p>Create an S3 bucket for a static site<br>Create a CloudFront distribution using the S3 bucket as origin<br>Use the default CloudFront domain (xxxx.cloudfront.net) over HTTPS</p><p>This is perfect for learning and keeps costs low.</p><h4>Phase B (optional, if you own a domain in Route 53)</h4><p>Request an ACM certificate (must be in <strong>us-east-1</strong> for CloudFront)<br>Validate via DNS<br>Add Route 53 record to map app.yourdomain.com → CloudFront</p><p>We’ll implement Phase A now (so you’re not blocked), then add Phase B as an optional upgrade.</p><h4>Step 5A: S3 + CloudFront (No Domain Required)</h4><h4>5A.1 Create the module folder</h4><p>Create:</p><pre>modules/edge_frontend/<br>  main.tf<br>  variables.tf<br>  outputs.tf</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/331/1*sHMkGO1mEfJIUcWhdxNevg.png" /></figure><h4>5A.2 modules/edge_frontend/variables.tf</h4><pre>variable &quot;name&quot; {<br>  type        = string<br>  description = &quot;Name prefix for frontend resources&quot;<br>}</pre><pre>variable &quot;tags&quot; {<br>  type        = map(string)<br>  default     = {}<br>  description = &quot;Extra tags to apply to resources&quot;<br>}</pre><h4>5A.3 modules/edge_frontend/main.tf</h4><p>This module will:</p><ul><li>Create a private S3 bucket (no public access)</li><li>Uploads are optional (we’ll add later)</li><li>Create a CloudFront distribution</li><li>Use <strong>Origin Access Control</strong> (OAC) so CloudFront can read the bucket securely</li><li>Block all public access to S3</li></ul><pre>resource &quot;aws_s3_bucket&quot; &quot;frontend&quot; {<br>  bucket = var.name<br>  tags   = var.tags<br>}</pre><pre>resource &quot;aws_s3_bucket_public_access_block&quot; &quot;frontend&quot; {<br>  bucket                  = aws_s3_bucket.frontend.id<br>  block_public_acls       = true<br>  block_public_policy     = true<br>  ignore_public_acls      = true<br>  restrict_public_buckets = true<br>}</pre><pre>resource &quot;aws_s3_bucket_server_side_encryption_configuration&quot; &quot;frontend&quot; {<br>  bucket = aws_s3_bucket.frontend.id<br>  rule {<br>    apply_server_side_encryption_by_default {<br>      sse_algorithm = &quot;AES256&quot;<br>    }<br>  }<br>}</pre><pre># CloudFront Origin Access Control (modern replacement for OAI)<br>resource &quot;aws_cloudfront_origin_access_control&quot; &quot;oac&quot; {<br>  name                              = &quot;${var.name}-oac&quot;<br>  description                       = &quot;OAC for ${var.name}&quot;<br>  origin_access_control_origin_type = &quot;s3&quot;<br>  signing_behavior                  = &quot;always&quot;<br>  signing_protocol                  = &quot;sigv4&quot;<br>}</pre><pre>resource &quot;aws_cloudfront_distribution&quot; &quot;cdn&quot; {<br>  enabled             = true<br>  comment             = &quot;Frontend CDN for ${var.name}&quot;<br>  default_root_object = &quot;index.html&quot;</pre><pre>  origin {<br>    domain_name              = aws_s3_bucket.frontend.bucket_regional_domain_name<br>    origin_id                = &quot;s3-frontend&quot;<br>    origin_access_control_id = aws_cloudfront_origin_access_control.oac.id<br>  }</pre><pre>  default_cache_behavior {<br>    target_origin_id       = &quot;s3-frontend&quot;<br>    viewer_protocol_policy = &quot;redirect-to-https&quot;</pre><pre>    allowed_methods = [&quot;GET&quot;, &quot;HEAD&quot;]<br>    cached_methods  = [&quot;GET&quot;, &quot;HEAD&quot;]</pre><pre>    forwarded_values {<br>      query_string = false<br>      cookies {<br>        forward = &quot;none&quot;<br>      }<br>    }<br>  }</pre><pre>  restrictions {<br>    geo_restriction {<br>      restriction_type = &quot;none&quot;<br>    }<br>  }</pre><pre>  viewer_certificate {<br>    cloudfront_default_certificate = true<br>  }</pre><pre>  tags = var.tags<br>}</pre><pre># Allow CloudFront to read from the bucket (bucket policy)<br>data &quot;aws_iam_policy_document&quot; &quot;bucket_policy&quot; {<br>  statement {<br>    sid     = &quot;AllowCloudFrontRead&quot;<br>    effect  = &quot;Allow&quot;<br>    actions = [&quot;s3:GetObject&quot;]</pre><pre>    resources = [&quot;${aws_s3_bucket.frontend.arn}/*&quot;]</pre><pre>    principals {<br>      type        = &quot;Service&quot;<br>      identifiers = [&quot;cloudfront.amazonaws.com&quot;]<br>    }</pre><pre>    condition {<br>      test     = &quot;StringEquals&quot;<br>      variable = &quot;AWS:SourceArn&quot;<br>      values   = [aws_cloudfront_distribution.cdn.arn]<br>    }<br>  }<br>}</pre><pre>resource &quot;aws_s3_bucket_policy&quot; &quot;frontend&quot; {<br>  bucket = aws_s3_bucket.frontend.id<br>  policy = data.aws_iam_policy_document.bucket_policy.json<br>}</pre><h4>5A.4 modules/edge_frontend/outputs.tf</h4><pre>output &quot;bucket_name&quot; {<br>  value = aws_s3_bucket.frontend.bucket<br>}</pre><pre>output &quot;cloudfront_domain_name&quot; {<br>  value = aws_cloudfront_distribution.cdn.domain_name<br>}</pre><h4>Step 5A.5 Wire the module into envs/dev</h4><p>Edit:</p><h4>envs/dev/main.tf (add below the budgets module)</h4><pre>module &quot;edge_frontend&quot; {<br>  source = &quot;../../modules/edge_frontend&quot;</pre><pre>  name = &quot;${var.project}-${var.env}-frontend-${data.aws_caller_identity.current.account_id}&quot;</pre><pre>  tags = {<br>    project = var.project<br>    env     = var.env<br>    owner   = var.owner<br>  }<br>}</pre><pre>data &quot;aws_caller_identity&quot; &quot;current&quot; {}</pre><p>Why we included the account ID in the bucket name:</p><ul><li>S3 bucket names must be globally unique</li><li>This prevents “BucketAlreadyExists” forever</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a3HlaJYgNVKUZViihEAqJQ.png" /></figure><h4>Step 5A.6 Run Terraform</h4><p>From the dev environment:</p><pre>cd envs/dev<br>terraform fmt<br>terraform validate<br>terraform plan</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AC_AcDXQkuu2E-tQrT_SgQ.png" /></figure><p>This error is <strong>real Terraform</strong>, because <strong>AWS provider v5+ removed this resource</strong>.</p><p>In older Terraform versions, notifications were a <strong>separate resource</strong>:</p><pre>aws_budgets_budget_notification</pre><p>In <strong>modern Terraform (AWS provider v5+)</strong>, notifications are defined <strong>inline</strong> inside:</p><pre>aws_budgets_budget</pre><p>We must:</p><ul><li>DELETE aws_budgets_budget_notification</li><li>Define notifications <strong>inside</strong> aws_budgets_budget</li></ul><h4>Replace the ENTIRE file:</h4><h4>modules/budgets/main.tf</h4><p><strong>Delete everything inside it and replace with this:</strong></p><pre>variable &quot;monthly_limit_usd&quot; {<br>  type        = number<br>  description = &quot;Monthly AWS budget limit in USD&quot;<br>}<br><br>variable &quot;email&quot; {<br>  type        = string<br>  description = &quot;Email address to receive budget alerts&quot;<br>}<br><br>resource &quot;aws_budgets_budget&quot; &quot;monthly&quot; {<br>  name         = &quot;terraform-monthly-budget&quot;<br>  budget_type  = &quot;COST&quot;<br>  limit_amount = tostring(var.monthly_limit_usd)<br>  limit_unit   = &quot;USD&quot;<br>  time_unit    = &quot;MONTHLY&quot;<br><br>  notification {<br>    notification_type           = &quot;ACTUAL&quot;<br>    comparison_operator         = &quot;GREATER_THAN&quot;<br>    threshold                   = 80<br>    threshold_type              = &quot;PERCENTAGE&quot;<br>    subscriber_email_addresses  = [var.email]<br>  }<br>}</pre><p>This is the <strong>only correct way</strong> to do this in AWS provider v5+.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hs9Cv4qvI_nQU6jTkqqb2g.png" /></figure><p>Then apply:</p><pre>terraform apply</pre><p>When managing S3 buckets with Terraform, least privilege must be applied at the <em>resource boundary</em>, not at individual GetBucket* API calls. Terraform reads the full bucket schema during refresh, making granular read permissions impractical.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HTpsUF0shfB8CzfighFspw.png" /></figure><h4>Step 5A.7 — Verify Everything in the AWS Console</h4><p>At this point, Terraform has successfully provisioned our <strong>edge layer</strong>.<br> Before moving forward, we must <strong>visually verify</strong> that what Terraform created matches our expectations.</p><p>This step is critical in real-world workflows:<br> <strong>never trust blindly — always verify.</strong></p><h4>1. Verify the CloudFront Distribution</h4><p>Go to:</p><pre>AWS Console → CloudFront → Distributions</pre><p>You should see:</p><ul><li>A CloudFront distribution created by Terraform</li><li><strong>Status: Deployed</strong></li><li>A valid <strong>Distribution ID</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sqcZ5wuUladugPzBWVT1lw.png" /></figure><p>This confirms:</p><ul><li>CloudFront was created successfully</li><li>AWS has finished global propagation</li><li>The distribution is ready to serve traffic</li></ul><h4>2. Verify the S3 Bucket Is Private</h4><p>Now check that the S3 bucket backing CloudFront is <strong>not publicly accessible</strong>.</p><p>Go to:</p><pre>AWS Console → S3 → your frontend bucket → Permissions</pre><p>Confirm:</p><ul><li><strong>Block all public access = ON</strong></li><li>No public bucket policies</li><li>No public ACLs</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LkfVqYv-ho3EdsDBGbfKfw.png" /></figure><p>This proves:</p><ul><li>The bucket is private</li><li>Objects can only be accessed through CloudFront</li><li>We are following AWS security best practices</li></ul><h4>3. Verify the CloudFront Domain Works</h4><p>Terraform outputs a CloudFront domain name similar to:</p><pre>xxxxxxxxxxxx.cloudfront.net</pre><p>Copy that URL and open it in your browser.</p><p>Expected behavior:</p><ul><li>It <strong>redirects to HTTPS</strong></li><li>You may see <strong>403 AccessDenied</strong> or <strong>Access Denied</strong></li></ul><p>This is <strong>EXPECTED</strong> at this stage.</p><p>Why?</p><ul><li>We haven’t uploaded any content yet</li><li>The bucket is private (as it should be)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SlOzMJ9KlKZLRWn5FhtHOg.png" /></figure><blockquote><strong><em>Note:</em></strong><em><br> A 403 at this stage is a </em>good sign<em>.<br> It means CloudFront works, HTTPS is enforced, and S3 is not public.</em></blockquote><h4>Step 5A.8 — Add a Simple index.html (So the CDN Shows Something)</h4><p>Now let’s upload a very simple static page so CloudFront has content to serve.</p><p>We’ll do this <strong>manually</strong> on purpose.</p><p>Why?</p><ul><li>Keeps Terraform clean</li><li>Avoids noisy diffs for static assets</li><li>Matches how many teams separate infra from content</li></ul><h4>1. Create a Local site/ Folder</h4><p>At the <strong>root of your repository</strong>, create:</p><pre>site/<br>  index.html</pre><p><strong>site/index.html</strong></p><pre>&lt;!doctype html&gt;<br>&lt;html&gt;<br>  &lt;head&gt;<br>    &lt;meta charset=&quot;utf-8&quot; /&gt;<br>    &lt;title&gt;Terraform Modern AWS&lt;/title&gt;<br>  &lt;/head&gt;<br>  &lt;body&gt;<br>    &lt;h1&gt;It works ✅&lt;/h1&gt;<br>    &lt;p&gt;Served by CloudFront, provisioned by Terraform.&lt;/p&gt;<br>  &lt;/body&gt;<br>&lt;/html</pre><p>This file is intentionally simple.<br> Its purpose is <strong>verification</strong>, not design.</p><h4>2. Upload index.html to S3 (Manually)</h4><p>Go to:</p><pre>AWS Console → S3 → your frontend bucket → Upload</pre><ul><li>Upload index.html</li><li>Keep default settings</li><li>Complete the upload</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LKPC29U2bep40Cqs9hV59w.png" /></figure><h4>3. Invalidate the CloudFront Cache</h4><p>CloudFront may cache the previous 403 response.</p><p>To force it to fetch the new file:</p><pre>CloudFront → Your distribution → Invalidations → Create invalidation</pre><p>Enter:</p><pre>/*</pre><p>Create the invalidation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JhaXdV-mMnndgQ_CMR7Aiw.png" /></figure><h4>4. Verify the Page Loads</h4><p>Wait ~30–60 seconds, then refresh the CloudFront URL in your browser.</p><p>You should now see:</p><pre>It works ✅<br>Served by CloudFront, provisioned by Terraform.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nZA72qxNHgMPk7QJc3-eOg.png" /></figure><p>The CDN is live.</p><h4>Cost Control Note</h4><p>This architecture is <strong>intentionally cheap</strong>:</p><ul><li><strong>S3</strong>: costs pennies for small files</li><li><strong>CloudFront</strong>: pay-per-request and data transfer<br> At low traffic, cost is negligible</li><li><strong>No EC2</strong></li><li><strong>No ALB</strong></li><li><strong>No NAT Gateway</strong></li><li><strong>No always-on compute</strong></li></ul><p>This is exactly how modern teams build <strong>cost-efficient edge layers</strong>.</p><h4>Step 6 — Add CI/CD (Automatic Frontend Deployments)</h4><h4>Goal of Step 6</h4><p>Every time you push to main:</p><ul><li>Your static site is uploaded to S3</li><li>CloudFront cache is invalidated</li><li>No manual uploads ever again</li></ul><p>This <strong>does NOT</strong> deploy infrastructure — only <strong>frontend content</strong>.</p><h4>Step 6.1 — Create an IAM User for GitHub Actions</h4><p><strong>Never use your devops-user or root in CI/CD</strong></p><h4>1. Go to IAM → Users → Create user</h4><p>Name:</p><pre>github-actions-frontend</pre><p>Access type:</p><ul><li>Programmatic access (Access key)</li></ul><h4>2. Attach this policy</h4><p>This is <strong>least privilege</strong> for:</p><ul><li>Uploading to the frontend bucket</li><li>Invalidating CloudFront</li></ul><pre>{<br>  &quot;Version&quot;: &quot;2012-10-17&quot;,<br>  &quot;Statement&quot;: [<br>    {<br>      &quot;Sid&quot;: &quot;UploadFrontend&quot;,<br>      &quot;Effect&quot;: &quot;Allow&quot;,<br>      &quot;Action&quot;: [<br>        &quot;s3:PutObject&quot;,<br>        &quot;s3:DeleteObject&quot;,<br>        &quot;s3:ListBucket&quot;<br>      ],<br>      &quot;Resource&quot;: [<br>        &quot;arn:aws:s3:::terraform-modern-aws-dev-frontend-559938827680&quot;,<br>        &quot;arn:aws:s3:::terraform-modern-aws-dev-frontend-559938827680/*&quot;<br>      ]<br>    },<br>    {<br>      &quot;Sid&quot;: &quot;InvalidateCloudFront&quot;,<br>      &quot;Effect&quot;: &quot;Allow&quot;,<br>      &quot;Action&quot;: &quot;cloudfront:CreateInvalidation&quot;,<br>      &quot;Resource&quot;: &quot;*&quot;<br>    }<br>  ]<br>}</pre><p>Create user<br>Save <strong>Access Key ID</strong> and <strong>Secret Access Key</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZX1BXt83B3SxtBrceN9arQ.png" /></figure><h4>Step 6.2 — Add GitHub Secrets</h4><p>Go to:<br> <strong>GitHub repo → Settings → Secrets and variables → Actions</strong></p><p>Add these <strong>exact names</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*Tqs5G44KPCpQmkIbCrrXhA.png" /></figure><p>CloudFront ID example:</p><pre>E1LKTOS8QVDE79</pre><h4>Step 6.3 — Create GitHub Actions Workflow</h4><p>Create this file <strong>exactly</strong>:</p><pre>.github/workflows/frontend.yml</pre><h4>frontend.yml</h4><pre>name: Deploy Frontend to S3 + CloudFront</pre><pre>on:<br>  push:<br>    branches:<br>      - main<br>    paths:<br>      - site/**<br>      - .github/workflows/frontend.yml</pre><pre>jobs:<br>  deploy:<br>    runs-on: ubuntu-latest</pre><pre>steps:<br>      - name: Checkout repository<br>        uses: actions/checkout@v4</pre><pre>- name: Configure AWS credentials<br>        uses: aws-actions/configure-aws-credentials@v4<br>        with:<br>          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}<br>          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}<br>          aws-region: us-east-1</pre><pre>- name: Upload site to S3<br>        run: |<br>          aws s3 sync site/ s3://${{ secrets.S3_BUCKET_NAME }} --delete</pre><pre>- name: Invalidate CloudFront cache<br>        run: |<br>          aws cloudfront create-invalidation \<br>            --distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} \<br>            --paths &quot;/*&quot;</pre><h4>1. Create .gitignore (at repo root)</h4><p>Create a file named <strong>.gitignore</strong> at the root of terraform-modern-aws.</p><p>Paste this <strong>exactly</strong>:</p><pre># Terraform<br>.terraform/<br>*.tfstate<br>*.tfstate.*<br>errored.tfstate</pre><pre># Terraform variables (optional safety)<br>*.tfvars<br>*.tfvars.json</pre><pre># AWS credentials<br>.aws/<br>.env</pre><pre># OS / Editor noise<br>.DS_Store<br>Thumbs.db<br>.vscode/<br>.idea/</pre><pre># Logs<br>*.log</pre><p>✅ Save it.</p><h4>2. Double-check Git status (important)</h4><p>From repo root:</p><pre>git status</pre><p>You should see:</p><ul><li>Terraform .tf files</li><li>.github/workflows/*.yml</li><li>site/index.html</li><li>NO .terraform/</li><li>NO .tfstate</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4DPP7fgcmrnG86TKkaqQFQ.png" /></figure><h4>3. Initialize Git (if not already)</h4><pre>git init<br>git branch -M main</pre><h4>4. First commit (clean infrastructure baseline)</h4><pre>git add .<br>git commit -m &quot;Initial Terraform infrastructure and frontend pipeline&quot;</pre><h4>5. Create GitHub repo</h4><ul><li>Name: terraform-modern-aws</li><li>Visibility: your choice</li><li>Do <strong>NOT</strong> add README or .gitignore (you already have them)</li></ul><p>Then connect:</p><pre>git remote add origin https://github.com/&lt;your-username&gt;/terraform-modern-aws.git<br>git push -u origin main</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O8KJfn4wpD3QE1iPs4o7jw.png" /></figure><h4>Step 6.4 — Test the Pipeline</h4><p>Now we test like a real DevOps team.</p><h4>1. Make a small frontend change</h4><p>Edit:</p><pre>site/index.html</pre><p>Change something obvious, e.g.:</p><pre>&lt;h1&gt;It works (via github action)&lt;/h1&gt;</pre><h4>2. Commit &amp; push (THIS triggers CI/CD)</h4><pre>git add site/index.html<br>git commit -m &quot;Update homepage&quot;<br>git push origin main</pre><h4>3. What should happen automatically</h4><p>GitHub:</p><ul><li>Repo → <strong>Actions</strong></li><li>Workflow: <strong>Deploy Frontend to S3 + CloudFront</strong></li><li>Status: 🟡 → 🟢</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IaTAHfFPtInkwQmddPo89Q.png" /></figure><p>AWS:</p><ul><li>S3 bucket updated</li><li>CloudFront cache invalidated</li></ul><p>Browser:</p><ul><li>Refresh CloudFront URL</li><li>New text appears</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8nQaraYYcDmmgozd1j0kwQ.png" /></figure><h4>Step 7 — Final Validation, Lessons Learned, and Next Steps</h4><p>At this point, the project is complete — but before calling it done, we validate the system <strong>the way a real platform team would</strong> and reflect on what was built.</p><p>This step is not about adding more infrastructure.<br> It’s about proving correctness, maturity, and readiness.</p><h4>7.1 End-to-End Validation</h4><p>By the end of this project, we successfully validated:</p><p><strong>Infrastructure</strong></p><ul><li>Terraform state stored remotely in S3</li><li>State locking enforced with DynamoDB</li><li>No local or committed state files</li><li>Clear separation between dev and prod environments</li></ul><p><strong>Security</strong></p><ul><li>S3 frontend bucket is fully private</li><li>CloudFront accesses S3 via Origin Access Control (OAC)</li><li>No public bucket access</li><li>IAM follows least-privilege principles</li><li>GitHub Actions uses a dedicated IAM user</li></ul><p><strong>CI/CD</strong></p><ul><li>GitHub Actions deploys frontend content automatically</li><li>Frontend deployments do <strong>not</strong> run Terraform</li><li>CloudFront cache invalidation happens on every deploy</li><li>Secrets are managed securely via GitHub Actions</li></ul><p><strong>Delivery</strong></p><ul><li>Global HTTPS endpoint via CloudFront</li><li>Static site updates deploy in seconds</li><li>No manual uploads required</li><li>Zero always-on compute costs</li></ul><p>The browser displaying:</p><blockquote><strong><em>“It works (via GitHub action)”</em></strong></blockquote><p>is the final proof that the entire system — from commit → CI/CD → S3 → CloudFront — is functioning exactly as designed.</p><h4>7.2 Why This Project Is “Production-Grade”</h4><p>This project intentionally avoids common Terraform and cloud anti-patterns:</p><p>No local state<br>No monolithic Terraform files<br>No public S3 buckets<br>No hard-coded credentials<br>No always-on infrastructure</p><p>Instead, it demonstrates how <strong>real platform teams</strong> work:</p><ul><li>Declarative infrastructure</li><li>Modular design</li><li>Environment isolation</li><li>CI/CD-first mindset</li><li>Cost awareness from day one</li></ul><p>This is not a demo — it’s a <strong>publishable reference architecture</strong>.</p><h4>7.3 Cost &amp; Operational Awareness</h4><p>A key goal of this project was to remain safe under limited AWS credits.</p><p>This architecture achieves that because:</p><ul><li>S3 storage costs pennies</li><li>CloudFront only charges for requests and data transfer</li><li>No EC2, ALB, or NAT Gateways running 24/7</li><li>DynamoDB uses on-demand billing</li><li>AWS Budgets enforce spending awareness</li></ul><p>You get:</p><ul><li>Global delivery</li><li>Strong security</li><li>CI/CD automation</li></ul><p>Without burning money.</p><h4>7.4 What This Project Sets You Up For Next</h4><p>This foundation can be extended naturally:</p><ul><li><strong>Add a custom domain</strong> (Route 53 + ACM)</li><li><strong>Add API Gateway + Lambda</strong> behind CloudFront</li><li><strong>Split Terraform apply into approval workflows</strong></li><li><strong>Add drift detection alerts</strong></li><li><strong>Promote dev → prod via GitHub Actions</strong></li><li><strong>Introduce policy-as-code (OPA / Sentinel)</strong></li></ul><p>None of those require restructuring — because the foundation is already correct.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f90ca8e18da2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How To Deploy Directly to AWS EC2 Using GitHub Actions]]></title>
            <link>https://medium.com/@shehuyusuf/how-to-deploy-directly-to-aws-ec2-using-github-actions-0468db844fff?source=rss-607ba1670d2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/0468db844fff</guid>
            <dc:creator><![CDATA[Shehu Yusuf]]></dc:creator>
            <pubDate>Wed, 17 Dec 2025 20:47:16 GMT</pubDate>
            <atom:updated>2025-12-17T20:47:16.051Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_vHHt9dhcEcCkmemm_PuDQ.png" /></figure><h4>Introduction</h4><p>Deploying applications manually to servers is error-prone, slow, and does not scale. Modern teams rely on <strong>CI/CD pipelines</strong> to automatically build, test, and deploy code whenever changes are pushed.</p><p>In this tutorial, we’ll build a <strong>production-ready deployment pipeline</strong> that deploys a <strong>Django application directly to an AWS EC2 instance using GitHub Actions</strong>.</p><p>By the end of this guide, we will have:</p><ul><li>A Django app running on <strong>AWS EC2</strong></li><li>A <strong>GitHub Actions pipeline</strong> that deploys on every git push</li><li>Secure <strong>SSH-based deployment</strong></li><li>Nginx + Gunicorn serving your app</li><li>A setup suitable for <strong>AWS Free Tier</strong></li></ul><p>This guide assumes:</p><ul><li>Basic Django knowledge</li><li>Basic Git/GitHub usage</li><li>No prior CI/CD or AWS automation experience</li></ul><h4>1. Environment Setup</h4><h4>Prerequisites</h4><ul><li>AWS account (Free Tier)</li><li>GitHub account</li><li>Django project pushed to GitHub</li><li>Local machine with:</li><li>Git</li><li>Python 3.10+</li><li>SSH client</li></ul><h4>2. Create an AWS EC2 Instance</h4><h4>Step 1: Launch EC2</h4><ol><li>Go to <strong>AWS Console → EC2 → Launch Instance</strong></li><li>Choose:</li></ol><ul><li><strong>AMI:</strong> Ubuntu Server 22.04 LTS</li><li><strong>Instance type:</strong> t3.small (Free Tier)</li></ul><p>3. Create or select a <strong>Key Pair</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*9Ip5JZKfE58x329yW0hFRA.png" /></figure><p>4. Configure Security Group:</p><p>As expected, <strong>we also create a key pair to enable SSH access to the instance</strong>. The key pair should be of RSA type and <em>.pem</em> format:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*pYObGxfCfBFqcKf6tqW5hA.png" /></figure><ul><li>Allow <strong>SSH (22)</strong> from your IP</li><li>Allow TCP <strong>(3000)</strong> from anywhere</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/986/1*oLwJozMnKBMzhOtAApj6Dg.png" /></figure><p>5. Launch instance:</p><p>We can now launch the instance and confirm it’s running from the <em>Instances</em> dashboard:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AbpNCHIkatd1WdDea04EFA.png" /></figure><h4>Step 2: Connect to EC2</h4><p>Once the EC2 instance is running, we connect to it using SSH.<br> From a local machine (Linux, macOS, or Windows via WSL or Git Bash), run:</p><pre>ssh -i your-key.pem ubuntu@EC2_PUBLIC_IP</pre><p>After a successful login, we should see the Ubuntu shell prompt:</p><pre>ubuntu@ip-172-31-xx-xx:~$</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*IXxyLFRzmHCMeYqm0Z8H6w.png" /></figure><h4>3. Preparing the EC2 Server</h4><p>Before deploying the Django application, we need to prepare the server environment.</p><h4>3.1 Update the System</h4><p>First, update system packages:</p><pre>sudo apt update &amp;&amp; sudo apt upgrade -y</pre><p>This ensures we are running the latest security patches.</p><h4>3.2 Install Required Dependencies</h4><p>We install Python 3.11 tooling, Git, and basic build utilities:</p><pre>sudo apt install -y \<br>  python3 python3-venv python3-pip \<br>  git</pre><blockquote><em>We intentionally </em><strong><em>do not install Nginx</em></strong><em>, as this deployment binds Gunicorn directly to TCP port </em><strong><em>3000</em></strong><em>.</em></blockquote><h4>4. Clone the Django Project on EC2</h4><p>We keep application code under /var/www, which is a common convention.</p><h4>Step 1: Create /var/www</h4><p>Run this <strong>on the EC2 instance</strong>:</p><pre>sudo mkdir -p /var/www</pre><p>Verify:</p><pre>ls -ld /var/www</pre><p>You should see something like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*eyR9uyvtP1Ui1iBYp_iZmw.png" /></figure><pre>drwxr-xr-x 2 root root 4096 /var/www</pre><pre>cd /var/www<br>sudo git clone https://github.com/Shakurrrr/PERCY-BLOG-WEBSITE.git</pre><p>You should never deploy as root so change ownership so the ubuntu user can manage the project:</p><pre>sudo chown -R ubuntu:ubuntu /var/www</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*MPEX4dGG55dVJH5YhZ-ocQ.png" /><figcaption>Setting up Ubuntu to take ownership.</figcaption></figure><h4>5. Setting Up the Python Virtual Environment</h4><p>Inside the project directory, create and activate a virtual environment:</p><pre>cd PERCY-BLOG-WEBSITE<br>python3 -m venv venv<br>source venv/bin/activate</pre><p>Upgrade pip and install project dependencies:</p><pre>pip install --upgrade pip<br>pip install -r requirements.txt</pre><p>At this point, Django and all required libraries should be installed.</p><h4>6. Django Production Configuration</h4><p>Before running the application, we must configure Django for production.</p><h4>6.1 Update settings.py</h4><p>Open blogsite/settings.py and update the following:</p><pre>DEBUG = False</pre><pre>ALLOWED_HOSTS = [<br>    &quot;EC2_PUBLIC_IP&quot;,<br>]</pre><blockquote><em>Do </em><strong><em>not</em></strong><em> include ports in </em><em>ALLOWED_HOSTS.</em></blockquote><h4>6.2 Run Database Migrations</h4><pre>python3 manage.py migrate</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*X87BdJ7a7t9f8o_XxfYINQ.png" /></figure><h4>6.3 Collect Static Files</h4><pre>python3 manage.py collectstatic --noinput</pre><p>Static files will be collected into the directory defined by STATIC_ROOT.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/961/1*LKPFmmG_TKVHKnzQ3kOV0w.png" /></figure><h4>7. Gunicorn Setup (TCP Port 3000)</h4><p>Django’s built-in development server is <strong>not suitable for production</strong>.<br> Instead, we use <strong>Gunicorn</strong>, a production-grade WSGI server.</p><h4>7.1 Install Gunicorn</h4><pre>pip install gunicorn</pre><h4>7.2 Test Gunicorn Manually</h4><p>Before automating, test Gunicorn directly:</p><pre>gunicorn blogsite.wsgi:application --bind 0.0.0.0:3000</pre><p>From a browser, visit:</p><pre>http://EC2_PUBLIC_IP:3000</pre><p>If the Django application loads, Gunicorn is working correctly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OmVsHMAsKQnqNcTEWejl8A.png" /></figure><p>Stop Gunicorn with CTRL+C.</p><h4>7.3 Create a systemd Service for Gunicorn</h4><p>To keep Gunicorn running persistently, we create a systemd service.</p><pre>sudo nano /etc/systemd/system/gunicorn.service</pre><p>Add the following configuration:</p><pre>[Unit]<br>Description=Gunicorn daemon for Percy Blog<br>After=network.target</pre><pre>[Service]<br>User=ubuntu<br>Group=www-data<br>WorkingDirectory=/var/www/PERCY-BLOG-WEBSITE<br>ExecStart=/var/www/PERCY-BLOG-WEBSITE/venv/bin/gunicorn \<br>          --workers 3 \<br>          --bind 0.0.0.0:3000 \<br>          blogsite.wsgi:application</pre><pre>Restart=always</pre><pre>[Install]<br>WantedBy=multi-user.target</pre><p>Reload and start the service:</p><pre>sudo systemctl daemon-reload<br>sudo systemctl start gunicorn<br>sudo systemctl enable gunicorn</pre><p>Check status:</p><pre>sudo systemctl status gunicorn</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*78kvfINY-zKFVSzH6nG7xg.png" /></figure><h4>8. Security Group Configuration (Critical)</h4><p>For the application to be accessible from the internet, the EC2 instance’s <strong>Security Group must explicitly allow inbound traffic on port 3000</strong>.</p><p>Update the Security Group attached to your EC2 instance with the following inbound rules:</p><pre>Type        Protocol   Port   Source<br>------------------------------------------<br>SSH         TCP        22     Your IP only<br>Custom TCP  TCP        3000   0.0.0.0/0</pre><p>Port <strong>3000</strong> is required because Gunicorn listens directly on this port.<br> Without this rule, the application will not be accessible externally.<br> Without this rule, the Django application will run successfully on the server but will not be reachable from a browser.</p><h4>9. SSH Setup for GitHub Actions</h4><p>To support automated deployments, GitHub Actions must be able to connect securely to the EC2 instance via SSH.</p><h4>9.1 Generate an SSH Key (Local Machine)</h4><p>Generate a dedicated SSH key pair for GitHub Actions:</p><pre>ssh-keygen -t ed25519 -C &quot;github-actions&quot;</pre><p>This creates a public and private key pair under ~/.ssh/.</p><h4>9.2 Add the Public Key to EC2</h4><p>Display the public key:</p><pre>cat /var/www/PERCY-BLOG-WEBSITE/github.pub</pre><p>On the EC2 instance, open the authorized keys file:</p><pre>nano ~/.ssh/authorized_keys</pre><p>Paste the public key into the file and save it. This allows GitHub Actions to authenticate via SSH.</p><h4>9.3 Configure GitHub Secrets</h4><p>In your GitHub repository, navigate to:</p><p><strong>Settings → Secrets → Actions</strong></p><p>Add the following secrets:</p><pre>Name          Value<br>---------------------------------------------<br>EC2_HOST      EC2 public IP address<br>EC2_USER      ubuntu<br>EC2_SSH_KEY   SSH private key contents</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*_lHaPAlfO2mMlvQDYll_Zw.png" /></figure><p>These secrets are <strong>encrypted by GitHub</strong> and securely injected into the GitHub Actions workflow at runtime.<br>They allow GitHub Actions to authenticate with the EC2 instance <strong>without exposing sensitive credentials in the repository</strong>.</p><h4>10. GitHub Actions Workflow</h4><p>Create the workflow directory and deployment file:</p><pre>mkdir -p .github/workflows<br>nano .github/workflows/deploy.yml</pre><p>Add the following workflow configuration:</p><pre>name: Deploy Percy Blog to EC2</pre><pre>on:<br>  push:<br>    branches:<br>      - main</pre><pre>jobs:<br>  deploy:<br>    runs-on: ubuntu-latest</pre><pre>steps:<br>      - name: Checkout code<br>        uses: actions/checkout@v4</pre><pre>- name: Deploy via SSH<br>        uses: appleboy/ssh-action@v1.0.3<br>        with:<br>          host: ${{ secrets.EC2_HOST }}<br>          username: ${{ secrets.EC2_USER }}<br>          key: ${{ secrets.EC2_SSH_KEY }}<br>          script: |<br>            cd /var/www/PERCY-BLOG-WEBSITE<br>            source venv/bin/activate<br>            pip install -r requirements.txt<br>            python manage.py migrate<br>            python manage.py collectstatic --noinput<br>            sudo systemctl restart gunicorn</pre><p>This workflow automatically syncs code to the server, installs dependencies, applies database migrations, collects static files, and restarts Gunicorn.</p><h4>11. Verifying the Deployment</h4><p>Push your changes to trigger the pipeline:</p><pre>git add .<br>git commit -m &quot;Deploy Django app via GitHub Actions&quot;<br>git push origin main</pre><p>Then:</p><ol><li>Open <strong>GitHub → Actions</strong></li><li>Monitor the deployment logs</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ddC5_k6zJfAUYOGktaK4QQ.png" /></figure><p>3. Visit the application in your browser:</p><p><a href="http://ec2_public_ip:3000/">4 . http://EC2_PUBLIC_IP:3000</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wdGK0xWBRV44n5JxTe5rWg.png" /></figure><p>If the page loads successfully, the deployment is complete which it did from the screenshot above.</p><h4>12. Security and Cost Best Practices</h4><p>To maintain a secure and cost-efficient setup:</p><ul><li>Restrict SSH access to your own IP address</li><li>Never commit secrets to the repository</li><li>Use environment variables or .env files</li><li>Stop EC2 instances when not actively in use</li><li>Monitor AWS Free Tier usage to avoid unexpected charges</li></ul><h4>Conclusion</h4><p>In this article, we built a fully automated CI/CD pipeline that:</p><ul><li>Uses GitHub Actions</li><li>Deploys directly to AWS EC2</li><li>Runs a production Django application</li><li>Serves traffic through Gunicorn on TCP port 3000</li><li>Requires zero manual redeployment steps</li></ul><p>This architecture closely mirrors what many early-stage startups and small teams use in real-world production environments: simple, reliable, and scalable without unnecessary complexity.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0468db844fff" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Practical Guide to GitOps on AWS: Building an End-to-End CI/CD Pipeline with Jenkins, Amazon ECR…]]></title>
            <link>https://medium.com/@shehuyusuf/a-practical-guide-to-gitops-on-aws-building-an-end-to-end-ci-cd-pipeline-with-jenkins-amazon-ecr-3b5322df0c2c?source=rss-607ba1670d2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b5322df0c2c</guid>
            <dc:creator><![CDATA[Shehu Yusuf]]></dc:creator>
            <pubDate>Thu, 11 Dec 2025 10:07:55 GMT</pubDate>
            <atom:updated>2025-12-11T10:07:55.179Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Practical Guide to GitOps on AWS: Building an End-to-End CI/CD Pipeline with Jenkins, Amazon ECR, ArgoCD, and Amazon EKS</h3><p><em>A complete, real-world walkthrough of how I designed and deployed a fully automated GitOps-driven CI/CD pipeline on AWS — from code commit to Kubernetes deployment — featuring Jenkins for CI, ArgoCD for CD, and EKS as the execution platform. Includes all configurations, commands, architecture, and the problems I encountered along the way.</em></p><h3>Introduction</h3><p>Modern software delivery demands automation, resilience, and speed. For teams deploying containerized applications at scale, traditional CI/CD workflows alone are often not enough. This is where <strong>GitOps</strong> becomes a game-changer. By using Git as the single source of truth for application and infrastructure states, GitOps provides transparency, repeatability, and safer environments.</p><p>In this case study, I walk through the <strong>exact process I used</strong> to build a <strong>production-ready CI/CD pipeline on AWS</strong> for a real Django application. The stack includes:</p><ul><li><strong>GitHub</strong> for version control</li><li><strong>Jenkins</strong> for Continuous Integration</li><li><strong>Amazon ECR</strong> for container image storage</li><li><strong>ArgoCD</strong> for Continuous Deployment using GitOps</li><li><strong>Amazon EKS</strong> for running applications on Kubernetes</li><li><strong>AWS Route 53</strong> for DNS</li><li><strong>CloudWatch </strong>for monitoring</li></ul><p>This article documents <strong>every major decision</strong>, <strong>every step taken</strong>, and even <strong>the problems I encountered and how I solved them</strong> — something most tutorials tend to skip.</p><p>The goal is to give you a reproducible blueprint that you can adapt to your own cloud-native projects.</p><h3>Architecture Overview</h3><p>Before diving into implementation, here’s the high-level workflow:</p><ol><li>A developer pushes code changes to GitHub</li><li>Jenkins automatically builds the application and Docker image</li><li>Jenkins pushes the image to <strong>Amazon ECR</strong></li><li>Jenkins updates the Kubernetes manifests stored in a dedicated <strong>GitOps repository</strong></li><li>ArgoCD detects the change and syncs the new version to <strong>Amazon EKS</strong></li><li>EKS exposes the application using a LoadBalancer or Ingress</li><li>Route 53 routes traffic to the application</li><li>CloudWatch captures logs and metrics</li></ol><p><em>A professionally designed diagram will be inserted here.</em></p><h3>Section 1: Setting Up the AWS Environment</h3><p>To build this pipeline, we first need to prepare the underlying AWS infrastructure.</p><h4>1.1 Creating the EKS Cluster</h4><p>EKS is Amazon’s managed Kubernetes service. Using eksctl, you can provision a cluster with a single command.</p><pre>eksctl create cluster \<br>  --name blog-cluster \<br>  --region us-east-1 \<br>  --nodes 2 \<br>  --node-type t3.small \<br>  --managed</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oieqdrXNPlUct4vKxKzyqw.png" /></figure><p>This sets up:</p><ul><li>Control plane</li><li>Node group</li><li>VPC networking</li><li>IAM roles</li></ul><p>Once completed, verify connectivity:</p><pre>kubectl get nodes</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/1*E45v5mJHD59NSIixz_2QOg.png" /></figure><h4>1.2 Setting Up Amazon ECR</h4><p>We need a secure place to store Docker images.</p><p>Create the repository:</p><pre>aws ecr create-repository \<br>  --repository-name percy-blog \<br>  --region us-east-1</pre><p>Note the output:</p><pre>&lt;aws_account_id&gt;.dkr.ecr.us-east-1.amazonaws.com/percy-blog</pre><p>You’ll use this in Jenkins.</p><h4>1.3 IAM Permissions for Jenkins</h4><p>Jenkins must authenticate with ECR and update GitOps manifests.</p><p>Minimum required permissions:</p><ul><li>ecr:BatchCheckLayerAvailability</li><li>ecr:CompleteLayerUpload</li><li>ecr:InitiateLayerUpload</li><li>ecr:PutImage</li><li>ecr:UploadLayerPart</li><li>Git permissions for GitHub</li><li>(Optional) eks:DescribeCluster</li></ul><p>Using IAM user:</p><pre>aws iam create-user --user-name jenkins-user<br>aws iam attach-user-policy \<br>  --user-name jenkins-user \<br>  --policy-arn arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess</pre><p>Generate access keys for Jenkins credentials.</p><h3>Section 2 — Installing &amp; Accessing ArgoCD on Amazon EKS</h3><p>With the EKS cluster up and running, the next step is to deploy ArgoCD.<br> While the installation itself is straightforward, accessing the ArgoCD UI inside a WSL-based development environment required deeper troubleshooting. This turned into one of the most educational parts of the entire project.</p><p>Below is exactly how ArgoCD was installed <strong>and how the access issue was solved in a production-grade way</strong>.</p><h4>2.1 Installing ArgoCD on EKS</h4><p>ArgoCD provides installation manifests that can be applied directly to any Kubernetes cluster.</p><pre>kubectl create namespace argocd</pre><pre>kubectl apply -n argocd \<br>  -f <a href="https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml">https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml</a></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*roKZnpFicagK93xHBdhrJw.png" /></figure><p>Verify that all ArgoCD components are running:</p><pre>kubectl get pods -n argocd</pre><p>You should see pods like:</p><pre>argocd-server<br>argocd-repo-server<br>argocd-application-controller<br>argocd-dex-server<br>...</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*jEDhPPtxxju1Wn5WeRu-Bw.png" /></figure><p>At this point, <strong>ArgoCD is fully installed and healthy inside EKS.</strong></p><h4>2.2 The UI Access Problem: Port-Forwarding Fails on Windows/WSL</h4><p>Typically, accessing ArgoCD locally is done with:</p><pre>kubectl port-forward svc/argocd-server -n argocd 8181:443</pre><p>But on Windows using WSL2, this failed consistently with:</p><pre>Unable to listen on port 8080: address already in use  <br>ERR_CONNECTION_REFUSED</pre><p>Even after switching ports (8182, 9000, 9090…), browsers still <strong>could not connect</strong>, even though:</p><pre>curl -k https://127.0.0.1:8181</pre><p>successfully returned the ArgoCD HTML markup from inside WSL.</p><h4>Root Cause</h4><p>WSL2 runs in a lightweight VM with its own virtualized network.<br> Port-forwarding inside WSL works <strong>only inside WSL</strong>, but <strong>does not propagate to the Windows host browser</strong> due to NAT isolation.</p><p>This is a known limitation of WSL2 when Kubernetes port-forwarding is involved.</p><p>So we needed a <strong>bridge</strong> between:</p><ul><li>ArgoCD ↔ Kubernetes service</li><li>WSL ↔ Windows host</li><li>Windows browser ↔ ArgoCD port inside WSL</li></ul><p>This is where the real solution came in.</p><h4>2.3 The Working Fix — A Stable Reverse Bridge Using socat + portproxy</h4><p>To bypass WSL2 networking limitations, we created a <strong>two-layer port bridge</strong>:</p><h4>Step 1 — Forward ArgoCD from Kubernetes into WSL</h4><p>Inside WSL:</p><pre>kubectl port-forward svc/argocd-server -n argocd 8181:443</pre><p>WSL can now reach ArgoCD:</p><pre>curl -k https://127.0.0.1:8181</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*9Uqp4tPN3PEOZ6i1bv54XA.png" /></figure><p>This confirmed ArgoCD was reachable <em>inside</em> WSL.</p><h4>Step 2 — Install socat inside WSL</h4><pre>sudo apt install socat -y</pre><p>socat allows us to expose the WSL port to a different host port so Windows can reach it.</p><h4>Step 3 — Create a Windows-reachable bridge port</h4><p>Run socat in WSL:</p><pre>socat TCP-LISTEN:3000,fork TCP:127.0.0.1:8181</pre><p>Meaning:</p><p>SourceTargetWindows → port 3000WSL → ArgoCD → 8181</p><h4>Step 4 — Expose WSL port to Windows using portproxy</h4><p>In PowerShell (Admin):</p><pre>netsh interface portproxy set v4tov4 `<br>  listenport=3000 listenaddress=0.0.0.0 `<br>  connectport=3000 connectaddress=127.0.0.1</pre><p>Firewalls were updated:</p><pre>New-NetFirewallRule -DisplayName &quot;ArgoCD-3000&quot; `<br>  -Direction Inbound -Protocol TCP -LocalPort 3000 -Action Allow</pre><h4>Step 5 — Access ArgoCD in Browser</h4><p>Finally:</p><p><a href="https://localhost:3000/"><strong>https://localhost:3000</strong></a></p><p>And the ArgoCD UI loaded successfully:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TIKF9dydHhD9_ZdSwyYh9Q.png" /></figure><p>This proved the full bridge was functioning:</p><pre>Browser (Windows) → Windows portproxy → WSL socat → kubectl port-forward → ArgoCD Pod → EKS Cluster</pre><h4>2.4 Retrieving the Initial Admin Password</h4><p>From WSL:</p><pre>kubectl -n argocd get secret argocd-initial-admin-secret \<br>  -o jsonpath=&quot;{.data.password}&quot; | base64 -d</pre><p>Enter:</p><ul><li><strong>Username:</strong> admin</li><li><strong>Password:</strong> (the retrieved value)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mo2Bq6cwmkxickEN8Gymmg.png" /></figure><p>You are now logged into ArgoCD successfully.</p><h3>Section 3 — Preparing the Application &amp; GitOps Repositories</h3><p>In a GitOps-driven deployment model, the separation of concerns between <strong>application code</strong> and <strong>deployment configuration</strong> is crucial. To achieve this clean separation, we use <strong>two repositories</strong>, each serving a distinct purpose.</p><h4>3.1 Repository Structure Overview</h4><h4>1. Application Repository (My Django Blog App)</h4><p>This repository contains all source code and CI logic. Typical structure:</p><pre>PERCY-BLOG-WEBSITE/<br>│<br>├── blogsite/<br>├── blog/<br>├── assets/<br>├── static/<br>├── docker/<br>│   └── Dockerfile<br>├── Jenkinsfile<br>└── requirements.txt</pre><p>This is where Jenkins will:</p><ul><li>Pull code</li><li>Build and test</li><li>Build the Docker image</li><li>Push the image to ECR</li></ul><h4>2. GitOps Repository (Kubernetes Manifests)</h4><p>This repository contains only Kubernetes deployment configuration. Typical layout:</p><pre>gitops-blog-deploy/<br>│<br>└── k8s/<br>    ├── deployment.yaml<br>    ├── service.yaml<br>    ├── ingress.yaml<br>    └── kustomization.yaml (optional)</pre><p>This repo is watched continuously by <strong>ArgoCD</strong>.</p><p>Whenever Jenkins updates the manifests (e.g., new Docker image tag), ArgoCD automatically syncs the change into the EKS cluster.</p><h4>3.2 Why Separate the Repositories? (GitOps Principle)</h4><p>GitOps requires that:</p><ul><li><strong>Application code</strong> lives in one repository</li><li><strong>Deployment state</strong> lives in a separate, declarative repository</li></ul><p>This separation brings several key benefits:</p><h4>Auditability</h4><p>Every deployment is a Git commit.<br>Every rollback is a Git revert.</p><h4>Security</h4><p>Developers do <strong>not</strong> need kubectl access to the cluster.<br>Everything goes through Git.</p><h4>Stability</h4><p>Rollbacks are as simple as reverting a Git commit.</p><h4>Automation</h4><p>ArgoCD continuously reconciles Kubernetes ↔ Git.<br>Kubernetes is <em>never</em> edited manually.</p><p>This architecture is <strong>industry standard</strong>, used by:</p><ul><li>Intuit (creators of ArgoCD)</li><li>Red Hat</li><li>Weaveworks</li><li>Large-scale Kubernetes teams</li></ul><h4>3.3 Preparing Kubernetes Manifests</h4><p>Here is a clean and production-ready deployment.yaml for the Django app:</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: percy-blog<br>  labels:<br>    app: percy-blog<br>spec:<br>  replicas: 2<br>  selector:<br>    matchLabels:<br>      app: percy-blog<br>  template:<br>    metadata:<br>      labels:<br>        app: percy-blog<br>    spec:<br>      containers:<br>        - name: percy-blog<br>          image: &lt;AWS_ACCOUNT_ID&gt;.dkr.ecr.eu-north-1.amazonaws.com/percy-blog:latest<br>          ports:<br>            - containerPort: 8000<br>          env:<br>            - name: DJANGO_SETTINGS_MODULE<br>              value: blogsite.settings</pre><p>The service:</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: percy-blog-service<br>spec:<br>  selector:<br>    app: percy-blog<br>  ports:<br>    - protocol: TCP<br>      port: 80<br>      targetPort: 8000<br>  type: LoadBalancer</pre><p>Ingress (optional for Route53 later):</p><pre>apiVersion: networking.k8s.io/v1<br>kind: Ingress<br>metadata:<br>  name: percy-blog-ingress<br>spec:<br>  rules:<br>    - http:<br>        paths:<br>        - path: /<br>          pathType: Prefix<br>          backend:<br>            service:<br>              name: percy-blog-service<br>              port:<br>                number: 80</pre><p>These files will go into my <strong>GitOps repo</strong>, not my application repo.</p><h4>3.4 Jenkins Will Update Only This Repo</h4><p>Later, when Jenkins builds a new Docker image, it will:</p><ol><li>Change the image tag in deployment.yaml</li><li>Commit the update</li><li>Push to my GitOps repo</li></ol><p>ArgoCD will detect this automatically and:</p><ul><li>Pull the new manifest</li><li>Compare cluster state with Git</li><li>Sync the change</li><li>Roll out the new version</li></ul><p>This is the heart of GitOps.</p><h4>3.5 What I Have Now</h4><p>By completing this section, I now have:</p><p>A clear separation between app code and deployment config<br> A GitOps-ready repository structure<br> Initial Kubernetes manifests for the Django app<br> A foundation that Jenkins + ArgoCD will automate</p><p>My system is now ready for the CI/CD pipeline integration.</p><h4>Section 4: Setting Up the CI Pipeline in Jenkins</h4><p>With ArgoCD and the GitOps repository prepared, the next major step is building the CI system that turns source code into deployed infrastructure.</p><p>This is where <strong>Jenkins</strong> comes in.</p><p>Jenkins will:</p><ol><li>Pull the application source code from GitHub</li><li>Build a Docker image for the Django app</li><li>Authenticate to Amazon ECR</li><li>Push the image to the ECR repository</li><li>Pull the GitOps repo and update the Kubernetes manifests</li><li>Commit &amp; push the change</li><li>ArgoCD automatically syncs and deploys the new version to EKS</li></ol><p>This is the complete, automated CI → GitOps → CD flow.</p><h4>4.1 Installing Jenkins on EC2 (Recommended Architecture)</h4><p>Although Jenkins can run on-prem or on local machines, the cleanest GitOps architecture is:</p><ul><li><strong>Jenkins runs inside an EC2 instance</strong></li><li><strong>EKS runs as its own service</strong></li><li><strong>WSL is used for development only</strong></li></ul><p>Create a dedicated EC2 instance to host Jenkins.</p><h4>EC2 Instance Requirements</h4><p>Ubuntu 22.04 LTS, t3.medium, 20–30GB, Port <strong>8080</strong> for Jenkins, Port <strong>22</strong> for SSH</p><p>SSH into the instance:</p><pre>ssh -i &lt;your-key&gt;.pem ubuntu@&lt;EC2_PUBLIC_IP&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K1jK4tOKwn9f9_vQpqZ1vw.png" /></figure><h4>4.1.1 Install Java and Jenkins</h4><p>Update the server:</p><pre>sudo apt update<br>sudo apt install fontconfig openjdk-17-jre -y</pre><p>Install Jenkins package key:</p><pre>curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key \<br>  | sudo tee /usr/share/keyrings/jenkins-keyring.asc &gt; /dev/null</pre><p>Add Jenkins repository:</p><pre>echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \<br>  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \<br>  /etc/apt/sources.list.d/jenkins.list &gt; /dev/null</pre><p>Install Jenkins:</p><pre>sudo apt update<br>sudo apt install jenkins -y</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0eLI7JDuW49stu0HqpZJfA.png" /></figure><p>Enable and start the service:</p><pre>sudo systemctl enable jenkins<br>sudo systemctl start jenkins</pre><p>Check status:</p><pre>sudo systemctl status jenkins</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U485VWDAYZrbVAbN1cMM2g.png" /></figure><h4>4.1.2 Access Jenkins UI</h4><p>Open in browser:</p><pre>http://&lt;EC2_PUBLIC_IP&gt;:8080</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C0XM2x5-iub6NlPa6DF-zw.png" /></figure><p>Retrieve admin password:</p><pre>sudo cat /var/lib/jenkins/secrets/initialAdminPassword</pre><p>Complete the setup wizard:</p><ul><li>Install suggested plugins</li><li>Create your admin user</li></ul><h4>4.2 Install Required Jenkins Plugins</h4><p>Inside Jenkins UI:</p><p><strong>Manage Jenkins → Plugins</strong></p><ul><li>Docker Pipeline</li><li>Credentials Binding</li><li>SSH Agent</li><li>Amazon ECR plugin (optional)</li></ul><p>Restart Jenkins when prompted.</p><h4>4.3 Configure AWS Credentials for Jenkins</h4><p>Create an IAM User in AWS:</p><pre>jenkins-user</pre><p>Attach policies:</p><ul><li><strong>AmazonEC2ContainerRegistryFullAccess</strong></li><li><strong>AmazonEKSReadOnlyAccess</strong></li><li><strong>CloudWatchLogsFullAccess</strong> (optional)</li><li><strong>AmazonS3FullAccess</strong> (optional)</li></ul><p>Generate Access Key ID + Secret Key.</p><p>Now go to:</p><p><strong>Jenkins → Manage Jenkins → Credentials → System → Global → Add credential</strong></p><p>Choose <strong>Username &amp; Password</strong>:</p><p>Fill everything.</p><p>Save.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2sXKVrvSv2DK6lnRdSQJQw.png" /></figure><h4>4.4 Configure Docker Build Environment on Jenkins EC2</h4><p>Jenkins must be able to run Docker commands.</p><p>Install Docker:</p><pre>sudo apt install ca-certificates curl gnupg -y</pre><p>Add Docker repo:</p><pre>sudo install -m 0755 -d /etc/apt/keyring<br>curl -fsSL https://download.docker.com/linux/ubuntu/gpg \<br>    | sudo tee /etc/apt/keyrings/docker.asc &gt; /dev/null<br>sudo chmod a+r /etc/apt/keyrings/docker.asc</pre><pre>echo \<br>  &quot;deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \<br>  https://download.docker.com/linux/ubuntu $(. /etc/os-release &amp;&amp; echo &quot;$VERSION_CODENAME&quot;) stable&quot; \<br>  | sudo tee /etc/apt/sources.list.d/docker.list &gt; /dev/null</pre><p>Install Docker engine:</p><pre>sudo apt update<br>sudo apt install docker-ce docker-ce-cli containerd.io -y</pre><p>Give Jenkins permission:</p><pre>sudo usermod -aG docker jenkins<br>sudo systemctl restart jenkins</pre><h4>4.5 Create the CI Pipeline (Jenkinsfile)</h4><p>Inside our application repo (PERCY-BLOG-WEBSITE), create a file:</p><pre>Jenkinsfile</pre><p>Paste the full GitOps pipeline:</p><pre>pipeline {<br>    agent any<br>    environment {<br>        AWS_DEFAULT_REGION = &quot;eu-north-1&quot;<br>        ECR_REPO = &quot;&lt;AWS_ACCOUNT_ID&gt;.dkr.ecr.eu-north-1.amazonaws.com/percy-blog&quot;<br>        GITOPS_REPO = &quot;https://github.com/Shakurrrr/gitops-blog-deploy.git&quot;<br>        GITOPS_DIR = &quot;gitops-blog-deploy/k8s&quot;<br>    }</pre><pre>    stages {<br>        stage(&#39;Checkout App Repo&#39;) {<br>            steps {<br>                git branch: &#39;main&#39;, url: &#39;https://github.com/Shakurrrr/PERCY-BLOG-WEBSITE.git&#39;<br>            }<br>        }</pre><pre>        stage(&#39;Login to AWS ECR&#39;) {<br>            steps {<br>                withCredentials([usernamePassword(<br>                    credentialsId: &#39;aws-creds&#39;,<br>                    passwordVariable: &#39;AWS_SECRET&#39;,<br>                    usernameVariable: &#39;AWS_KEY&#39;<br>                )]) {<br>                    sh &#39;&#39;&#39;<br>                    aws configure set aws_access_key_id $AWS_KEY<br>                    aws configure set aws_secret_access_key $AWS_SECRET<br>                    aws configure set default.region ${AWS_DEFAULT_REGION}</pre><pre>                    aws ecr get-login-password --region eu-north-1 \<br>                      | docker login --username AWS --password-stdin ${ECR_REPO}<br>                    &#39;&#39;&#39;<br>                }<br>            }<br>        }</pre><pre>        stage(&#39;Build Docker Image&#39;) {<br>            steps {<br>                script {<br>                    IMAGE_TAG = &quot;v${BUILD_NUMBER}&quot;<br>                }<br>                sh &#39;&#39;&#39;<br>                  docker build -t ${ECR_REPO}:${IMAGE_TAG} .<br>                &#39;&#39;&#39;<br>            }<br>        }</pre><pre>        stage(&#39;Push to ECR&#39;) {<br>            steps {<br>                sh &#39;&#39;&#39;<br>                  docker push ${ECR_REPO}:${IMAGE_TAG}<br>                &#39;&#39;&#39;<br>            }<br>        }</pre><pre>        stage(&#39;Update GitOps repo&#39;) {<br>            steps {<br>                sh &#39;&#39;&#39;<br>                rm -rf gitops-blog-deploy<br>                git clone ${GITOPS_REPO}</pre><pre>                sed -i &quot;s|image:.*|image: ${ECR_REPO}:${IMAGE_TAG}|&quot; \<br>                  ${GITOPS_DIR}/deployment.yaml</pre><pre>                cd gitops-blog-deploy<br>                git config --global user.email &quot;jenkins@ci.com&quot;<br>                git config --global user.name &quot;Jenkins CI&quot;<br>                git add .<br>                git commit -m &quot;Update image tag to ${IMAGE_TAG}&quot;<br>                git push<br>                &#39;&#39;&#39;<br>            }<br>        }<br>    }</pre><pre>    post {<br>        success {<br>            echo &quot;Deployment triggered through GitOps → ArgoCD → EKS!&quot;<br>        }<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zcMqkjbi_4-pCccFp6cuuQ.png" /></figure><h4>4.6 Create the Jenkins Job</h4><p>Inside Jenkins:</p><ol><li><strong>New Item</strong></li><li>Choose <strong>Pipeline</strong></li><li>Name: percy-blog-ci</li><li>Select <strong>Pipeline from SCM</strong></li><li>SCM: <strong>Git</strong></li><li>Repository URL:</li></ol><pre>https://github.com/Shakurrrr/PERCY-BLOG-WEBSITE.git</pre><p>Save → Build Now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m16Cd4cBP0UHhnMH0mRRMg.png" /></figure><h4>4.7 Validate the Deployment</h4><p>Check pods in EKS:</p><pre>kubectl get pods</pre><p>Expected:</p><pre>percy-blog-xxxx  Running</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*UFRXbObaHAfqwZLJ9bwNiw.png" /></figure><p>Check service:</p><pre>kubectl get svc percy-blog-service</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/991/1*dxEjDFLMFq3AQGqT8dRm-A.png" /></figure><p>Open in browser:</p><pre>http://&lt;elb-dns-name&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*blO3ipXncHXpWePx_12_ZQ.png" /></figure><p>If everything is correct, the Django application should now load from EKS — deployed through GitOps automatically.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b5322df0c2c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[System Information Script (Linux + Shell Scripting for DevOps)]]></title>
            <link>https://medium.com/@shehuyusuf/system-information-script-linux-shell-scripting-for-devops-1e502ca014ef?source=rss-607ba1670d2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e502ca014ef</guid>
            <dc:creator><![CDATA[Shehu Yusuf]]></dc:creator>
            <pubDate>Wed, 03 Dec 2025 10:12:27 GMT</pubDate>
            <atom:updated>2025-12-03T10:12:27.829Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Building a Foundational Diagnostics Tool Every DevOps Engineer Must Know</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v9TyAQ-D57WoOXQuH94e1w.png" /></figure><h3><strong>Introduction</strong></h3><p>Before diving into advanced DevOps tools like Kubernetes, Terraform, Jenkins, or AWS automation, every engineer must first master Linux fundamentals and shell scripting.<br>Linux servers power almost every modern cloud platform — AWS EC2, Azure VMs, Google Compute Engine, EKS nodes, Docker containers, Kubernetes clusters, and even serverless runtimes internally.</p><p>One of the most essential capabilities of a DevOps engineer is the ability to quickly assess the health of a system, especially during troubleshooting, incident response, or performance monitoring.</p><p>This project teaches you how to build a comprehensive Linux system information script, the same kind of script used by SREs, DevOps engineers, and Cloud administrators during production incidents.</p><p>By the end of this project, you will understand:</p><p><em>Core Linux commands</em></p><p><em>Scripting fundamentals</em></p><p><em>How to gather key system metrics</em></p><p><em>How real engineers debug live servers</em></p><p><em>How to automate system checks into a single command</em></p><h4><strong>Why This Script Is Important</strong></h4><p>During outages or performance issues, teams often start with the same basic questions:</p><p><em>Is the server overloaded?</em></p><p><em>Is the CPU spiking?</em></p><p><em>Is the RAM full?</em></p><p><em>Is the disk out of space?</em></p><p><em>Is a rogue process consuming resources?</em></p><p><em>Is someone logged into the machine right now?</em></p><p>In production systems, these checks happen constantly.<br>This script provides a single command that gives you a full diagnostic overview — like a doctor running vitals before deeper investigation.</p><p>This ability separates beginners from real DevOps professionals.</p><h3><strong>The Complete system-info.sh Script (Production-Style)</strong></h3><p>Below is the full script with clean formatting, comments, and readable output.</p><pre>#!/bin/bash</pre><pre># ==========================================================<br>#  SYSTEM INFORMATION SCRIPT<br>#  Author: Shakur<br>#  Purpose: Provide a complete diagnostic summary for Linux<br># ==========================================================</pre><pre>echo &quot;====================================================&quot;<br>echo &quot;                SYSTEM INFORMATION REPORT            &quot;<br>echo &quot;====================================================&quot;<br>echo</pre><pre># -------------------------<br># OS Information<br># -------------------------<br>echo &quot;🟢 OS INFORMATION:&quot;<br>if command -v lsb_release &gt;/dev/null 2&gt;&amp;1; then<br>    lsb_release -a<br>else<br>    cat /etc/os-release<br>fi<br>echo</pre><pre># -------------------------<br># Kernel Version<br># -------------------------<br>echo &quot;🟢 KERNEL VERSION:&quot;<br>uname -r<br>echo</pre><pre># -------------------------<br># CPU Information<br># -------------------------<br>echo &quot;🟢 CPU INFORMATION:&quot;<br>lscpu | grep -E &#39;Model name|CPU\(s\)|Thread|Core|MHz&#39;<br>echo</pre><pre># -------------------------<br># Memory Usage<br># -------------------------<br>echo &quot;🟢 MEMORY (RAM) USAGE:&quot;<br>free -h<br>echo</pre><pre># -------------------------<br># Disk Usage<br># -------------------------<br>echo &quot;🟢 DISK USAGE:&quot;<br>df -h --total | grep -E &quot;Filesystem|total&quot;<br>echo</pre><pre># -------------------------<br># Top Running Processes<br># -------------------------<br>echo &quot;🟢 TOP 10 RUNNING PROCESSES (by CPU usage):&quot;<br>ps -eo pid,ppid,cmd,%cpu,%mem --sort=-%cpu | head<br>echo</pre><pre># -------------------------<br># Logged-in Users<br># -------------------------<br>echo &quot;🟢 LOGGED-IN USERS:&quot;<br>who<br>echo</pre><pre>echo &quot;====================================================&quot;<br>echo &quot;            END OF SYSTEM INFORMATION REPORT         &quot;<br>echo &quot;====================================================&quot;</pre><h4>How To Use This Script</h4><h4>1. Create the file</h4><pre>nano system-info.sh</pre><p>Paste the script.</p><h4>2. Make it executable</h4><pre>chmod +x system-info.sh</pre><h4>3. Run it</h4><pre>./system-info.sh</pre><p>You’ll see a complete, professional system report.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*usqWtR22k2tW5jjQ2rYa8Q.png" /></figure><h3>Deep Dive: What Each Section Teaches You</h3><p>This section is extremely important for Medium readers — it transforms your post from “just code” into “real DevOps learning”.</p><h4>1. OS Information</h4><p>Shows the Linux distribution and version.<br> Helpful when determining:</p><ul><li>Package manager</li><li>Kernel compatibility</li><li>Installation methods</li><li>Debug compatibility (Ubuntu vs CentOS vs Amazon Linux)</li></ul><h4>2. Kernel Version</h4><p>The kernel controls memory, networking, drivers, and process scheduling.<br> Knowing the kernel helps diagnose:</p><ul><li>Docker compatibility</li><li>Kubernetes node failures</li><li>Network driver bugs</li><li>System crashes</li></ul><h4>3. CPU Information</h4><p>Displays:</p><ul><li>Total cores</li><li>Threads</li><li>Clock speed</li><li>CPU model</li></ul><p>Used for:</p><ul><li>Performance analysis</li><li>Capacity planning</li><li>Debugging throttling</li></ul><h4>4. RAM Usage</h4><p>If a server is swapping or memory is full, everything slows down.<br> This helps identify:</p><ul><li>Memory leaks</li><li>Heavy applications</li><li>Under-provisioned servers</li></ul><h4>5. Disk Usage</h4><p>Running out of disk is one of the <strong>most common production issues</strong>.</p><p>This helps you catch:</p><ul><li>Full Docker volumes</li><li>Logs consuming space</li><li>Application uploads filling storage</li><li>Insufficient root partition</li></ul><h4>6. Running Processes</h4><p>Shows the <strong>top 10 processes using the most CPU</strong>.</p><p>Critical for troubleshooting:</p><ul><li>Rogue processes</li><li>High CPU usage</li><li>Stuck loops</li><li>Attacks or malware</li></ul><h4>7. Logged-In Users</h4><p>You can see:</p><ul><li>Who is connected</li><li>Whether someone is troubleshooting</li><li>Unauthorized access</li><li>Parallel work on the same node</li></ul><h4>Conclusion</h4><p>Mastering this project is far more valuable than simply writing another Bash script. It teaches you to think the way real DevOps engineers and SREs think when they log into a server during an incident, when performance suddenly drops, or when a production node begins misbehaving. By learning how to gather system information, interpret resource usage, and automate diagnostic tasks into a single command, you build the foundational instincts required for every higher-level DevOps tool that comes later. Whether you are troubleshooting Docker containers, debugging Kubernetes nodes, analyzing EC2 performance, or preparing an environment for CI/CD pipelines, these skills remain essential.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e502ca014ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Install Minikube and Kind on WSL2 for Local Kubernetes Deployment]]></title>
            <link>https://medium.com/@shehuyusuf/how-to-install-minikube-and-kind-on-wsl2-for-local-kubernetes-deployment-9977e340e3e9?source=rss-607ba1670d2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/9977e340e3e9</guid>
            <dc:creator><![CDATA[Shehu Yusuf]]></dc:creator>
            <pubDate>Tue, 02 Dec 2025 22:02:33 GMT</pubDate>
            <atom:updated>2025-12-02T22:02:33.616Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A full guide for DevOps engineers, cloud learners, and backend developers transitioning into platform engineering</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MaA_r-QvTlT8Z82IEu3GfQ.png" /></figure><h3>Introduction</h3><p>Kubernetes has become the backbone of modern cloud-native infrastructure. Whether you’re deploying microservices, building CI/CD pipelines, or practicing Infrastructure-as-Code, understanding Kubernetes is no longer optional — it’s essential.</p><p>But learning Kubernetes doesn’t require an expensive cloud subscription.<br> With <strong>WSL2 (Windows Subsystem for Linux)</strong>, you can run a full-featured Kubernetes environment <strong>directly on your Windows machine</strong>.</p><p>In this guide, we’ll install and configure two powerful Kubernetes tools:</p><h4>1. Minikube — a full local Kubernetes cluster</h4><h4>2. Kind — ultra-fast Kubernetes clusters running inside Docker</h4><p>Both of these are widely used in:</p><ul><li>Local development</li><li>DevOps pipelines</li><li>Testing deployments</li><li>Practicing manifests</li><li>Running Ingress controllers</li><li>Experimenting with microservices</li></ul><p>When installed together, they form an incredibly powerful learning and development setup.</p><p>This guide is designed for <strong>clarity, depth, and real-world workflow</strong>. By the end, you’ll have <strong>two Kubernetes clusters</strong>, each with different strengths, running flawlessly on your WSL2 Ubuntu environment.</p><p>Let’s begin.</p><h3>Prerequisites</h3><p>Before starting, ensure you have:</p><p><strong>Windows 10/11</strong><br> <strong>WSL2 enabled</strong><br> <strong>Ubuntu 22.04 or 24.04 installed on WSL2</strong><br> <strong>Docker Engine installed inside WSL2</strong> (not Docker Desktop)</p><p>You can verify Docker is working:</p><pre>docker ps</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*92tdEDVzV-_n16xnRIZZgA.png" /></figure><h3><strong>Section 1 — Installing Minikube on WSL2</strong></h3><p>Minikube is one of the most popular tools for learning and experimenting with Kubernetes. It runs a complete Kubernetes node (control plane + worker) inside a lightweight VM or container.</p><p>In WSL2, the best runtime is <strong>Docker Engine</strong>.</p><h4>Step 1 — Download and Install Minikube</h4><p>Run these commands inside your WSL2 Ubuntu terminal:</p><pre>curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb<br>sudo dpkg -i minikube_latest_amd64.deb</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nDQQTlNPrxXn7aIgRFVySw.png" /></figure><p>Verify installation:</p><pre>minikube version</pre><p>If Minikube prints a version, installation was successful.</p><h4>Step 2 — Install the Kubernetes CLI (kubectl)</h4><p>Minikube ships with its own kubectl,<br> but DevOps engineers always install the standalone version.</p><p>Run:</p><pre>sudo apt update<br>sudo apt install -y curl</pre><pre>curl -LO &quot;https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl&quot;</pre><pre>chmod +x kubectl<br>sudo mv kubectl /usr/local/bin/</pre><p>Verify:</p><pre>kubectl version --client</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZdhMLtMpDEdHlgRZHg7QtQ.png" /></figure><h4>Step 3 — Start Minikube Using Docker Engine</h4><p>This is the most important step.</p><p>Since we’re using WSL2 + Docker Engine,<br> the correct startup command is:</p><pre>minikube start --driver=docker</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*KS7Jns_CGstlbycmNnHW0A.png" /></figure><p>What this does:</p><ul><li>Creates a Kubernetes node inside a Docker container</li><li>Downloads the Kubernetes components</li><li>Sets up the control plane</li><li>Writes your kubeconfig</li><li>Connects kubectl to your new cluster</li></ul><p>Check the node status:</p><pre>kubectl get nodes</pre><p>Expected output:</p><pre>NAME       STATUS   ROLES                  AGE   VERSION<br>minikube   Ready    control-plane        3m18s   v1.34.0</pre><p>At this point, we now have a working Kubernetes cluster.</p><h3>Section 2 — Enabling Key Minikube Addons</h3><p>Minikube supports many optional add-ons that enhance local Kubernetes development.</p><p>Let’s enable the essentials.</p><h4>Step 1— Enable the Ingress Controller</h4><p>Ingress lets you expose Kubernetes services using hostnames like(my personal blog project):</p><pre>http://percyblog.local</pre><p>Enable it:</p><pre>minikube addons enable ingress</pre><p>Check the controller:</p><pre>kubectl get pods -n ingress-nginx</pre><p>You should see:</p><pre>ingress-nginx-controller-xxxxx   Running</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*k8iBq0-HUkBb03X4CCIMrg.png" /></figure><h4>Step 2— Enable the Kubernetes Dashboard</h4><p>The dashboard is extremely useful when learning Kubernetes.</p><p>Enable:</p><pre>minikube addons enable dashboard</pre><p>Launch the dashboard:</p><pre>minikube dashboard</pre><p>This opens a web UI showing:</p><ul><li>Pods</li><li>Deployments</li><li>Logs</li><li>Services</li><li>Events</li><li>Cluster resources</li></ul><p>As seen below. Perfect for visual learning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SQHGCzkT0_tIeVbggFuvpQ.png" /><figcaption>Minikube Dashboard</figcaption></figure><h3>Section 3 — Installing Kind (Kubernetes in Docker)</h3><p>While Minikube is ideal for a full-featured cluster, <strong>Kind</strong> is unbeatable for:</p><ul><li>CI/CD pipelines</li><li>GitHub Actions</li><li>Terraform experimentation</li><li>Multi-cluster setups</li><li>Lightning-fast cluster creation</li></ul><p>Kind runs Kubernetes nodes as Docker containers — super efficient.</p><h4>Step 1— Install Kind</h4><p>Run:</p><pre>curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64<br>chmod +x kind<br>sudo mv kind /usr/local/bin/</pre><p>What this does:</p><ul><li>curl -Lo ./kind downloads the Kind binary</li><li>chmod +x makes it executable</li><li>mv moves it into your PATH so you can run kind globally</li></ul><p>Verify:</p><pre>kind version</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1009/1*6OQZn-LhpuhT7MMzxii74Q.png" /></figure><h4>Step 2— Create Your First Kind Cluster</h4><pre>kind create cluster</pre><p>Kind will:</p><ul><li>Pull Kubernetes images</li><li>Create a control-plane container</li><li>Configure kubeconfig</li><li>Connect kubectl automatically</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/986/1*T0HpEd7X_M73GbRIPD02LQ.png" /></figure><p>Check the nodes:</p><pre>kubectl get nodes</pre><p>If you see something like:</p><pre>kind-control-plane   Ready   control-plane   1m   v1.30.x</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*G43a5S1wEKPllU4ahH6_rg.png" /></figure><p>We’re good to go.</p><h3>Section 4 — Minikube vs Kind: When to Use Each</h3><p>Understanding when to use <strong>Minikube</strong> vs <strong>Kind</strong> is an essential DevOps skill.<br> Both are local Kubernetes tools, but they are designed for <strong>different workflows</strong>.</p><h4>When Should You Use Minikube?</h4><p>Choose <strong>Minikube</strong> when you want:</p><ul><li>A <strong>learning-friendly</strong> Kubernetes experience</li><li>GUI tools like <strong>Kubernetes Dashboard</strong></li><li>To experiment with <strong>add-ons</strong> (metrics-server, storage, registry, etc.)</li><li>A more “VM-like” single-node cluster</li></ul><p>Minikube behaves more like a <strong>developer workstation cluster</strong>.</p><h4>When Should You Use Kind?</h4><p>Choose <strong>Kind</strong> when you need:</p><ul><li>Fast Kubernetes cluster startup</li><li>CI/CD pipelines (GitHub Actions, Jenkins, GitLab Runner)</li><li>Repeatable automation</li><li>Lightweight clusters running <strong>inside Docker</strong></li><li>Multiple clusters for testing (multi-cluster, GitOps, service mesh experiments)</li></ul><p>Kind is the tool used by:</p><ul><li>Kubernetes SIG-testing</li><li>FluxCD</li><li>ArgoCD contributors</li><li>Many production CI systems</li></ul><h4>Why I Use Both in This Project</h4><p>Using both tools gives a <strong>real DevOps mastery</strong></p><ul><li><strong>Minikube</strong> → great for experimenting interactively</li><li><strong>Kind</strong> → better for automated, scripted deployments</li></ul><p>Together, they mirror how real companies work:</p><blockquote><strong><em>Develop locally on Minikube →<br> Test &amp; automate on Kind →<br> Deploy to cloud Kubernetes</em></strong></blockquote><p>This project teaches us both workflows.</p><h3>Section 5 — What’s Next (Deploying Your First App)</h3><p>Now that our Kubernetes tooling is fully installed (Minikube + Kind + kubectl), it’s time to deploy a <strong>real application</strong> into the cluster.</p><p>In this section, we’d take our Django project — <em>Percy Blog</em> — and gradually convert it into a fully containerized, scalable Kubernetes workload.</p><p>This is where Kubernetes goes from “installed” to “useful.”</p><h4>Step 1 — Create a Kubernetes Namespace</h4><p>Namespaces help organize workloads.<br> We’ll create one specifically for our blog:</p><p><strong>namespace.yaml</strong></p><pre>apiVersion: v1<br>kind: Namespace<br>metadata:<br>  name: percyblog</pre><p>Apply it:</p><pre>kubectl apply -f namespace.yaml</pre><p>You should now see it:</p><pre>kubectl get ns</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/989/1*Gk876YwevX7pEQwhefLg7w.png" /></figure><h4>Step 2 — Create the Application ConfigMap</h4><p>ConfigMaps store <strong>non-sensitive environment variables</strong> like:</p><ul><li>DEBUG</li><li>ALLOWED_HOSTS</li><li>database URLs (optional)</li><li>feature flags</li></ul><p>Below is our configmap configuration:</p><p>📄 <strong>configmap.yaml</strong></p><pre>apiVersion: v1<br>kind: ConfigMap<br>metadata:<br>  name: percyblog-config<br>  namespace: percyblog<br>data:<br>  DEBUG: &quot;True&quot;<br>  DJANGO_ALLOWED_HOSTS: &quot;*&quot;<br>  DJANGO_SETTINGS_MODULE: &quot;blogsite.settings&quot;</pre><p>Apply it:</p><pre>kubectl apply -f configmap.yaml</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*2UzK4OurhWaujQVjQ1vKCQ.png" /></figure><h4>Step 3 — Create a Kubernetes Secret</h4><p>Secrets store sensitive values like our Django SECRET_KEY.</p><p>First, let’s generate our own Django secret key using:</p><pre>python3 -c &#39;import secrets; print(secrets.token_urlsafe(50))&#39;</pre><p>Copy the output.</p><p><strong>secret.yaml</strong></p><pre>apiVersion: v1<br>kind: Secret<br>metadata:<br>  name: percyblog-secret<br>  namespace: percyblog<br>type: Opaque<br>stringData:<br>  DJANGO_SECRET_KEY: &quot;your-secret-key-here&quot;</pre><p>Apply:</p><pre>kubectl apply -f secret.yaml</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/1*ulBUVuy0MI7sttVuVr10QQ.png" /></figure><h4>Step 4 — Create the Deployment (Django App)</h4><p>This is the core part: running our Django container in Kubernetes.</p><p>I’ll use the Docker image we already built &amp; pushed:</p><pre>shehu98/percy-blog-website:latest</pre><p><strong>deployment.yaml</strong></p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: percyblog-app<br>  namespace: percyblog<br>spec:<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      app: percyblog<br>  template:<br>    metadata:<br>      labels:<br>        app: percyblog<br>    spec:<br>      containers:<br>        - name: percyblog<br>          image: shehu98/percy-blog-website:latest<br>          ports:<br>            - containerPort: 8000<br>          envFrom:<br>            - configMapRef:<br>                name: percyblog-config<br>            - secretRef:<br>                name: percyblog-secret<br>          readinessProbe:<br>            httpGet:<br>              path: /<br>              port: 8000<br>            initialDelaySeconds: 10<br>            periodSeconds: 5<br>          livenessProbe:<br>            httpGet:<br>              path: /<br>              port: 8000<br>            initialDelaySeconds: 20<br>            periodSeconds: 10</pre><p>Apply it:</p><pre>kubectl apply -f deployment.yaml</pre><p>Check:</p><pre>kubectl get pods -n percyblog</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/986/1*cL2wVIFHUdq-jIVGXp5_0A.png" /></figure><h4>Step 5 — Expose the App with a Service</h4><p>At this point, our Django application is successfully running inside the cluster, but it is <strong>not reachable</strong> — neither from the browser nor from an Ingress.</p><p>Kubernetes Pods have <strong>ephemeral IPs</strong>, meaning they change on restarts.<br> We need a <strong>stable internal endpoint</strong> for routing traffic.</p><p>That’s where a <em>Service</em> comes in.</p><p><strong>service.yaml</strong></p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: percyblog-service<br>  namespace: percyblog<br>spec:<br>  selector:<br>    app: percyblog<br>  ports:<br>    - port: 8000<br>      targetPort: 8000<br>  type: ClusterIP</pre><p>Apply:</p><pre>kubectl apply -f service.yaml</pre><p>Verify:</p><pre>kubectl get svc -n percyblog</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*eSmmmGPEpQCqJjOQ5EUgDQ.png" /><figcaption>Cluster-IP</figcaption></figure><h4>Step 6 — Create an Ingress (percyblog.local)</h4><p>To make our Django application accessible through a custom hostname, we’ll set up an <strong>Ingress</strong>.</p><p>What Ingress Does:</p><ul><li><em>Acts as an HTTP/HTTPS entry point</em></li><li><em>Routes traffic based on hostname or path rules</em></li><li><em>Works with the NGINX Ingress controller we enabled earlier</em></li></ul><p><strong>ingress.yaml</strong></p><pre>apiVersion: networking.k8s.io/v1<br>kind: Ingress<br>metadata:<br>  name: percyblog-ingress<br>  namespace: percyblog<br>  annotations:<br>    nginx.ingress.kubernetes.io/rewrite-target: /<br>spec:<br>  ingressClassName: nginx<br>  rules:<br>    - host: percyblog.local<br>      http:<br>        paths:<br>          - path: /<br>            pathType: Prefix<br>            backend:<br>              service:<br>                name: percyblog-service<br>                port:<br>                  number: 8000</pre><p>Apply:</p><pre>kubectl apply -f ingress.yaml</pre><p>Check ingress:</p><pre>kubectl get ingress -n percyblog</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/986/1*5DVX7VNLNKdgzFJiqMGJSw.png" /></figure><h3>Section 6— Making Ingress Reachable From Windows (The Reliable WSL2 → Kind Bridge)</h3><p>By this point, our Django app is deployed inside Kind, and the Ingress is working correctly <strong>inside WSL2</strong>.<br> However, on Windows, we still cannot access:</p><pre>http://percyblog.local:30080</pre><h4>Why Windows Cannot Reach Kind Directly</h4><p>Kind runs inside Docker,<br> Docker runs inside WSL2,<br> so Kind’s node IP looks like:</p><pre>172.19.x.x</pre><p>Windows <strong>cannot route traffic into Docker subnets inside WSL2</strong>, so NodePort traffic never reaches the cluster.</p><p>But Windows <strong>CAN</strong> reach the primary WSL2 IP:</p><pre>wsl hostname -I<br># Example:<br>172.31.28.31</pre><p>This means we need a bridge between:</p><ul><li><strong>Windows → WSL</strong></li><li><strong>WSL → Kind node (172.19.x.x)</strong></li></ul><p>The simplest, most stable way to do this is using <strong>socat</strong>.</p><h4>Bridge Windows → Kind Using Socat (Most Stable Method)</h4><h4>Step 1. Install socat inside WSL2</h4><pre>sudo apt update<br>sudo apt install socat</pre><h4>Step 2. Identify your Kind node IP</h4><pre>kubectl get nodes -o wide</pre><p>You will see something like:</p><pre>INTERNAL-IP: 172.19.0.2</pre><p>That is the cluster IP we must forward traffic to.</p><h4>Step 3. Run socat to forward Windows traffic → Kind</h4><p>Run this inside WSL2 (Ubuntu terminal):</p><pre>socat TCP-LISTEN:30080,bind=0.0.0.0,fork TCP:172.19.0.2:30080</pre><p>Or run socat in the background</p><p>If you want it to stay running even after terminal closes:</p><pre>nohup socat TCP-LISTEN:30080,bind=0.0.0.0,fork TCP:172.19.0.2:30080 &gt; /dev/null 2&gt;&amp;1 &amp;</pre><p>just like a production-grade domain.</p><p>This creates a live TCP bridge:</p><pre>WSL port 30080  →  Kind node port 30080</pre><h4>Step 4. Add host entry in Windows</h4><p>Open:</p><pre>C:\Windows\System32\drivers\etc\hosts</pre><p>Add:</p><pre>172.31.28.31    percyblog.local</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w0rUP1Zpg610NFjpxIlShg.png" /></figure><p>(Replace with your actual WSL IP from: wsl hostname -I)</p><p>Flush DNS:</p><pre>ipconfig /flushdns</pre><h4>Step 5. Lets access our app from Windows</h4><p>Open:</p><pre>http://percyblog.local:30080</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qg154shw40Ysw_Pup4bGzg.png" /></figure><h3>Section 7 — Enabling Autoscaling on Kind (HPA + Metrics Server + Fixed RBAC)</h3><p>Autoscaling is one of Kubernetes’ most important real-world features.<br> However, <strong>Kind does NOT ship with Metrics Server</strong>, and the official manifest <strong>fails</strong> inside Kind due to:</p><ul><li>Docker/Kind network addressing</li><li>strict TLS verification</li><li>missing RBAC privileges</li></ul><p>In this section, we will:</p><ol><li>Install Metrics Server</li><li>Patch Metrics Server for Kind networking</li><li>Fix all RBAC permissions</li><li>Verify CPU &amp; memory metrics</li><li>Create an HPA for the percyblog-app Deployment</li><li>Generate load and watch autoscaling work</li><li>Watch pods scale down again</li></ol><h4>Step 1 — Install Metrics Server (Official Manifest)</h4><p>Apply the official Metrics Server deployment:</p><pre>kubectl apply -f \<br>  https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml</pre><p>This installs:</p><ul><li>APIService v1beta1.metrics.k8s.io</li><li>Deployment metrics-server</li><li>ServiceAccount</li><li>RBAC roles</li><li>Service</li></ul><p><strong>BUT it will crash inside Kind</strong> without patching.</p><h4>Step 2 — Patch Metrics Server for Kind (Fix TLS + Certificates)</h4><p>Metrics Server fails in Kind because the kubelet is accessed on internal Docker IPs like:</p><pre>172.19.0.2</pre><p>Those IPs are <strong>not in the kubelet certificate SAN</strong>, resulting in:</p><p>connection refused<br>certificate errors<br>CrashLoopBackOff</p><p>To fix this, we must:</p><ul><li>allow insecure TLS</li><li>mount a writable /tmp directory</li><li>force Metrics Server to use InternalIP</li><li>regenerate certificates cleanly</li></ul><p>I’ll apply this patch:</p><pre>kubectl patch deployment metrics-server -n kube-system --type=&#39;json&#39; -p=&#39;[<br>  {<br>    &quot;op&quot;: &quot;add&quot;,<br>    &quot;path&quot;: &quot;/spec/template/spec/containers/0/args&quot;,<br>    &quot;value&quot;: [<br>      &quot;--cert-dir=/tmp&quot;,<br>      &quot;--secure-port=4443&quot;,<br>      &quot;--kubelet-insecure-tls&quot;,<br>      &quot;--kubelet-use-node-status-port&quot;,<br>      &quot;--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname&quot;,<br>      &quot;--metric-resolution=15s&quot;<br>    ]<br>  },<br>  {<br>    &quot;op&quot;: &quot;add&quot;,<br>    &quot;path&quot;: &quot;/spec/template/spec/volumes&quot;,<br>    &quot;value&quot;: [<br>      { &quot;name&quot;: &quot;tmp-dir&quot;, &quot;emptyDir&quot;: {} }<br>    ]<br>  },<br>  {<br>    &quot;op&quot;: &quot;add&quot;,<br>    &quot;path&quot;: &quot;/spec/template/spec/containers/0/volumeMounts&quot;,<br>    &quot;value&quot;: [<br>      { &quot;name&quot;: &quot;tmp-dir&quot;, &quot;mountPath&quot;: &quot;/tmp&quot; }<br>    ]<br>  }<br>]&#39;</pre><p>Restart Metrics Server:</p><pre>kubectl delete pod -n kube-system -l k8s-app=metrics-server</pre><h4>Step 3 — Fix RBAC (The Critical Part We Just Solved)</h4><p>Metrics Server still cannot retrieve metrics without additional RBAC:</p><h4>1 — Allowing Metrics Server to read authentication config</h4><pre>kubectl apply -f - &lt;&lt;EOF<br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: ClusterRole<br>metadata:<br>  name: system:metrics-server-auth-reader<br>rules:<br>- apiGroups: [&quot;&quot;]<br>  resources: [&quot;configmaps&quot;]<br>  resourceNames: [&quot;extension-apiserver-authentication&quot;]<br>  verbs: [&quot;get&quot;, &quot;list&quot;]<br>---<br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: RoleBinding<br>metadata:<br>  name: metrics-server-auth-reader<br>  namespace: kube-system<br>roleRef:<br>  apiGroup: rbac.authorization.k8s.io<br>  kind: ClusterRole<br>  name: system:metrics-server-auth-reader<br>subjects:<br>- kind: ServiceAccount<br>  name: metrics-server<br>  namespace: kube-system<br>EOF</pre><h4>2 — Allowing Metrics Server to perform SubjectAccessReviews</h4><pre>kubectl apply -f - &lt;&lt;EOF<br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: ClusterRole<br>metadata:<br>  name: metrics-server-subjectaccessreviews<br>rules:<br>- apiGroups: [&quot;authorization.k8s.io&quot;]<br>  resources: [&quot;subjectaccessreviews&quot;]<br>  verbs: [&quot;create&quot;]<br>EOF</pre><pre>kubectl apply -f - &lt;&lt;EOF<br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: ClusterRoleBinding<br>metadata:<br>  name: metrics-server-subjectaccessreviews<br>roleRef:<br>  apiGroup: rbac.authorization.k8s.io<br>  kind: ClusterRole<br>  name: metrics-server-subjectaccessreviews<br>subjects:<br>- kind: ServiceAccount<br>  name: metrics-server<br>  namespace: kube-system<br>EOF</pre><h4>3 — Re-applying main Metrics Server ClusterRole Binding</h4><pre>kubectl apply -f - &lt;&lt;EOF<br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: ClusterRoleBinding<br>metadata:<br>  name: system:metrics-server<br>roleRef:<br>  apiGroup: rbac.authorization.k8s.io<br>  kind: ClusterRole<br>  name: system:metrics-server<br>subjects:<br>- kind: ServiceAccount<br>  name: metrics-server<br>  namespace: kube-system<br>EOF</pre><p>Restart again:</p><pre>kubectl delete pod -n kube-system -l k8s-app=metrics-server</pre><h4>Step 4 — Verifying Metrics Server Is Working</h4><h4>Check node metrics:</h4><pre>kubectl top nodes</pre><p>You should see output like:</p><pre>NAME                 CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%<br>kind-control-plane   245m         6%     1065Mi          27%</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*Tvp2ZEWXCPlP1Twzt-Mt7Q.png" /></figure><h4>Check pod metrics:</h4><pre>kubectl top pods -A</pre><p>Example:</p><pre>percyblog-app   34m   103Mi<br>metrics-server  4m    15Mi</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*lyWB2RWJJCXgl5hKbXLdQg.png" /></figure><p>Metrics Server is FULLY working now.</p><h4>Step 5 — Create the HPA for the PercyBlog App</h4><p>Deployment name:</p><pre>percyblog-app<br>namespace: percyblog</pre><p>Create an HPA that scales from <strong>1 to 5 pods</strong> at <strong>50% CPU usage</strong>:</p><pre>kubectl autoscale deployment percyblog-app \<br>  --cpu-percent=50 \<br>  --min=1 \<br>  --max=5 \<br>  -n percyblog</pre><p>Check HPA:</p><pre>kubectl get hpa -n percyblog</pre><p>Example:</p><pre>NAME            REFERENCE                  TARGETS        MIN   MAX   REPLICAS   AGE<br>percyblog-app   Deployment/percyblog-app   cpu: 10%/50%   1     5     1          30s</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/779/1*KQ_9_e5MF-QgT1GX8jL0dQ.png" /></figure><h4>Step 6 — Generate Load (Trigger Autoscaling)</h4><p>Inside WSL2, run:</p><pre>while true; do<br>  curl -H &quot;Host: percyblog.local&quot; http://localhost:30080 &gt; /dev/null<br>done</pre><p>Check HPA again after 20–40 seconds:</p><pre>kubectl get hpa -n percyblog</pre><p>You should see CPU rising.</p><p>Then watch scaling:</p><pre>kubectl get deploy percyblog-app -n percyblog</pre><p>Example:</p><pre>NAME            READY   UP-TO-DATE   AVAILABLE   AGE<br>percyblog-app  3/3     3            3           10m</pre><p>Pods scaling live:</p><pre>kubectl get pods -n percyblog -w</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*7qteWaTVy05JjemlHJuVyw.png" /></figure><h4>Step 7 — Scale Down Automatically</h4><p>Stop load with <strong>CTRL + C</strong>.</p><p>After ~5 minutes:</p><ul><li>CPU falls below 50%</li><li>HPA reduces replicas back to 1</li></ul><h3>Conclusion</h3><p>At the end of this entire setup, what we’ve basically done is build a full, production-style Kubernetes environment completely inside WSL2, where Kind runs the actual cluster, NGINX Ingress handles all external routing, Windows connects to the cluster through a socat bridge, custom hostnames like percyblog.local resolve exactly the way they would in a real cloud environment, Metrics Server is fully fixed and reporting CPU and memory usage, RBAC is properly configured so the API can authenticate and authorize requests, Horizontal Pod Autoscaling works exactly like it does on GKE, EKS, or AKS, and we can literally generate load, watch our pods automatically scale up, stop the load and watch them scale back down — meaning the whole environment now behaves like a real-world Kubernetes cluster but running entirely on our laptop, with everything hooked together from application deployment to ingress traffic, metrics, monitoring, autoscaling and full Windows-to-WSL-to-Kind connectivity all functioning exactly the way a real DevOps pipeline would expect.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9977e340e3e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>