# afsar-0007/aws-costoptimization architecture

> Architecture diagram of the GitHub repository afsar-0007/aws-costoptimization, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/afsar-0007/aws-costoptimization
- Repository: https://github.com/afsar-0007/aws-costoptimization

## Overview

This repository automates cleanup of orphaned EBS snapshots: a schedule invokes Lambda, which checks snapshots against their volumes, deletes orphaned snapshots, logs activity, and can notify by email. Terraform defines the AWS infrastructure, including monitoring and access control. The Lambda behavior is supported by source; infrastructure relationships are based on the README because the Terraform files were not sampled.

Main entry points: [`terraform/lambda/lambda_function.py`](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/lambda/lambda_function.py) (Cleanup Lambda), [`terraform/main.tf`](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/main.tf) (Terraform infrastructure), [`terraform/sns.tf`](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/sns.tf) (SNS notifications), [`terraform/eventbridge.tf`](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/eventbridge.tf) (EventBridge schedule)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cleanup["Snapshot cleanup"]
  node_lambda["Cleanup Lambda<br/>[lambda_function.py]"]
  node_snapshot_check["Snapshot discovery<br/>[lambda_function.py]"]
  node_volume_check["Volume existence check<br/>[lambda_function.py]"]
  node_snapshot_delete["Orphan deletion<br/>[lambda_function.py]"]
end

subgraph group_aws["AWS services"]
  node_scheduler["EventBridge schedule<br/>[eventbridge.tf]"]
  node_ec2["EC2 / EBS"]
  node_sns["SNS notifications<br/>[sns.tf]"]
  node_email(("Email subscriber"))
end

subgraph group_ops["Infrastructure and operations"]
  node_logs[("CloudWatch logs<br/>[cloudwatch.tf]")]
  node_alarm["Throttling alarm<br/>[cloudwatch.tf]"]
  node_iam["IAM permissions<br/>[iam.tf]"]
  node_terraform["Terraform infrastructure<br/>[main.tf]"]
end

subgraph group_state["Terraform state"]
  node_state[("Remote state<br/>[backend.tf]")]
end

node_scheduler -->|"triggers"| node_lambda
node_lambda -->|"runs"| node_snapshot_check
node_snapshot_check -->|"lists snapshots"| node_ec2
node_lambda -->|"checks volumes"| node_volume_check
node_volume_check -->|"describes volumes"| node_ec2
node_lambda -->|"deletes orphans"| node_snapshot_delete
node_snapshot_delete -->|"deletes snapshots"| node_ec2
node_lambda -->|"writes activity"| node_logs
node_lambda -->|"publishes report"| node_sns
node_sns -->|"delivers email"| node_email
node_alarm -->|"sends alert"| node_sns
node_iam -->|"grants permissions"| node_lambda
node_terraform -->|"provisions"| node_scheduler
node_terraform -->|"provisions"| node_lambda
node_terraform -->|"configures monitoring"| node_logs
node_terraform -->|"provisions"| node_sns
node_terraform -->|"configures access"| node_iam
node_terraform -->|"stores state"| node_state

click node_scheduler "https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/eventbridge.tf"
click node_lambda "https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/lambda/lambda_function.py"
click node_snapshot_check "https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/lambda/lambda_function.py"
click node_volume_check "https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/lambda/lambda_function.py"
click node_snapshot_delete "https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/lambda/lambda_function.py"
click node_logs "https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/cloudwatch.tf"
click node_alarm "https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/cloudwatch.tf"
click node_sns "https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/sns.tf"
click node_iam "https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/iam.tf"
click node_terraform "https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/main.tf"
click node_state "https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/backend.tf"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_lambda,node_snapshot_check,node_volume_check,node_snapshot_delete toneBlue
class node_scheduler,node_ec2,node_sns,node_email toneAmber
class node_logs,node_alarm,node_iam,node_terraform toneMint
class node_state toneRose
```

## Components

### Snapshot cleanup

- [Cleanup Lambda](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/lambda/lambda_function.py): `terraform/lambda/lambda_function.py`
- [Snapshot discovery](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/lambda/lambda_function.py): `terraform/lambda/lambda_function.py`
- [Volume existence check](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/lambda/lambda_function.py): `terraform/lambda/lambda_function.py`
- [Orphan deletion](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/lambda/lambda_function.py): `terraform/lambda/lambda_function.py`

### AWS services

- [EventBridge schedule](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/eventbridge.tf): `terraform/eventbridge.tf`
- **EC2 / EBS**
- [SNS notifications](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/sns.tf): `terraform/sns.tf`
- **Email subscriber**

### Infrastructure and operations

- [CloudWatch logs](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/cloudwatch.tf): `terraform/cloudwatch.tf`
- [Throttling alarm](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/cloudwatch.tf): `terraform/cloudwatch.tf`
- [IAM permissions](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/iam.tf): `terraform/iam.tf`
- [Terraform infrastructure](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/main.tf): `terraform/main.tf`

### Terraform state

- [Remote state](https://github.com/afsar-0007/aws-costoptimization/blob/main/terraform/backend.tf): `terraform/backend.tf`

## Connections

- EventBridge schedule → Cleanup Lambda: triggers (evidence: `README.md`)
- Cleanup Lambda → Snapshot discovery: runs (evidence: `terraform/lambda/lambda_function.py`)
- Snapshot discovery → EC2 / EBS: lists snapshots (evidence: `terraform/lambda/lambda_function.py`)
- Cleanup Lambda → Volume existence check: checks volumes (evidence: `terraform/lambda/lambda_function.py`)
- Volume existence check → EC2 / EBS: describes volumes (evidence: `terraform/lambda/lambda_function.py`)
- Cleanup Lambda → Orphan deletion: deletes orphans (evidence: `terraform/lambda/lambda_function.py`)
- Orphan deletion → EC2 / EBS: deletes snapshots (evidence: `terraform/lambda/lambda_function.py`)
- Cleanup Lambda → CloudWatch logs: writes activity (evidence: `README.md`)
- Cleanup Lambda → SNS notifications: publishes report (evidence: `terraform/lambda/lambda_function.py`)
- SNS notifications → Email subscriber: delivers email (evidence: `README.md`)
- Throttling alarm → SNS notifications: sends alert (evidence: `README.md`)
- IAM permissions → Cleanup Lambda: grants permissions (evidence: `README.md`)
- Terraform infrastructure → EventBridge schedule: provisions (evidence: `README.md`)
- Terraform infrastructure → Cleanup Lambda: provisions (evidence: `README.md`)
- Terraform infrastructure → CloudWatch logs: configures monitoring (evidence: `README.md`)
- Terraform infrastructure → SNS notifications: provisions (evidence: `README.md`)
- Terraform infrastructure → IAM permissions: configures access (evidence: `README.md`)
- Terraform infrastructure → Remote state: stores state (evidence: `README.md`)
