# adrianojiu/cloudfront_add_ranges_security_group_auto_lambda architecture

> Architecture diagram of the GitHub repository adrianojiu/cloudfront_add_ranges_security_group_auto_lambda, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/adrianojiu/cloudfront_add_ranges_security_group_auto_lambda
- Repository: https://github.com/adrianojiu/cloudfront_add_ranges_security_group_auto_lambda

## Overview

##### Project Type and Purpose
- This is a serverless, event-driven utility that automatically updates a Security Group with AWS CloudFront IP ranges.
- It uses AWS services (SNS, Lambda, EC2 APIs, S3) and Infrastructure-as-Code (CloudFormation) to deploy and manage resources.

##### Main Components
a. AWS Services
- SNS Topic: AmazonIpSpaceChanged (publishes IP range change events)
- Lambda Function: Python 3.9 handler (sg-cloudfront-global-edge-ips-rule-lambda.py)
- IAM Role & Policy: Grants Lambda permissions to describe/update security groups
- EC2 Security Group: Holds CloudFront IP rules (updated by Lambda)
- S3 Bucket: Hosts sg_lambda.zip code bundle
- CloudFormation: Template (cloudformation_create_lambda_function.json) to provision Lambda, IAM, SNS subscription
b. Optional Manual Invocation
- AWS CLI: `aws lambda invoke` to manually trigger the Lambda

##### Relationships and Data Flows
- CloudFormation → creates IAM Role, Lambda Function, SNS Subscription
- S3 Bucket (sg_lambda.zip) → Lambda function code at deployment
- SNS Topic → invokes Lambda whenever AWS publishes new IP ranges
- Lambda Function → uses IAM Role to call EC2 APIs → adds/removes rules on the Security Group
- (Optional) AWS CLI → calls Lambda handler endpoint directly

##### Architectural Patterns and Principles
- Event-Driven Architecture: SNS → Lambda on IP-change events
- Serverless / FaaS: No long-running servers, pay per execution
- Infrastructure as Code: All AWS resources can be deployed via CloudFormation
- Idempotent Operations: Lambda checks for existing rules before creation

- Inside, list the three resources it creates: IAM Role, Lambda Function (with link to S3), SNS Subscription

##### Final Touches
- Component has a clear label.
- Arrowheads indicating flow direction.
- Related services into logical zones (e.g., “AWS Control Plane”, “Serverless Runtime”, “Networking”).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "AWS Control Plane"
        CF["CloudFormation Template"]:::service
    end

    subgraph "Deployed Resources"
        IAM["IAM Role & Policy"]:::iam
        Lambda["Update-SG-Lambda\n(Python 3.9)"]:::compute
        Sub["SNS Subscription"]:::service
    end

    S3["S3 Bucket\n(sg_lambda.zip)"]:::service
    SNS["SNS Topic\n(AmazonIpSpaceChanged)"]:::service
    EC2["EC2 Security Group"]:::security
    CLI["AWS CLI"]:::external
    Docs["Project Docs\nREADME.md"]:::external
    Lic["License\nLICENSE"]:::external

    CF -->|creates IAM Role| IAM
    CF -->|creates Lambda Function| Lambda
    CF -->|creates SNS Subscription| Sub
    CF -->|fetch sg_lambda.zip| S3

    SNS -->|publishes IP changes| Sub
    Sub -->|invokes| Lambda

    Lambda -->|assume role| IAM
    IAM -->|EC2 permissions| EC2
    Lambda -->|"Describe/Authorize/Revoke SG rules"| EC2

    CLI ---|manual invoke| Lambda

    subgraph "Documentation"
        Docs
        Lic
    end

    click CF "https://github.com/adrianojiu/cloudfront_add_ranges_security_group_auto_lambda/blob/main/cloudformation_create_lambda_function.json"
    click IAM "https://github.com/adrianojiu/cloudfront_add_ranges_security_group_auto_lambda/blob/main/iam_policy_role_sample.json"
    click Lambda "https://github.com/adrianojiu/cloudfront_add_ranges_security_group_auto_lambda/blob/main/sg-cloudfront-global-edge-ips-rule-lambda.py"
    click S3 "https://github.com/adrianojiu/cloudfront_add_ranges_security_group_auto_lambda/blob/main/sg_lambda.zip"
    click Docs "https://github.com/adrianojiu/cloudfront_add_ranges_security_group_auto_lambda/blob/main/README.md"
    click Lic "https://github.com/adrianojiu/cloudfront_add_ranges_security_group_auto_lambda/tree/main/LICENSE"

    classDef service fill:#add8e6,stroke:#333,stroke-width:1px
    classDef compute fill:#90ee90,stroke:#333,stroke-width:1px
    classDef iam fill:#ffcc99,stroke:#333,stroke-width:1px
    classDef security fill:#ff9999,stroke:#333,stroke-width:1px
    classDef external fill:#d3d3d3,stroke:#333,stroke-width:1px
```
