<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Susseta Bose</title>
    <description>The latest articles on DEV Community by Susseta Bose (@susseta).</description>
    <link>https://dev.to/susseta</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3578839%2F96a443c2-8d75-4e75-803b-ed1801b7838a.PNG</url>
      <title>DEV Community: Susseta Bose</title>
      <link>https://dev.to/susseta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/susseta"/>
    <language>en</language>
    <item>
      <title>Automated SSH Configuration Drift Remediation on AWS Using Ansible, SSM, and Terraform</title>
      <dc:creator>Susseta Bose</dc:creator>
      <pubDate>Mon, 16 Nov 2026 05:01:16 +0000</pubDate>
      <link>https://dev.to/aws-builders/automated-ssh-configuration-drift-remediation-on-aws-using-ansible-ssm-and-terraform-2aa2</link>
      <guid>https://dev.to/aws-builders/automated-ssh-configuration-drift-remediation-on-aws-using-ansible-ssm-and-terraform-2aa2</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Why SSH Configuration Drift Is a Real Security Risk&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Security in cloud environments is not a one-time setup. It is a continuous process. One of the most overlooked but dangerous problems in managing a fleet of EC2 instances is &lt;strong&gt;configuration drift&lt;/strong&gt; — the gradual deviation of a system's actual state from its desired, secure baseline.&lt;/p&gt;

&lt;p&gt;SSH (Secure Shell) is the primary remote access protocol for Linux-based EC2 instances. The &lt;code&gt;/etc/ssh/sshd_config&lt;/code&gt; file controls how the SSH daemon behaves on a server. A single misconfigured directive in this file can open the door to serious security vulnerabilities. For example, &lt;code&gt;PermitRootLogin yes&lt;/code&gt; allows direct root access over SSH, which means an attacker who obtains valid credentials — or exploits a brute-force vulnerability — can gain full administrative control of the server without any additional privilege escalation.&lt;/p&gt;

&lt;p&gt;This is not a theoretical risk. CIS (Center for Internet Security) Benchmarks, PCI-DSS, HIPAA, and SOC 2 compliance frameworks all explicitly require that root login over SSH be disabled. Failing this check during an audit can result in compliance violations, financial penalties, and reputational damage.&lt;/p&gt;

&lt;p&gt;The challenge is that in a dynamic cloud environment, configuration drift happens constantly. A developer might temporarily enable root login to debug an issue and forget to revert it. A poorly written bootstrap script might overwrite the SSH config. An OS update might reset certain directives to their defaults. Whatever the cause, the result is the same your instances are no longer in the secure state you intended.&lt;/p&gt;

&lt;p&gt;Manual audits — where someone periodically SSHes into each server and checks the config — simply do not scale. If you have 50, 100, or 500 instances, manual checks are impractical, inconsistent, and slow. What you need is an automated system that continuously monitors every instance, detects drift the moment it occurs, and remediates it without any human intervention.&lt;/p&gt;

&lt;p&gt;This blog presents exactly such a system — a fully automated, event-driven SSH configuration drift detection and remediation pipeline built entirely on AWS-native services, with Ansible handling the actual remediation logic, Terraform managing all infrastructure as code, and AWS CodeBuild providing the CI/CD backbone.&lt;/p&gt;




&lt;h2&gt;
  
  
  Architecture Overview
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────────────────────────────────────────────────────┐
│                          AWS Account (us-east-1)                            │
│                                                                             │
│  ┌──────────────┐    Weekly Cron     ┌─────────────────────────────────┐   │
│  │  EC2 Fleet   │◄───────────────────│   SSM State Manager Association  │   │
│  │ (All Managed │   (Every Sunday    │   (AWS-RunShellScript)           │   │
│  │  Instances)  │    at 02:00 UTC)   │   Checks: PermitRootLogin no     │   │
│  └──────┬───────┘                   └──────────────┬──────────────────┘   │
│         │                                          │                        │
│         │ exit 1 (Non-Compliant)                   │ Association Status     │
│         │                                          │ = "Failed"             │
│         │                                          ▼                        │
│         │                           ┌──────────────────────────┐           │
│         │                           │   Amazon EventBridge     │           │
│         │                           │   Rule                   │           │
│         │                           │   source: aws.ssm        │           │
│         │                           │   status: Failed         │           │
│         │                           └──────────┬───────────────┘           │
│         │                                      │ Triggers                  │
│         │                                      ▼                           │
│         │                           ┌──────────────────────────┐           │
│         │                           │   SSM Automation         │           │
│         │                           │   Document               │           │
│         │                           │   (Custom: ssh-permit    │           │
│         │                           │    rootlogin-remediation)│           │
│         │                           └──────────┬───────────────┘           │
│         │                                      │ aws:runCommand            │
│         │                                      │ (AWS-RunShellScript)      │
│         │◄─────────────────────────────────────┘                           │
│         │  ansible-playbook /tmp/ssh_config.yml                            │
│         │                                      ▲                           │
│         │                                      │ Downloads playbook        │
│         │                           ┌──────────┴───────────────┐           │
│         │                           │   Amazon S3 Bucket       │           │
│         │                           │   ansible-playbook-*     │           │
│         │                           │   └── ssh_config.yml     │           │
│         │                           └──────────────────────────┘           │
│                                                                             │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                        CI/CD Pipeline                               │   │
│  │  CodeCommit/GitHub → CodeBuild (buildspec.yml) → Terraform Apply   │   │
│  │                    → CodeBuild (destroybuildspec.yml) → TF Destroy  │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsfcehp0fmq9295zzsah7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsfcehp0fmq9295zzsah7.png" alt=" " width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The pipeline involves six core AWS services working in concert: &lt;strong&gt;SSM State Manager&lt;/strong&gt;, &lt;strong&gt;Amazon EventBridge&lt;/strong&gt;, &lt;strong&gt;SSM Automation&lt;/strong&gt;, &lt;strong&gt;Amazon S3&lt;/strong&gt;, &lt;strong&gt;IAM&lt;/strong&gt;, and &lt;strong&gt;AWS CodeBuild&lt;/strong&gt;. Ansible provides the remediation logic, and Terraform provisions every resource. Let's walk through each component in detail.&lt;/p&gt;




&lt;h2&gt;
  
  
  Component 1:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;SSM State Manager — The Compliance Scanner&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;AWS Systems Manager (SSM) State Manager is a configuration management service that ensures your EC2 instances maintain a defined state over time. Unlike a one-time script, a State Manager Association is a persistent, scheduled policy that continuously re-applies or re-checks a configuration.&lt;/p&gt;

&lt;p&gt;In this solution, a State Manager Association is created that targets &lt;strong&gt;all SSM-managed instances&lt;/strong&gt; in the account using the wildcard &lt;code&gt;InstanceIds: ["*"]&lt;/code&gt;. This is a powerful targeting mechanism — any new instance that gets the SSM Agent installed and registers with Systems Manager will automatically be included in the compliance check without any manual intervention.&lt;/p&gt;

&lt;p&gt;The association runs on a &lt;strong&gt;weekly cron schedule&lt;/strong&gt; — every Sunday at 02:00 UTC (&lt;code&gt;cron(0 2 ? * SUN *)&lt;/code&gt;). This timing is intentional: it runs during a low-traffic window to minimize any potential performance impact on production workloads.&lt;/p&gt;

&lt;p&gt;The compliance check itself is a simple but effective shell script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; grep -q &lt;span class="s2"&gt;"^PermitRootLogin no"&lt;/span&gt; /etc/ssh/sshd_config&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Compliant"&lt;/span&gt;
  &lt;span class="nb"&gt;exit&lt;/span&gt; 0
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Non-Compliant"&lt;/span&gt;
  &lt;span class="nb"&gt;exit&lt;/span&gt; 1
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The script uses &lt;code&gt;grep&lt;/code&gt; with the &lt;code&gt;^&lt;/code&gt; anchor to check for the exact directive &lt;code&gt;PermitRootLogin no&lt;/code&gt; at the beginning of a line in &lt;code&gt;/etc/ssh/sshd_config&lt;/code&gt;. The &lt;code&gt;^&lt;/code&gt; anchor is important — it prevents false positives from commented-out lines like &lt;code&gt;# PermitRootLogin no&lt;/code&gt;. If the directive is found, the script exits with code &lt;code&gt;0&lt;/code&gt;, which SSM interprets as a &lt;strong&gt;Success&lt;/strong&gt;. If it is not found — meaning root login is either enabled, commented out, or the directive is missing entirely — the script exits with code &lt;code&gt;1&lt;/code&gt;, which SSM interprets as a &lt;strong&gt;Failed&lt;/strong&gt; status.&lt;/p&gt;

&lt;p&gt;The association is configured with &lt;code&gt;compliance_severity = "HIGH"&lt;/code&gt;. This means that when an instance fails the check, it appears as a HIGH severity finding in the &lt;strong&gt;SSM Compliance dashboard&lt;/strong&gt;, giving security teams immediate visibility into which instances are non-compliant, even before the automated remediation kicks in.&lt;/p&gt;




&lt;h2&gt;
  
  
  Component 2:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Amazon EventBridge — The Event-Driven Trigger&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Amazon EventBridge is a serverless event bus that enables you to build event-driven architectures by routing events from AWS services to target actions. In this solution, EventBridge acts as the bridge between the compliance check result and the remediation action.&lt;/p&gt;

&lt;p&gt;When an SSM State Manager Association run completes — whether it succeeds or fails — SSM automatically publishes an event to the AWS default event bus. The event looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"source"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"aws.ssm"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"detail-type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"EC2 State Manager Instance Association State Change"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"detail"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Failed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"instance-id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"i-0abc123def456789"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"association-id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"abc12345-..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"document-name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"AWS-RunShellScript"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An EventBridge rule is configured to match exactly this pattern — specifically filtering for events where &lt;code&gt;source&lt;/code&gt; is &lt;code&gt;aws.ssm&lt;/code&gt;, &lt;code&gt;detail-type&lt;/code&gt; is &lt;code&gt;EC2 State Manager Instance Association State Change&lt;/code&gt;, and &lt;code&gt;detail.status&lt;/code&gt; is &lt;code&gt;Failed&lt;/code&gt;. This precision filtering ensures that the remediation pipeline is only triggered when a compliance check actually fails — not for every SSM event in the account.&lt;/p&gt;

&lt;p&gt;One of the most important features used here is the &lt;strong&gt;input transformer&lt;/strong&gt;. The raw SSM event payload does not match the parameter schema expected by the SSM Automation document. The input transformer solves this by extracting the &lt;code&gt;instance-id&lt;/code&gt; from the event payload using a JSONPath expression (&lt;code&gt;$.detail.instance-id&lt;/code&gt;) and mapping it to the &lt;code&gt;InstanceId&lt;/code&gt; parameter that the Automation document expects. It also injects the &lt;code&gt;PlaybookName&lt;/code&gt; parameter statically, pointing to the Ansible playbook stored in S3.&lt;/p&gt;

&lt;p&gt;This approach is elegant because it eliminates the need for a Lambda function as a middleware layer. In many similar architectures, a Lambda function is used to transform the event payload and invoke the next step. Here, EventBridge's native input transformer handles that transformation directly, reducing operational overhead, cost, and the number of moving parts in the system.&lt;/p&gt;




&lt;h2&gt;
  
  
  Component 3:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;SSM Automation Document — The Remediation Orchestrator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The SSM Automation document is the central orchestrator of the remediation workflow. It is a YAML-formatted document that defines a sequence of steps to execute when triggered. In this solution, a custom Automation document named &lt;code&gt;ssh-permitrootlogin-remediation&lt;/code&gt; is created.&lt;/p&gt;

&lt;p&gt;The document is of type &lt;code&gt;Automation&lt;/code&gt; and uses schema version &lt;code&gt;0.3&lt;/code&gt;. It defines two input parameters: &lt;code&gt;InstanceId&lt;/code&gt; (the ID of the non-compliant instance, injected by EventBridge) and &lt;code&gt;PlaybookName&lt;/code&gt; (the name of the Ansible playbook to run, also injected by EventBridge).&lt;/p&gt;

&lt;p&gt;The document has a single step named &lt;code&gt;RunPlaybook&lt;/code&gt; that uses the &lt;code&gt;aws:runCommand&lt;/code&gt; action. This action invokes the &lt;code&gt;AWS-RunShellScript&lt;/code&gt; SSM document on the target instance, executing two shell commands in sequence:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;aws s3 cp s3://&amp;lt;bucket&amp;gt;/Playbook/&amp;lt;PlaybookName&amp;gt;.yml /tmp/&lt;/code&gt; — This downloads the Ansible playbook from the S3 bucket to the &lt;code&gt;/tmp/&lt;/code&gt; directory on the target instance. The instance must have an IAM instance profile with &lt;code&gt;s3:GetObject&lt;/code&gt; permission on the bucket for this to work.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;ansible-playbook /tmp/&amp;lt;PlaybookName&amp;gt;.yml&lt;/code&gt; — This executes the downloaded playbook using the Ansible CLI installed on the instance.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The document assumes an IAM role called &lt;code&gt;CustomSSMAutomationRole&lt;/code&gt; (referenced via a Terraform &lt;code&gt;data&lt;/code&gt; source). This role must have the necessary permissions to invoke &lt;code&gt;ssm:SendCommand&lt;/code&gt; on EC2 instances. Using a &lt;code&gt;data&lt;/code&gt; source rather than creating the role in Terraform is a deliberate design choice — it assumes the role is managed separately (perhaps by a central security team), keeping the blast radius of this Terraform module small and focused.&lt;/p&gt;

&lt;p&gt;The parameterized design of the Automation document is a key architectural strength. Because &lt;code&gt;PlaybookName&lt;/code&gt; is a runtime parameter rather than a hardcoded value, the same document can be reused to run any Ansible playbook stored in the S3 bucket. This means you can extend the remediation system to cover other security configurations — password policies, audit logging, file permissions — simply by uploading new playbooks and creating new EventBridge rules, without modifying the Automation document at all.&lt;/p&gt;




&lt;h2&gt;
  
  
  Component 4:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Amazon S3 — The Playbook Repository&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Amazon S3 serves as the centralized, durable storage for Ansible playbooks. The S3 bucket (&lt;code&gt;ansible-playbook-837439744&lt;/code&gt;) is created by Terraform, and the playbook (&lt;code&gt;ssh_config.yml&lt;/code&gt;) is uploaded as an S3 object.&lt;/p&gt;

&lt;p&gt;A particularly smart detail in the Terraform configuration is the use of &lt;code&gt;etag = filemd5("../Ansible/ssh_config.yml")&lt;/code&gt; on the S3 object resource. The &lt;code&gt;etag&lt;/code&gt; attribute stores an MD5 hash of the local file. Every time &lt;code&gt;terraform apply&lt;/code&gt; is run, Terraform computes the current MD5 hash of the local playbook file and compares it to the stored &lt;code&gt;etag&lt;/code&gt;. If the file has changed, Terraform detects the difference and re-uploads the file to S3. If the file is unchanged, Terraform skips the upload.&lt;/p&gt;

&lt;p&gt;This means that updating the remediation logic is as simple as editing the Ansible playbook locally and running &lt;code&gt;terraform apply&lt;/code&gt; — or pushing to the repository and letting CodeBuild handle it. There is no manual S3 upload step, no separate deployment process for the playbook. The infrastructure and the remediation logic are deployed together as a single atomic unit.&lt;/p&gt;




&lt;h2&gt;
  
  
  Component 5:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The Ansible Playbook — Idempotent Remediation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Ansible playbook (&lt;code&gt;ssh_config.yml&lt;/code&gt;) is the actual remediation logic. It is intentionally simple and focused:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="nn"&gt;---
&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Remediate SSH Configuration Drift&lt;/span&gt;
  &lt;span class="na"&gt;hosts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;localhost&lt;/span&gt;
  &lt;span class="na"&gt;become&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;yes&lt;/span&gt;
  &lt;span class="na"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Disable root login&lt;/span&gt;
      &lt;span class="na"&gt;lineinfile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/etc/ssh/sshd_config&lt;/span&gt;
        &lt;span class="na"&gt;regexp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'^PermitRootLogin'&lt;/span&gt;
        &lt;span class="na"&gt;line&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'PermitRootLogin no'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The playbook targets &lt;code&gt;localhost&lt;/code&gt; because SSM Run Command executes the script directly on the target instance — there is no Ansible control node involved. The &lt;code&gt;become: yes&lt;/code&gt; directive ensures the task runs with root privileges, which is required to modify &lt;code&gt;/etc/ssh/sshd_config&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;lineinfile&lt;/code&gt; module is the right tool for this job for several reasons. First, it is &lt;strong&gt;idempotent&lt;/strong&gt; — running it multiple times produces the same result as running it once. If &lt;code&gt;PermitRootLogin no&lt;/code&gt; is already present, the module does nothing. If &lt;code&gt;PermitRootLogin yes&lt;/code&gt; is present, it replaces it. If no &lt;code&gt;PermitRootLogin&lt;/code&gt; directive exists at all, it inserts &lt;code&gt;PermitRootLogin no&lt;/code&gt;. This idempotency is critical in an automated remediation system — you never want a remediation action to cause unintended side effects if it runs more than once.&lt;/p&gt;

&lt;p&gt;Second, &lt;code&gt;lineinfile&lt;/code&gt; is &lt;strong&gt;surgical&lt;/strong&gt; — it modifies only the specific line matching the &lt;code&gt;regexp&lt;/code&gt; pattern, leaving the rest of the SSH configuration file untouched. This is far safer than replacing the entire &lt;code&gt;sshd_config&lt;/code&gt; file with a template, which could inadvertently overwrite instance-specific customizations.&lt;/p&gt;

&lt;p&gt;Third, the &lt;code&gt;regexp: '^PermitRootLogin'&lt;/code&gt; pattern matches any existing &lt;code&gt;PermitRootLogin&lt;/code&gt; directive regardless of its current value, ensuring the line is always updated to the desired state.&lt;/p&gt;




&lt;h2&gt;
  
  
  Component 6:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;IAM — Least-Privilege Security&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Two IAM resources are created in this solution, each following the principle of least privilege.&lt;/p&gt;

&lt;p&gt;The first is the &lt;strong&gt;EventBridge IAM role&lt;/strong&gt; (&lt;code&gt;eventbridge-ssm-role&lt;/code&gt;). This role is assumed by the &lt;code&gt;events.amazonaws.com&lt;/code&gt; service principal, meaning only EventBridge can assume it. The role's inline policy grants only &lt;code&gt;ssm:SendCommand&lt;/code&gt; — the minimum permission needed for EventBridge to invoke the SSM Automation document. In a production hardening step, the &lt;code&gt;Resource&lt;/code&gt; in this policy should be scoped to the specific SSM document ARN rather than &lt;code&gt;"*"&lt;/code&gt;, further limiting the blast radius if the role were ever misused.&lt;/p&gt;

&lt;p&gt;The second is the &lt;strong&gt;CustomSSMAutomationRole&lt;/strong&gt;, which is referenced via a Terraform &lt;code&gt;data&lt;/code&gt; source rather than created by this module. This role is assumed by the SSM Automation document and needs permissions to run commands on EC2 instances via &lt;code&gt;ssm:SendCommand&lt;/code&gt;. Managing this role outside the module is a good practice — it allows a central security team to control what the automation can do, while the application team manages the rest of the infrastructure.&lt;/p&gt;




&lt;h2&gt;
  
  
  Component 7:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Terraform Infrastructure as Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every AWS resource in this solution is defined in Terraform, organized across focused, single-responsibility files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;provider.tf&lt;/code&gt; — Configures the AWS provider (pinned to version &lt;code&gt;6.54.0&lt;/code&gt; for reproducibility) and the S3 remote backend for state management. Remote state in S3 enables team collaboration and prevents state file conflicts.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;variable.tf&lt;/code&gt; — Declares all input variables (&lt;code&gt;bucket_name&lt;/code&gt;, &lt;code&gt;object_name&lt;/code&gt;, &lt;code&gt;ssm_doc&lt;/code&gt;, &lt;code&gt;event_rule_name&lt;/code&gt;), making the module configurable and reusable across environments.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;terraform.tfvars&lt;/code&gt; — Provides concrete values for all variables, keeping configuration separate from logic.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s3.tf&lt;/code&gt; — Creates the S3 bucket and uploads the Ansible playbook with MD5-based change detection.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;stateassociations.tf&lt;/code&gt; — Creates the SSM State Manager Association that runs the weekly compliance check.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ssmdocument.tf&lt;/code&gt; — Defines the custom SSM Automation document that orchestrates remediation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;eventbridge.tf&lt;/code&gt; — Creates the EventBridge rule and target with the input transformer.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;iam.tf&lt;/code&gt; — Creates the EventBridge IAM role and its inline policy.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;data.tf&lt;/code&gt; — Fetches the pre-existing &lt;code&gt;CustomSSMAutomationRole&lt;/code&gt; by name.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This file-per-resource-type organization makes the codebase easy to navigate, review, and extend. Each file has a clear, single responsibility, and dependencies between resources are expressed through Terraform's &lt;code&gt;depends_on&lt;/code&gt; and implicit reference mechanisms.&lt;/p&gt;




&lt;h2&gt;
  
  
  Component 8:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;CI/CD with AWS CodeBuild&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The project includes two CodeBuild buildspec files that implement a complete infrastructure lifecycle pipeline.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;buildspec.yml&lt;/code&gt; handles the &lt;strong&gt;deploy&lt;/strong&gt; workflow. It installs Terraform &lt;code&gt;1.8.5&lt;/code&gt; from HashiCorp's official release, initializes the backend, validates the configuration syntax, generates a plan saved as &lt;code&gt;tfplan&lt;/code&gt;, and then applies that exact plan. The separation of &lt;code&gt;plan&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; into &lt;code&gt;pre_build&lt;/code&gt; and &lt;code&gt;build&lt;/code&gt; phases is intentional — it ensures that what was reviewed and approved in the plan phase is exactly what gets deployed, with no possibility of drift between the two steps.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;destroybuildspec.yml&lt;/code&gt; handles the &lt;strong&gt;teardown&lt;/strong&gt; workflow. It follows the same initialization steps but generates a destroy plan (&lt;code&gt;terraform plan -destroy&lt;/code&gt;) and applies it. Having a dedicated, version-controlled destroy pipeline is a best practice that is often overlooked. It enables clean teardown of ephemeral environments, prevents orphaned resources from accumulating costs, and ensures the destroy process is as auditable and repeatable as the deploy process.&lt;/p&gt;

&lt;p&gt;Both buildspec files cache the &lt;code&gt;.terraform/&lt;/code&gt; directory, which contains the downloaded provider plugins. This significantly speeds up subsequent builds by avoiding repeated downloads of the AWS provider binary.&lt;/p&gt;




&lt;h2&gt;
  
  
  End-to-End Flow:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Putting It All Together&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To understand how all these components work together, let's trace a complete remediation event from start to finish.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Every Sunday at 02:00 UTC, the SSM State Manager Association triggers and runs the compliance check shell script on every managed EC2 instance in the account simultaneously.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On a particular instance (&lt;code&gt;i-0abc123def456789&lt;/code&gt;), the script finds that &lt;code&gt;PermitRootLogin&lt;/code&gt; is set to &lt;code&gt;yes&lt;/code&gt; (or is missing entirely). The script exits with code &lt;code&gt;1&lt;/code&gt;. SSM records the association run as &lt;strong&gt;Failed&lt;/strong&gt; for that instance and updates the SSM Compliance dashboard with a HIGH severity finding.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SSM publishes an &lt;code&gt;EC2 State Manager Instance Association State Change&lt;/code&gt; event to the EventBridge default event bus, with &lt;code&gt;status: Failed&lt;/code&gt; and &lt;code&gt;instance-id: i-0abc123def456789&lt;/code&gt; in the event detail.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The EventBridge rule matches this event. The input transformer extracts &lt;code&gt;i-0abc123def456789&lt;/code&gt; as &lt;code&gt;instanceId&lt;/code&gt; and constructs the parameter payload &lt;code&gt;{ "InstanceId": ["i-0abc123def456789"], "PlaybookName": ["ssh_config.yml"] }&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;EventBridge invokes the SSM Automation document &lt;code&gt;ssh-permitrootlogin-remediation&lt;/code&gt; with this parameter payload, using the &lt;code&gt;eventbridge-ssm-role&lt;/code&gt; IAM role.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Automation document's &lt;code&gt;RunPlaybook&lt;/code&gt; step executes &lt;code&gt;aws:runCommand&lt;/code&gt; on instance &lt;code&gt;i-0abc123def456789&lt;/code&gt;, running two shell commands: first downloading &lt;code&gt;ssh_config.yml&lt;/code&gt; from S3 to &lt;code&gt;/tmp/&lt;/code&gt;, then running &lt;code&gt;ansible-playbook /tmp/ssh_config.yml&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ansible's &lt;code&gt;lineinfile&lt;/code&gt; module finds the &lt;code&gt;PermitRootLogin yes&lt;/code&gt; line in &lt;code&gt;/etc/ssh/sshd_config&lt;/code&gt; and replaces it with &lt;code&gt;PermitRootLogin no&lt;/code&gt;. The SSH daemon configuration is now compliant.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The following Sunday, the compliance check runs again on the same instance. This time, &lt;code&gt;grep&lt;/code&gt; finds &lt;code&gt;PermitRootLogin no&lt;/code&gt; and the script exits with code &lt;code&gt;0&lt;/code&gt;. The association run is recorded as &lt;strong&gt;Success&lt;/strong&gt;. The HIGH severity finding is cleared from the SSM Compliance dashboard.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The entire remediation cycle — from detection to fix — happens automatically, without any human intervention, typically within minutes of the compliance check completing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkyte2wp42563babxt4ch.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkyte2wp42563babxt4ch.png" alt=" " width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkwzeq5et02mooh0m64td.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkwzeq5et02mooh0m64td.png" alt=" " width="800" height="179"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Design Decisions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;No Lambda function.&lt;/strong&gt; Many event-driven architectures on AWS use Lambda as a glue layer between services. Here, EventBridge's native input transformer handles the payload transformation, and SSM Automation handles the orchestration. Eliminating Lambda reduces cost, operational overhead, cold start latency, and the number of components to monitor and maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ansible over raw shell scripts.&lt;/strong&gt; The remediation could have been implemented as a raw shell script embedded in the SSM document. Ansible was chosen because it provides idempotency guarantees, a declarative syntax that is easier to read and audit, and a rich module library that makes it easy to extend the playbook to cover additional SSH directives or other configuration files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Playbook in S3, not embedded in the SSM document.&lt;/strong&gt; Embedding the playbook content directly in the SSM Automation document would tightly couple the remediation logic to the infrastructure definition. Storing it in S3 decouples them — you can update the playbook without modifying or redeploying the Terraform infrastructure. The &lt;code&gt;etag = filemd5(...)&lt;/code&gt; trick in Terraform ensures the S3 object stays in sync with the local file automatically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parameterized Automation document.&lt;/strong&gt; The &lt;code&gt;PlaybookName&lt;/code&gt; parameter makes the Automation document a generic Ansible runner, not a single-purpose SSH remediation tool. This is a forward-looking design — as your compliance requirements grow, you can add new playbooks to S3 and new EventBridge rules without touching the core automation infrastructure.&lt;/p&gt;




&lt;h2&gt;
  
  
  Prerequisites and Deployment
&lt;/h2&gt;

&lt;p&gt;Before deploying, ensure the following are in place on all target EC2 instances:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SSM Agent installed and running (included by default on Amazon Linux 2, Amazon Linux 2023, and Ubuntu 20.04+).&lt;/li&gt;
&lt;li&gt;Ansible installed (&lt;code&gt;sudo pip install ansible&lt;/code&gt; or via the OS package manager).&lt;/li&gt;
&lt;li&gt;An IAM instance profile attached to each instance with &lt;code&gt;s3:GetObject&lt;/code&gt; permission on the playbook bucket and &lt;code&gt;ssm:*&lt;/code&gt; permissions for SSM Agent communication.&lt;/li&gt;
&lt;li&gt;A pre-existing IAM role named &lt;code&gt;CustomSSMAutomationRole&lt;/code&gt; with &lt;code&gt;ssm:SendCommand&lt;/code&gt; permissions.&lt;/li&gt;
&lt;li&gt;An S3 bucket for Terraform remote state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Deployment is straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;your-repo-url&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; sshconfig_remediation_systemmanager/Terraform
terraform init
terraform plan
terraform apply
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or push to your connected repository and let the CodeBuild pipeline handle it automatically.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This solution demonstrates that you do not need complex tooling or a large number of services to build a robust, production-grade security remediation pipeline on AWS. By thoughtfully combining SSM State Manager, EventBridge, SSM Automation, S3, Ansible, and Terraform, you get a system that is automated, scalable, auditable, and extensible.&lt;/p&gt;

&lt;p&gt;The compliance check runs on a schedule across your entire fleet. Failures are detected immediately and trigger automated remediation without human intervention. Every component is defined as code, version-controlled, and deployable through a CI/CD pipeline. The remediation logic is decoupled from the infrastructure, making it easy to update. And the architecture is generic enough to extend to any OS-level configuration that needs to stay compliant — not just SSH.&lt;/p&gt;

&lt;p&gt;In a world where security misconfigurations are one of the leading causes of cloud breaches, having an automated system that continuously enforces your security baseline is not a luxury. It is a necessity.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>devops</category>
      <category>security</category>
      <category>terraform</category>
    </item>
    <item>
      <title>Auto-Orphan-Volume-Cleanup-Automation</title>
      <dc:creator>Susseta Bose</dc:creator>
      <pubDate>Sun, 28 Dec 2025 16:36:40 +0000</pubDate>
      <link>https://dev.to/susseta/auto-orphan-volume-cleanup-automation-33ej</link>
      <guid>https://dev.to/susseta/auto-orphan-volume-cleanup-automation-33ej</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;:&lt;br&gt;
In modern cloud environments, unused resources often accumulate silently, driving up costs and creating operational inefficiencies. One of the most common culprits is orphaned EBS volumes that remain unattached and unnoticed after workloads are terminated. This project, was initiated to address that challenge by introducing an automated, secure, and auditable workflow for managing and cleaning up unused volumes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Statement:&lt;/strong&gt;&lt;br&gt;
In dynamic cloud environments, unused resources often accumulate unnoticed. One of the most common examples is orphaned Amazon EBS volumes left behind after instances are terminated. These unused volumes not only increase storage costs but also pose governance and compliance challenges. For example, a 100 GB General Purpose SSD (gp3) volume costs about $8 per month, while a 500 GB Provisioned IOPS SSD (io2) volume with 20,000 IOPS can exceed $1,250 per month. Snapshots add further hidden expenses at $0.05 per GB-month. When multiplied across multiple accounts and regions, these orphaned volumes can silently drive up bills by hundreds or even thousands of dollars monthly. Manual cleanup is error-prone and time-consuming, especially at scale. The need is clear: an automated and secure solution to manage the EBS volume lifecycle and eliminate unnecessary costs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Current Workflow State:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Traditionally, teams rely on manual scripts or periodic audits to identify unused volumes. This approach suffers from:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Lack of visibility across accounts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No centralized approval mechanism.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Risk of accidental deletion of critical data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;High operational overhead&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Target Workflow State&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The goal was to design a fully automated system that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Discovers unused EBS volumes regularly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provides a centralized approval interface before deletion.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ensures secure, auditable cleanup with minimal human intervention.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Integrates seamlessly with existing CI/CD pipelines.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Architecture Diagram&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8aqz72tcj0i4i00hs2zf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8aqz72tcj0i4i00hs2zf.png" alt=" " width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What AWS Services We Used:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;AWS Lambda – Discovery and deletion of EBS volumes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Amazon DynamoDB – Metadata and approval status storage&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Amazon ECS + ALB – Hosting the Streamlit approval application&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Amazon EventBridge – Scheduling automated discovery jobs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Amazon ECR – Container image repository for the web app&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;AWS IAM &amp;amp; KMS – Security and encryption&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Amazon CloudWatch – Logging and monitoring&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Explaination Of Entire System Workflow:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Volume Discovery Lambda runs on a schedule via EventBridge, scanning for unused EBS volumes and storing volume details in DynamoDB. Additionally, it exports the volume details and upload to S3 bucket, which lambda function use to send an email notification to end user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Streamlit Web App (deployed on ECS) provides a user-friendly interface to review discovered volumes. Approver can mark volumes as Approved in “column” for deletion. Once it’s saved in the application, it makes change in dynamodb table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Delete Volume Lambda gets triggered once the  change is detected in “DeleteConfirmation” in dynamodb table due to enablement of dynamodb stream feature. This lambda function identifies all the approved volumes and delete each one of them one by one.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CI/CD Pipeline ensures infrastructure and application updates are deployed consistently using GitLab and Terraform.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn1rmnjrjx8ys2dmp2rhr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn1rmnjrjx8ys2dmp2rhr.png" alt=" " width="800" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How Did We Implement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Infrastructure as Code: Terraform provisions all AWS resources including Lambdas, DynamoDB, ECS, ALB, and EventBridge rules.&lt;/p&gt;

&lt;p&gt;Containerization: The Streamlit app is packaged with Docker and pushed to ECR.&lt;/p&gt;

&lt;p&gt;Automation: GitLab CI/CD pipeline builds images, pushes them to ECR, and applies Terraform changes.&lt;/p&gt;

&lt;p&gt;Security: IAM roles follow least privilege principles, ECS logs are encrypted with KMS, and VPC security groups enforce isolation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:.
│   .gitlab-ci.yml
│   Dockerfile
│   README.md
│
├───DeleteVolumeFunction
│       lambda_function.py
│
├───StreamlitApplication
│       approval.py
│
├───Terraform
│       alb.tf
│       data.tf
│       dynamodb.tf
│       ecs.tf
│       eventbridge.tf
│       iam.tf
│       lambda.tf
│       output.tf
│       provider.tf
│       terraform.tfvars
│       variable.tf
│
└───VolumeDiscoveryFunction
        available_vol_discovery.py
        lambda_function.py
        push_to_dynamodb.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Lambda Scripts&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;VolumeDiscoveryFunction/available_vol_discovery.py&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import boto3
import os
import sys
import pandas as pd
from datetime import datetime
#~~~~~~~~~~~~~ Create EC2 client and describe volumes ~~~~~~~~~~~~~#

client = boto3.client('ec2')
sns_client = boto3.client('sns')

#~~~~~~~~~~~~~ Define a blank variable to store ebs volume info ~~~~~~~~~~~~~#

volume_data = []

#~~~~~~~~~~~~~ Loop through the response and extract relevant information ~~~~~~~~~~~~~#
def vol_discovery():

    response = client.describe_volumes()
    for vol in response['Volumes']:
        if vol['State'] == 'available':
            Volume_ID = vol['VolumeId']
            Size = vol['Size']
            State = vol['State']
            Creation_time = vol['CreateTime']
            Creation_time = Creation_time.replace(tzinfo=None) if Creation_time.tzinfo is not None else None
            Creation_time = Creation_time.strftime("%Y-%m-%d %H:%M:%S")
            Vol_Type = vol['VolumeType']
            Disk_Type = [ tag['Value'] for tag in vol['Tags'] if tag['Key'] == 'Type'][0]
            Owner = [ tag['Value'] for tag in vol['Tags'] if tag['Key'] == 'Owner'][0]

            data = {
                "VolumeID": Volume_ID,
                "Size": Size,
                "State": State,
                "Created": Creation_time,
                "VolumeType": Vol_Type,
                "DiskType": Disk_Type,
                "Owner": Owner,
                "DeleteConfirmation": "Pending"
            }
            volume_data.append(data)

    #~~~~~~~~~~~~~ Create a Excel file with extracted volume information ~~~~~~~~~~~~~#
    time = datetime.now().strftime("%H%M%S")
    df = pd.DataFrame(volume_data)
    output_file = f'discovery_available_ebsvol-{time}.xlsx'
    df.to_excel(f'/tmp/{output_file}', index=False)

    #~~~~~~~~~~~~~ Upload the Excel file to S3 ~~~~~~~~~~~~~#

    s3 = boto3.client('s3')
    bucket_name = os.environ['BUCKET_NAME']
    file_path = f'/tmp/{output_file}'
    s3_object_key = f'OrphanEBSReport/{output_file}' # Desired object key in S3

    try:
        s3.upload_file(file_path, bucket_name, s3_object_key)
        s3_url = f"https://{bucket_name}.s3.amazonaws.com/{s3_object_key}"
        print(f"File uploaded to S3: {s3_url}")
    except Exception as e:
        print(f"Error uploading file to S3: {e}")
        exit()
    #~~~~~~~~~~~~~ Send an email with the Excel file as an attachment ~~~~~~~~~~~~~#

    snsarn = os.environ['SNS_ARN']
    body = f"Hi Team, \n\nPlease be informed that the following EBS volumes are in 'available' state and not attached to any EC2 instances. Kindly review the excel report from below link.\n\nLink: {s3_url}\n\nPlease click on http://ALB-External-364496655.us-east-1.elb.amazonaws.com to provide an approval.\n\nBest Regards,\nSystems Management Team."
    res = sns_client.publish(
        TopicArn = snsarn,
        Subject = f'Orphan EBS Volume Discovery Report',
        Message = str(body)
        )
    return volume_data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;VolumeDiscoveryFunction/lambda_function.py&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import boto3
import os
import sys
import available_vol_discovery
import push_to_dynamodb


def lambda_handler(event, context):
    #~~~~~~~~~~~~~ Call the volume discovery function ~~~~~~~~~~~~~#
    volume_data = available_vol_discovery.vol_discovery()
    #~~~~~~~~~~~~~~ Call the function to push data to DynamoDB ~~~~~~~~~~~~~#
    push_to_dynamodb.push_data_to_dynamodb(volume_data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;VolumeDiscoveryFunction/push_to_dynamodb.py&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import boto3
import os

def push_data_to_dynamodb(volume_data):
    dynamodb = boto3.resource("dynamodb")
    table_name = os.environ.get("TABLE_NAME")
    table = dynamodb.Table(table_name)
    for data in volume_data:
        table.put_item(Item=data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;StreamlitApplication/approval.py&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import streamlit as st
import boto3
import pandas as pd
from boto3.dynamodb.types import TypeDeserializer

db = boto3.client('dynamodb')

def list_tables():
    list_table = db.list_tables()
    list_table = tuple(tables for tables in list_table['TableNames'])
    return list_table

def get_columns_from_table(table_name):
    response = db.scan(TableName=table_name, Limit=100)
    columns = set()
    for item in response.get('Items', []):
        columns.update(item.keys())
    # Handle pagination if table is large
    while 'LastEvaluatedKey' in response:
        response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'], Limit=100)
        for item in response.get('Items', []):
            columns.update(item.keys())
    return tuple(columns)

def get_table_key_schema(table_name):
    response = db.describe_table(TableName=table_name)
    key_schema = response['Table']['KeySchema']
    attribute_definitions = {attr['AttributeName']: attr['AttributeType'] for attr in response['Table']['AttributeDefinitions']}
    return key_schema, attribute_definitions

def get_items(table_name, select_col_name, possible_val):
    deserializer = TypeDeserializer()
    response = db.scan(TableName=table_name, Limit=100)
    items = response['Items']
    clean_items = [{k: deserializer.deserialize(v) for k, v in item.items()} for item in items]
    df = pd.DataFrame(clean_items)
    return df


def streamlit_approval():
    # ---- Streamlit UI ----
    st.title("✨DynamoDB Table Viewer✨")
    st.sidebar.title("Filter Table options")
    table_name = st.sidebar.selectbox("Select Table", list_tables())
    select_col_name = st.sidebar.selectbox("Filter the Column", 'DeleteConfirmation' if table_name else [])
    possible_val = st.sidebar.selectbox("Select The Column Value", ("Pending")) if select_col_name == 'DeleteConfirmation'else []

    if st.sidebar.button("Get Data"):
        df = get_items(table_name, select_col_name, possible_val)
        st.session_state.df = df
        st.session_state.table_name = table_name

    if 'df' in st.session_state:
        st.write(f"### DynamoDB Table: `{st.session_state.table_name}`")
        df_filtered = st.session_state.df
        column_config = {}
        for col in df_filtered.columns:
            if col == "DeleteConfirmation":
                column_config[col] = st.column_config.SelectboxColumn(
                    "DeleteConfirmation",
                    options=["Pending", "Approved"],
                    help="Change delete status"
                )
            else:
                column_config[col] = st.column_config.TextColumn(
                    label=col,
                    disabled=True
                )
        edited_df = st.data_editor(df_filtered, column_config=column_config, use_container_width=True, key="only_delete_editable", num_rows="fixed")

        if st.button("Save Changes"):
            key_schema, attribute_definitions = get_table_key_schema(st.session_state.table_name)
            for index, row in edited_df.iterrows():
                if row['DeleteConfirmation'] == 'Approved':
                    # Build the key based on table schema
                    key = {}
                    for key_attr in key_schema:
                        attr_name = key_attr['AttributeName']
                        attr_type = attribute_definitions[attr_name]
                        key_value = row[attr_name]
                        key[attr_name] = {attr_type: str(key_value)}

                    db.update_item(
                        TableName=st.session_state.table_name,
                        Key=key,
                        UpdateExpression='SET DeleteConfirmation = :val',
                        ExpressionAttributeValues={':val': {'S': 'Approved'}}
                    )
            st.success("Changes saved successfully!")

streamlit_approval()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;DeleteVolumeFunction/lambda_function.py&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import boto3
import os
import json

def lambda_handler(event, context):
    for ev in event:
        if ev['dynamodb']['NewImage']['DeleteConfirmation']['S'] == 'Approved' and ev['dynamodb']['OldImage']['DeleteConfirmation']['S'] == 'Pending':
            volume_id = ev['dynamodb']['OldImage']['VolumeID']['S']
            region = ev['awsRegion']
            ec2 = boto3.client('ec2', region_name=region)
            try:
                ec2.delete_volume(VolumeId=volume_id)
                print(f"Successfully deleted volume: {volume_id}")
                ## Delete the item of that volume id from DynamoDB
                dynamodb = boto3.resource('dynamodb', region_name=region)
                table_name = os.environ['DYNAMODB_TABLE_NAME']
                table = dynamodb.Table(table_name)
                table.delete_item(
                    Key={
                        'VolumeId': volume_id
                    }
                )

            except Exception as e:
                print(f"Error deleting volume {volume_id}: {str(e)}")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Terraform Codes:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Terraform/alb.tf&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_lb_target_group" "this_tg" {
  name     = var.TG_conf["name"]
  port     = var.TG_conf["port"]
  protocol = var.TG_conf["protocol"]
  vpc_id   = data.aws_vpc.this_vpc.id
  health_check {
    enabled           = var.TG_conf["enabled"]
    healthy_threshold = var.TG_conf["healthy_threshold"]
    interval          = var.TG_conf["interval"]
    path              = var.TG_conf["path"]
  }
  target_type = var.TG_conf["target_type"]
  tags = {
    Attached_ALB_dns = aws_lb.this_alb.dns_name
  }
}


resource "aws_lb" "this_alb" {
  name               = var.ALB_conf["name"]
  load_balancer_type = var.ALB_conf["load_balancer_type"]
  ip_address_type    = var.ALB_conf["ip_address_type"]
  internal           = var.ALB_conf["internal"]
  security_groups    = [data.aws_security_group.ext_alb.id]
  subnets            = [data.aws_subnet.web_subnet_1a.id, data.aws_subnet.web_subnet_1b.id]
  tags               = merge(var.alb_tags)
}

resource "aws_lb_listener" "this_alb_lis" {
  for_each          = var.Listener_conf
  load_balancer_arn = aws_lb.this_alb.arn
  port              = each.value["port"]
  protocol          = each.value["protocol"]
  default_action {
    type             = each.value["type"]
    target_group_arn = aws_lb_target_group.this_tg.arn
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terraform/data.tf&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# vpc details :

data "aws_vpc" "this_vpc" {
  state = "available"
  filter {
    name   = "tag:Name"
    values = ["custom-vpc"]
  }
}
# subnets details :

data "aws_subnet" "web_subnet_1a" {
  vpc_id = data.aws_vpc.this_vpc.id
  filter {
    name   = "tag:Name"
    values = ["weblayer-pub1-1a"]
  }
}

data "aws_subnet" "web_subnet_1b" {
  vpc_id = data.aws_vpc.this_vpc.id
  filter {
    name   = "tag:Name"
    values = ["weblayer-pub2-1b"]
  }
}

# ALB security group details :
data "aws_security_group" "ext_alb" {
  filter {
    name   = "tag:Name"
    values = ["ALBSG"]
  }
}

data "aws_security_group" "streamlit_app" {
  filter {
    name   = "tag:Name"
    values = ["StreamlitAppSG"]
  }
}

# Lambda execution role
data "aws_iam_role" "lambda_role" {
  name = var.lambda_role
}

# sns topic details
data "aws_sns_topic" "sns_topic_info" {
  name = var.sns
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terraform/dynamodb.tf&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_dynamodb_table" "dynamodb-table" {
  name             = var.dynamodb_table
  billing_mode     = "PAY_PER_REQUEST"
  hash_key         = "VolumeID"
  stream_enabled   = true
  stream_view_type = "NEW_AND_OLD_IMAGES"
  attribute {
    name = "VolumeID"
    type = "S" # String type
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terraform/ecs.tf&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~AWS ECR Repository~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

resource "aws_ecr_repository" "aws-ecr" {
  name = var.ecr_repo
  tags = var.ecr_tags
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~AWS ECS Cluster~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

resource "aws_ecs_cluster" "aws-ecs-cluster" {
  name = var.ecs_details["Name"]
  configuration {
    execute_command_configuration {
      kms_key_id = aws_kms_key.kms.arn
      logging    = var.ecs_details["logging"]
      log_configuration {
        cloud_watch_encryption_enabled = true
        cloud_watch_log_group_name     = aws_cloudwatch_log_group.log-group.name
      }
    }
  }
  tags = var.custom_tags
}

resource "aws_ecs_task_definition" "taskdef" {
  family = var.ecs_task_def["family"]
  container_definitions = jsonencode([
    {
      "name" : "${var.ecs_task_def["cont_name"]}",
      "image" : "${aws_ecr_repository.aws-ecr.repository_url}:v1",
      "entrypoint" : [],
      "essential" : "${var.ecs_task_def["essential"]}",
      "logConfiguration" : {
        "logDriver" : "${var.ecs_task_def["logdriver"]}",
        "options" : {
          "awslogs-group" : "${aws_cloudwatch_log_group.log-group.id}",
          "awslogs-region" : "${var.region}",
          "awslogs-stream-prefix" : "app-prd"
        }
      },
      "portMappings" : [
        {
          "containerPort" : "${var.ecs_task_def["containerport"]}",
        }
      ],
      "cpu" : "${var.ecs_task_def["cpu"]}",
      "memory" : "${var.ecs_task_def["memory"]}",
      "networkMode" : "${var.ecs_task_def["networkmode"]}"
    }
  ])

  requires_compatibilities = var.ecs_task_def["requires_compatibilities"]
  network_mode             = var.ecs_task_def["networkmode"]
  memory                   = var.ecs_task_def["memory"]
  cpu                      = var.ecs_task_def["cpu"]
  execution_role_arn       = aws_iam_role.ecsTaskExecutionRole.arn
  task_role_arn            = aws_iam_role.ecsTaskExecutionRole.arn

  tags = var.custom_tags
}



#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~AWS CloudWatch Log Group~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

resource "aws_cloudwatch_log_group" "log-group" {
  name = var.cw_log_grp
  tags = var.custom_tags
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~AWS KMS Key~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

resource "aws_kms_key" "kms" {
  description             = var.kms_key["description"]
  deletion_window_in_days = var.kms_key["deletion_window_in_days"]
  tags                    = var.custom_tags
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terraform/eventbridge.tf&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;



resource "aws_pipes_pipe" "event_pipe" {
  depends_on  = [data.aws_iam_role.lambda_role]
  name        = var.eventbridge_pipe
  description = "EventBridge Pipe to process DynamoDB Stream data to Lambda"
  role_arn    = data.aws_iam_role.lambda_role.arn
  source      = aws_dynamodb_table.dynamodb-table.stream_arn
  target      = aws_lambda_function.lambda_2.arn

  source_parameters {
    dynamodb_stream_parameters {
      starting_position = "LATEST"
    }

    filter_criteria {
      filter {
        pattern = jsonencode({
          dynamodb = {
            OldImage = {
              DeleteConfirmation = {
                S = ["Pending"]
              }
            },
            NewImage = {
              DeleteConfirmation = {
                S = ["Approved"]
              }
            }
          }
        })
      }
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terraform/iam.tf&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_iam_role" "ecsTaskExecutionRole" {
  name               = var.ecs_role
  assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
}

data "aws_iam_policy_document" "assume_role_policy" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["ecs-tasks.amazonaws.com"]
    }
  }
}

locals {
  policy_arn = [
    "arn:aws:iam::aws:policy/AdministratorAccess",
    "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role",
    "arn:aws:iam::669122243705:policy/CustomPolicyECS"
  ]
}
resource "aws_iam_role_policy_attachment" "ecsTaskExecutionRole_policy" {
  count      = length(local.policy_arn)
  role       = aws_iam_role.ecsTaskExecutionRole.name
  policy_arn = element(local.policy_arn, count.index)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terraform/lambda.tf&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Archive the Codespaces~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

data "archive_file" "lambda_zip_1" {
  type        = "zip"
  source_dir  = "${path.module}/../VolumeDiscoveryFunction"
  output_path = "${path.module}/../VolumeDiscoveryFunction/lambda.zip"
}

data "archive_file" "lambda_zip_2" {
  type        = "zip"
  source_dir  = "${path.module}/../DeleteVolumeFunction"
  output_path = "${path.module}/../DeleteVolumeFunction/lambda.zip"
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Lambda Functions~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;Lambda Func: VolumeDiscoveryFunction&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;#

resource "aws_lambda_function" "lambda_1" {
  filename         = data.archive_file.lambda_zip_1.output_path
  function_name    = var.lambda_function_1
  role             = data.aws_iam_role.lambda_role.arn
  handler          = "lambda_function.lambda_handler"
  source_code_hash = data.archive_file.lambda_zip_1.output_base64sha256

  runtime     = "python3.13"
  layers      = [var.lambda_layer_arn]
  timeout     = 60
  memory_size = 900
  ephemeral_storage {
    size = 1024
  }

  environment {
    variables = {
      BUCKET_NAME = var.s3_bucket_name
      SNS_ARN     = data.aws_sns_topic.sns_topic_info.arn
      TABLE_NAME  = var.dynamodb_table
    }
  }

}


#&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;Lambda Func: DeleteVolumeFunction&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;#

resource "aws_lambda_function" "lambda_2" {
  filename         = data.archive_file.lambda_zip_2.output_path
  function_name    = var.lambda_function_2
  role             = data.aws_iam_role.lambda_role.arn
  handler          = "lambda_function.lambda_handler"
  source_code_hash = data.archive_file.lambda_zip_2.output_base64sha256

  runtime     = "python3.13"
  timeout     = 60
  memory_size = 250
  environment {
    variables = {
      TABLE_NAME = var.dynamodb_table
    }
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terraform/output.tf&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~AWS ECR Repository~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
output "ecr_arn" {
  value = aws_ecr_repository.aws-ecr.arn
}

output "ecr_registry_id" {
  value = aws_ecr_repository.aws-ecr.registry_id
}

output "ecr_url" {
  value = aws_ecr_repository.aws-ecr.repository_url
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~AWS ALB~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
output "arn" {
  value = [aws_lb.this_alb.arn]
}

output "dns_name" {
  value = [aws_lb.this_alb.dns_name]
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~AWS ECS Cluster~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
output "ecs_arn" {
  value = aws_ecs_cluster.aws-ecs-cluster.id
}

output "cw_log_group_arn" {
  value = aws_cloudwatch_log_group.log-group.arn
}

output "kms_id" {
  value = aws_kms_key.kms.id
}

output "kms_arn" {
  value = aws_kms_key.kms.arn
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~AWS Lambda~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

output "lambda" {
  value = {
    lambda_1 = aws_lambda_function.lambda_1.arn
    lambda_2 = aws_lambda_function.lambda_2.arn
  }
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~AWS DynamoDB~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

output "dynamodb_table_name" {
  value = aws_dynamodb_table.dynamodb-table.arn
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terraform/provider.tf&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "6.17.0"
    }
    archive = {
      source  = "hashicorp/archive"
      version = "2.7.1"
    }
  }
}

terraform {
  backend "s3" {
    bucket = "terraform0806"
    key    = "TerraformStateFiles"
    region = "us-east-1"
  }
}


provider "aws" {
  # Configuration options
  region = "us-east-1"
}
provider "archive" {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terraform/terraform.tfvars&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Terraform/terraform.tfvars of ALB~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

TG_conf = {
  enabled           = true
  healthy_threshold = "2"
  interval          = "30"
  name              = "TargetGroup-External"
  port              = "8501"
  protocol          = "HTTP"
  target_type       = "ip"
  path              = "/"
}

ALB_conf = {
  internal           = false
  ip_address_type    = "ipv4"
  load_balancer_type = "application"
  name               = "ALB-External"
}

Listener_conf = {
  "1" = {
    port     = "80"
    priority = 100
    protocol = "HTTP"
    type     = "forward"
  }
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Terraform/terraform.tfvars of ECS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

ecs_details = {
  Name                           = "Streamlit-cluster"
  logging                        = "OVERRIDE"
  cloud_watch_encryption_enabled = true
}

ecs_task_def = {
  family                   = "custom-task-definition"
  cont_name                = "streamlit"
  cpu                      = 256
  memory                   = 512
  essential                = true
  logdriver                = "awslogs"
  containerport            = 8501
  networkmode              = "awsvpc"
  requires_compatibilities = ["FARGATE", ]
}


cw_log_grp = "cloudwatch-log-group-ecs-cluster"

kms_key = {
  description             = "log group encryption"
  deletion_window_in_days = 7
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Terraform/terraform.tfvars of Lambda~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

lambda_role       = "custom-lambda-role"
lambda_function_1 = "VolumeDiscoveryFunction-1"
lambda_function_2 = "DeleteVolumeFunction-1"
s3_bucket_name    = "terraform0806"
dynamodb_table    = "AvailableEBSVolume-1"
sns               = "SNSEmailNotification"
lambda_layer_arn  = "arn:aws:lambda:us-east-1:336392948345:layer:AWSSDKPandas-Python313:4"
eventbridge_pipe  = "Custom-Eventbridge-Pipe"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terraform/variable.tf&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Variables of ALB~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

variable "TG_conf" {
  type = object({
    name              = string
    port              = string
    protocol          = string
    target_type       = string
    enabled           = bool
    healthy_threshold = string
    interval          = string
    path              = string
  })
}

variable "ALB_conf" {
  type = object({
    name               = string
    internal           = bool
    load_balancer_type = string
    ip_address_type    = string
  })
}

variable "Listener_conf" {
  type = map(object({
    port     = string
    protocol = string
    type     = string
    priority = number
  }))
}

variable "alb_tags" {
  description = "provides the tags for ALB"
  type = object({
    Environment = string
    Email       = string
    Type        = string
    Owner       = string
  })
  default = {
    Email       = "dasanirban9019@gmail.com"
    Environment = "Dev"
    Owner       = "Anirban Das"
    Type        = "External"
  }
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Variables of ECR~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

variable "ecr_repo" {
  description = "Name of repository"
  default     = "streamlit-repo"
}

variable "ecr_tags" {
  type = map(any)
  default = {
    "AppName" = "StreamlitApp"
    "Env"     = "Dev"
  }
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Variables of ECS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

variable "region" {
  type    = string
  default = "us-east-1"
}

variable "ecs_role" {
  description = "ecs roles"
  default     = "ecsTaskExecutionRole"
}

variable "ecs_details" {
  description = "details of ECS cluster"
  type = object({
    Name                           = string
    logging                        = string
    cloud_watch_encryption_enabled = bool
  })
}

variable "ecs_task_def" {
  description = "defines the configurations of task definition"
  type = object({
    family                   = string
    cont_name                = string
    cpu                      = number
    memory                   = number
    essential                = bool
    logdriver                = string
    containerport            = number
    networkmode              = string
    requires_compatibilities = list(string)

  })
}


variable "cw_log_grp" {
  description = "defines the log group in cloudwatch"
  type        = string
  default     = ""
}

variable "kms_key" {
  description = "defines the kms key"
  type = object({
    description             = string
    deletion_window_in_days = number
  })
}

variable "custom_tags" {
  description = "defines common tags"
  type        = object({})
  default = {
    AppName = "StreamlitApp"
    Env     = "Dev"
  }
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Variables of Lambda~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

variable "lambda_role" {
  type = string
}

variable "lambda_function_1" {
  type = string
}

variable "lambda_function_2" {
  type = string
}

variable "s3_bucket_name" {
  type = string
}

variable "dynamodb_table" {
  type = string
}

variable "sns" {
  type = string
}

variable "lambda_layer_arn" {
  type = string
}

variable "eventbridge_pipe" {
  type = string
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Impact Analysis:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cost Savings: Automated cleanup reduces unnecessary storage costs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Operational Efficiency: Eliminates manual audits and cleanup scripts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Governance: Approval workflow ensures accountability and prevents accidental deletions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Scalability: Works seamlessly across multiple accounts and regions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Future Improvement Possibilities:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Extend support to unused snapshots and AMIs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add multi-account aggregation using AWS Organizations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Integrate with Slack or Teams notifications for approval requests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enhance the web app with role-based access control (RBAC).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implement machine learning-based recommendations for identifying safe-to-delete volumes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Auto AMI Cleanup project demonstrates how automation, infrastructure-as-code, and approval workflows can transform cloud resource management. By combining AWS services with a simple web interface, we achieved a secure, scalable, and cost-efficient solution to a common cloud challenge. This approach not only saves money but also strengthens governance and operational hygiene in AWS environments.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>automation</category>
      <category>devops</category>
    </item>
    <item>
      <title>Monthly Golden Image Build Process using Packer &amp; Ansible</title>
      <dc:creator>Susseta Bose</dc:creator>
      <pubDate>Tue, 23 Dec 2025 04:43:31 +0000</pubDate>
      <link>https://dev.to/susseta/monthly-golden-image-build-process-using-packer-ansible-191i</link>
      <guid>https://dev.to/susseta/monthly-golden-image-build-process-using-packer-ansible-191i</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;In IT operations,Imagine We're working in a cloud organization that deploys hundreds of EC2 instances every month. Each instance needs to be secure, compliant.Manually configuring each instance is a nightmare. Instead, you want a golden image — a reusable AMI that’s pre-hardened and provisioned with all necessary tools.&lt;/p&gt;

&lt;p&gt;This is the point at which Packer becomes relevant.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Statement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In most organizations, EC2 instances are launched frequently to support various workloads. But here's the catch — each instance needs to be:&lt;/p&gt;

&lt;p&gt;Secure and compliant&lt;/p&gt;

&lt;p&gt;Equipped with monitoring and security agents&lt;/p&gt;

&lt;p&gt;Consistently configured&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real Time Scenario:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose you’re part of a security-conscious enterprise. Every EC2 instance must:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Follow CIS benchmarks&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Have CrowdStrike and Qualys agents installed&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Instead of configuring each instance post-launch, you want to create a golden AMI that’s already hardened and provisioned. This image will serve as the base for all future deployments — saving time and ensuring consistency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tools Involved:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Packer&lt;/li&gt;
&lt;li&gt;AWS EC2&lt;/li&gt;
&lt;li&gt;Ansible&lt;/li&gt;
&lt;li&gt;Gitlab CI/CD&lt;/li&gt;
&lt;li&gt;Amazon SSM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Architecture Diagram:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyiwql5nlfclj0j1skzzf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyiwql5nlfclj0j1skzzf.png" alt=" " width="800" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This workflow automates the creation of a secure AMI by:&lt;/p&gt;

&lt;p&gt;Launching a temporary EC2 instance from a base image.&lt;/p&gt;

&lt;p&gt;Running provisioning scripts to:&lt;/p&gt;

&lt;p&gt;Apply OS hardening (CIS benchmarks, firewall rules, SSH configs).&lt;/p&gt;

&lt;p&gt;Creating a new AMI from the configured instance.&lt;/p&gt;

&lt;p&gt;Terminating the temporary instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation Steps :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step1: Install Packer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F42bflm2ao27pe95xeb3b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F42bflm2ao27pe95xeb3b.png" alt=" " width="583" height="99"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step2: Create Packer Template with Ansible Provisioning&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This Packer template automates the creation of a custom Amazon Machine Image (AMI) by launching a temporary EC2 instance in a specific AWS VPC and subnet, using a designated SSH key pair for secure access.&lt;/p&gt;

&lt;p&gt;Code block&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;packer {
  required_plugins {
    amazon = {
      version = "&amp;gt;= 1.2.8"
      source  = "github.com/hashicorp/amazon"
    }
    ansible = {
      version = "~&amp;gt; 1"
      source = "github.com/hashicorp/ansible"
    }  
  }
}
variable "ami_prefix" {
  type = string
  default = ""
}

variable "reference_image" {
  type = string
  default = ""
}

locals {
  timestamp = regex_replace(timestamp(), "[- TZ:]", "")
}

variable "privatekey"{
  type = string
  default = ""
}

source "amazon-ebs" "amazon_linux" {
  ami_name      = "${var.ami_prefix}-${local.timestamp}"
  instance_type = "t2.micro"
  region        = "ap-south-1"
  vpc_id = "vpc-07b2ce11f9b189f3b"
  subnet_id = "subnet-063ebc661edd9fb37"
  security_group_id = "sg-04e9ae673095b02e9"
  ssh_interface = "private_ip"
  associate_public_ip_address = true
  ssh_keypair_name = "runner_key"
  ssh_private_key_file = var.privatekey

  source_ami_filter {
    filters = {
      name                = "${var.reference_image}"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = [""]
  }
  ssh_username = "ec2-user"
}

build {
  name = "learn-packer"
  sources = [
    "source.amazon-ebs.amazon_linux"
  ]

  provisioner "shell" {
  inline = [
    "sleep 20",
    "echo '--- Running AMI pre-check ---'",
    "set -e",
    "sudo mkdir -p /usr/lib",
    "# Ensure SFTP subsystem path exists",
    "if [ ! -f /usr/lib/sftp-server ]; then",
    "  if [ -f /usr/libexec/openssh/sftp-server ]; then",
    "    sudo ln -s /usr/libexec/openssh/sftp-server /usr/lib/sftp-server",
    "    echo 'Linked /usr/libexec/openssh/sftp-server -&amp;gt; /usr/lib/sftp-server';",
    "  else",
    "    echo 'Warning: sftp-server not found, installing openssh-server...';",
    "    sudo yum install -y openssh-server || sudo apt-get install -y openssh-server;",
    "  fi;",
    "fi",

    "# Basic network sanity check",
    "sudo yum clean all || true",
    "sudo yum update -y || true",
    "echo '--- Pre-check complete ---'"
  ]
  }

  provisioner "shell" {
  inline = [
    "sudo mkdir -p /tmp/.ansible",
    "sudo chmod 777 /tmp/.ansible"
  ]
  }

  provisioner "ansible" {
    playbook_file = "./playbook/main.yml"
    use_proxy = false
    extra_arguments = ["--vault-password-file=/home/gitlab-runner/.vault_pass",
    "-e", "ansible_remote_tmp=/tmp/.ansible",
    "-e", "ansible_local_tmp=/tmp/.ansible",
    "-e", "ansible_scp_if_ssh=True",
    "-e", "ansible_python_interpreter=/usr/bin/python3",
    "-e", "ansible_ssh_transfer_method=scp"]
  }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ansible Main Block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- name: Create users and providing sudo access
  hosts: all
  become: true
  gather_facts: true
  vars_files:
    - ../vars/useradd.yml
    - ../vars/vault.yml
  roles:
    - ../roles/useradd
    - ../roles/sudo

- name: Set hostnames
  hosts: all
  become: true
  gather_facts: false
  vars_files:
    - ../vars/var.yml
  roles:
    - ../roles/hostnamectl

- name: Enable or Set miscellaneous services
  hosts: all
  gather_facts: false
  become: true
  roles:
     - ../roles/ssh
     - ../roles/login_banner
     - ../roles/services
     - ../roles/timezone
    # - ../roles/fs_integrity
    #  - ../roles/selinux
    #  - ../roles/firewalld
    #  - ../roles/log_management
     - ../roles/rsyslog
    #  - ../roles/cron
    #  - ../roles/journald

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ansible User Creation block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- hosts: all
  become: true
  gather_facts: true
  vars_files:
    - ../vars/useradd.yml
    - ../vars/vault.yml
  roles:
    - ../roles/useradd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Outcome:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This template builds a custom AMI by:&lt;/p&gt;

&lt;p&gt;Launching a VM in a specific VPC and subnet.&lt;/p&gt;

&lt;p&gt;Using a defined SSH key pair for access.&lt;/p&gt;

&lt;p&gt;Running shell scripts and Ansible to configure the instance.&lt;/p&gt;

&lt;p&gt;Saving the final image with a unique name for future use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; Now, I've also configured a CI/CD variable in GitLab to securely store the private key content used for SSH access in the Packer build.&lt;/p&gt;

&lt;p&gt;GitLab CI/CD Variable for Private Key&lt;br&gt;
In GitLab, CI/CD variables allow us to store sensitive data like passwords, tokens, or SSH keys securely.&lt;/p&gt;

&lt;p&gt;I've created a variable (e.g., PRIVATE_KEY) that contains the entire private key content (not just the path).&lt;/p&gt;

&lt;p&gt;This variable is injected into the pipeline at runtime, allowing tools like Packer to use it without hardcoding the key or exposing it in our repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;packer validate -var-file="ami.pkrvars.hcl" -var "privatekey=runner_key.pem" aws-linux.pkr.hcl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step3: Gitlab Pipeline Stages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this, GitLab CI/CD job automates the AMI creation process by:&lt;/p&gt;

&lt;p&gt;Securely injecting an SSH private key from a CI/CD variable.&lt;/p&gt;

&lt;p&gt;Validating and building a Packer template.&lt;/p&gt;

&lt;p&gt;Provisioning an EC2 instance in a specific VPC/subnet.&lt;/p&gt;

&lt;p&gt;Saving the final image for future use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default: 
    tags:
      - gitlab_runner

stages:
  - image_build

Image Build: 
  stage: image_build
  script: 
    - echo "$SSH_PRIVATE_KEY" &amp;gt; runner_key.pem
    - chmod 400 runner_key.pem
    - packer init .
    - echo "Validating packer template..."
    - packer validate -var-file="ami.pkrvars.hcl" -var "privatekey=runner_key.pem" aws-linux.pkr.hcl
    - echo "Building AMI..."
    - packer build -var-file="ami.pkrvars.hcl" -var "privatekey=runner_key.pem" aws-linux.pkr.hcl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt; In this article, we tackled a common challenge in cloud operations i.e., ensuring every EC2 instance is secure, compliant, and consistently configured — without manual intervention.&lt;/p&gt;

&lt;p&gt;By combining Packer, Ansible, and GitLab CI/CD, we built a fully automated pipeline that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Launches a temporary EC2 instance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Applies CIS hardening and installs security agents&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Saves a golden AMI for future use&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Secures credentials using GitLab CI/CD variables&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This approach not only boosts security and compliance but also saves hours of manual effort, reduces human error, and ensures every deployment starts from a trusted baseline.&lt;/p&gt;

&lt;p&gt;Thanks,&lt;br&gt;
Susseta Bose&lt;/p&gt;

</description>
      <category>automation</category>
      <category>aws</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
