Application ArchitectureΒΆ

Table of ContentsΒΆ

  1. Executive Summary

  2. System Context

  3. Component Architecture

  4. Key Architecture Decisions

  5. Security Architecture

  6. Deployment Architecture


Executive SummaryΒΆ

SG Provisioner is an enterprise-grade AWS Security Group provisioning and management tool built for cloud infrastructure teams. It automates the creation, validation, and lifecycle management of Security Groups using a scenario-based, configuration-driven approach backed by AWS CloudFormation.

Rather than requiring engineers to manually define individual firewall rules, SG Provisioner provides pre-built scenario templates for common application architectures (3-tier web, 2-tier web, database-specific variants). Engineers select a scenario, apply overrides for their specific requirements, and the tool generates a validated CloudFormation template that provisions all Security Groups with correct cross-tier references.

Key Capabilities:

  • 9 pre-built scenarios covering common 2-tier and 3-tier architectures

  • Override system for port changes and additional ingress/egress rules per tier

  • Workload discriminator for deploying multiple SG sets in the same environment

  • CloudFormation-based provisioning with automatic circular dependency resolution

  • Security validation blocking dangerous patterns (open DB ports, SSH/RDP from public)

  • Pre-deployment review reports with override highlighting

  • Post-deployment reports with real Security Group IDs

  • SSM Parameter Store integration for downstream resource discovery

  • AWS Marketplace license validation

  • Docker containerization with Cython-compiled core modules for IP protection


System ContextΒΆ

SG Provisioner is one of four provisioners in the mlops-infra-suite β€” a suite of infrastructure automation tools designed to work together to provision complete AWS environments for ML workloads.

Position in the SuiteΒΆ

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              mlops-infra-suite                β”‚
β”‚                                               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚ VPC Provisioner │───▢│ SG Provisioner  β”‚   β”‚
β”‚  β”‚                 β”‚    β”‚                 β”‚   β”‚
β”‚  β”‚ Provisions VPC, β”‚    β”‚ Provisions      β”‚   β”‚
β”‚  β”‚ subnets, NAT,   β”‚    β”‚ Security Groups β”‚   β”‚
β”‚  β”‚ route tables    β”‚    β”‚ per tier        β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                  β”‚            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚ S3 Provisioner  β”‚    β”‚ SEC Provisioner β”‚   β”‚
β”‚  β”‚                 β”‚    β”‚                 β”‚   β”‚
β”‚  β”‚ Provisions S3   β”‚    β”‚ Provisions IAM  β”‚   β”‚
β”‚  β”‚ buckets and     β”‚    β”‚ roles and       β”‚   β”‚
β”‚  β”‚ folder structureβ”‚    β”‚ policies        β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Dependency OrderΒΆ

SG Provisioner depends on the VPC Provisioner β€” it requires a VPC to exist before Security Groups can be created. The typical provisioning order is:

  1. VPC Provisioner β€” creates VPC, subnets, gateways, stores VPC ID in SSM

  2. SG Provisioner β€” reads VPC ID from SSM, creates Security Groups, stores SG IDs in SSM

  3. SEC Provisioner β€” creates IAM roles and policies

  4. S3 Provisioner β€” creates S3 buckets and folder structures

SSM Parameter Store as Integration BackboneΒΆ

All provisioners use AWS SSM Parameter Store as the integration backbone β€” each provisioner stores its outputs so downstream provisioners and application workloads can discover resources without hardcoding IDs:

/vpc/{vpc-name}/VPCId                          β†’ consumed by SG Provisioner
/sg/{sg-name}/{tier}/SecurityGroupId           β†’ consumed by EC2, RDS, ECS, Lambda, SageMaker
/s3/{bucket-name}/BucketName                   β†’ consumed by application workloads
/security/{stem}/StackId                       β†’ consumed by application workloads

External DependenciesΒΆ

Service

Purpose

AWS CloudFormation

Stack-based provisioning and lifecycle management

AWS EC2

Security Group resource creation

AWS SSM Parameter Store

VPC ID resolution (input) and SG ID storage (output)

AWS Marketplace

License validation

Component ArchitectureΒΆ

Module OverviewΒΆ

sg_provisioner/
β”œβ”€β”€ cli.py                          # CLI entry point, action dispatch
β”œβ”€β”€ __main__.py                     # Python module execution entry
β”‚
β”œβ”€β”€ config/
β”‚   β”œβ”€β”€ loader.py (.so)             # Configuration loading and validation
β”‚   └── app_config.yaml             # Application-level settings (dirs, timeouts)
β”‚
β”œβ”€β”€ core/
β”‚   └── sg_manager.py (.so)         # Core orchestration and AWS operations
β”‚
β”œβ”€β”€ generators/
β”‚   └── cfn_generator.py (.so)      # CloudFormation template generation
β”‚
β”œβ”€β”€ scenarios/
β”‚   β”œβ”€β”€ loader.py (.so)             # Scenario loading and override application
β”‚   └── validator.py (.so)          # Security rule validation
β”‚
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ scenario.py                 # Scenario, Tier data models
β”‚   └── security_group.py           # SecurityGroup, SecurityGroupRule data models
β”‚
β”œβ”€β”€ license/
β”‚   └── validator.py (.so)          # AWS Marketplace license validation
β”‚
└── utils/
    β”œβ”€β”€ html_generator.py (.so)     # Deployment HTML report generation
    └── review_report.py (.so)      # Pre-deployment review report generation

common/ (shared library)
β”œβ”€β”€ cli/
β”‚   └── base_cli.py                 # Base CLI with --config, --action, --force flags
β”œβ”€β”€ logger/
β”‚   └── base_logger.py              # Shared logging setup
β”œβ”€β”€ utils/
β”‚   β”œβ”€β”€ aws_helpers.py              # AWS credential verification and region validation
β”‚   β”œβ”€β”€ aws_retry.py                # Retry logic with exponential backoff
β”‚   β”œβ”€β”€ class_helpers.py            # Class utility functions
β”‚   β”œβ”€β”€ config_helpers.py           # YAML configuration parsing utilities
β”‚   β”œβ”€β”€ dry_run_helpers.py          # Dry-run mode decorator
β”‚   └── timing_helpers.py           # Performance timing decorators
└── validation/
    └── config_validator.py         # JSON schema validation engine

Module ResponsibilitiesΒΆ

cli.py β€” Entry Point

  • Validates AWS Marketplace license before any action

  • Parses CLI arguments (-con, -act, --force, --verbose)

  • Instantiates SgManager and dispatches the requested action

  • Handles top-level exceptions with user-friendly messages

config/loader.py β€” Configuration Management

  • Loads and parses the customer YAML configuration file

  • Validates against JSON schema (schemas/validation-schema.yaml)

  • Computes derived values: SG name, stack name, log file path, template path

  • Initialises AWS clients (EC2, CloudFormation, SSM)

core/sg_manager.py β€” Core Orchestration

  • Central orchestrator for all 12 CLI actions

  • Manages the full SG lifecycle: validate β†’ generate β†’ deploy β†’ monitor β†’ delete

  • Handles CloudFormation stack operations with waiter pattern for async completion

  • Stores and deletes SG IDs in SSM Parameter Store

  • Generates pre-deployment and post-deployment HTML reports

generators/cfn_generator.py β€” CloudFormation Template Generation

  • Builds the complete CloudFormation template dict from a Scenario object

  • Generates AWS::EC2::SecurityGroup resources per tier

  • Resolves cross-tier references as standalone AWS::EC2::SecurityGroupIngress / AWS::EC2::SecurityGroupEgress resources to avoid circular dependencies

  • Generates AWS::SSM::Parameter resources to store SG IDs at deployment time

  • Builds CloudFormation Outputs and Exports per tier

scenarios/loader.py β€” Scenario Management

  • Loads scenario YAML files from schemas/scenarios/

  • Builds Scenario / Tier / SecurityGroup object graphs

  • Applies customer overrides: port substitutions, additional ingress/egress rules

scenarios/validator.py β€” Security Validation

  • Validates scenario rules before template generation

  • Blocks dangerous patterns as ERRORs: database ports open to 0.0.0.0/0 or ::/0, missing rule descriptions, references to non-existent tiers

  • Warns on risky patterns: SSH (22) or RDP (3389) open to the world

models/ β€” Data Models

  • Scenario, Tier β€” represent the scenario structure

  • SecurityGroup, SecurityGroupRule β€” represent individual SG rules with port/port_range, source/source_tier, destination/destination_tier fields

license/validator.py β€” License Validation

  • Validates AWS Marketplace product subscription before any action executes

  • Calls AWS Marketplace Metering API to verify entitlement

utils/html_generator.py β€” Deployment Report

  • Generates post-deployment HTML report with real SG IDs, stack outputs, and deployment metadata

utils/review_report.py β€” Pre-Deployment Report

  • Generates pre-deployment review YAML (templates/) and HTML (reports/) with override highlighting


Key Architecture DecisionsΒΆ

ADR-001: CloudFormation over Direct Boto3 Resource CreationΒΆ

Decision: Use AWS CloudFormation for Security Group provisioning.

Alternatives Considered: Direct boto3 API calls for each resource.

Rationale:

  • Atomic Operations β€” CloudFormation treats all Security Groups as a single unit, ensuring all-or-nothing deployment

  • Automatic Rollback β€” Failed deployments automatically roll back to the previous state

  • Dependency Management β€” CloudFormation handles resource dependencies automatically

  • State Management β€” CloudFormation maintains resource state, enabling updates, change preview, and drift detection

  • Change Sets β€” Preview changes before applying them via show-changes action

Consequences:

  • βœ… Simplified error handling and recovery

  • βœ… Built-in drift detection via check-drift action

  • βœ… Change preview via CloudFormation ChangeSets

  • βœ… Async operations handled via boto3 waiter pattern β€” provides reliable completion detection with configurable polling interval and timeout


ADR-002: Scenario-Based ArchitectureΒΆ

Decision: All Security Group topologies defined as pre-built YAML scenario files.

Rationale:

  • Separation of Concerns β€” Network topology (data) separated from provisioning logic (code)

  • Reusability β€” Common architectures templated and reused across clients

  • Override System β€” Clients customise scenarios without modifying core files

  • Validation β€” Scenarios validated for security issues before deployment

  • Extensibility β€” New scenarios added by dropping a YAML file into schemas/scenarios/

Consequences:

  • βœ… Consistent security posture across deployments

  • βœ… Easy to add new architecture patterns

  • βœ… Overrides keep customisations minimal and auditable

  • βœ… Fundamentally different architectures are captured as new named scenarios β€” promoting reuse and standardisation


ADR-003: Standalone Cross-Tier Rules to Avoid Circular DependenciesΒΆ

Decision: Cross-tier Security Group references are generated as standalone AWS::EC2::SecurityGroupIngress / AWS::EC2::SecurityGroupEgress resources rather than inline rules.

Context: In a 3-tier scenario, the web SG references the app SG and vice versa. If both are defined as inline rules within their respective AWS::EC2::SecurityGroup resources, CloudFormation detects a circular dependency and fails.

Rationale:

  • Standalone ingress/egress resources break the circular dependency by decoupling the rule from the SG resource definition

  • CloudFormation can then resolve the dependency graph correctly

Consequences:

  • βœ… No circular dependency errors regardless of scenario complexity

  • βœ… Transparent to the client β€” handled automatically by CfnGenerator

  • βœ… Additional standalone resources per cross-tier rule keep the template clean and explicit


ADR-004: Cython Compilation for Code ProtectionΒΆ

Decision: Compile core Python modules to .so binary files using Cython.

Modules Compiled:

  • config/loader.py, core/sg_manager.py, generators/cfn_generator.py

  • scenarios/loader.py, scenarios/validator.py, license/validator.py

  • utils/html_generator.py, utils/review_report.py

Rationale:

  • IP Protection β€” Core business logic not exposed as plain text

  • Tamper Resistance β€” Binary files harder to modify

  • AWS Marketplace Standard β€” Common practice for commercial containerised products

Consequences:

  • βœ… Protected intellectual property

  • βœ… cli.py and __init__.py remain as plain Python for entry point compatibility

  • βœ… Platform-specific binaries optimised for Linux x86_64 β€” the standard deployment environment


Security ArchitectureΒΆ

Code ProtectionΒΆ

Core modules are compiled to .so binary files via Cython, protecting intellectual property and preventing reverse engineering. Only cli.py and __init__.py are distributed as plain Python β€” these are entry points with no business logic.

License ValidationΒΆ

Every action β€” including local-only operations like validate-config β€” requires a valid AWS Marketplace subscription. License validation runs before any business logic executes. If validation fails, the tool exits immediately with a clear error message.

Container SecurityΒΆ

Control

Implementation

Non-root execution

Container runs as sguser (UID 1000)

Read-only credentials

~/.aws mounted as :ro

Read-only config

/app/configs mounted as :ro

No exposed ports

Container exposes no network services

No privileged mode

Standard Docker execution

Minimal base image

python:3.13-slim (Debian)

Multi-stage build

Build dependencies excluded from runtime image

Credential ManagementΒΆ

  • No credentials hardcoded in code or container image

  • AWS credentials provided at runtime via volume mount or environment variables

  • IAM roles (EC2/ECS instance metadata) supported β€” no credentials needed

  • Temporary credentials (STS AssumeRole, SSO) fully supported

Input ValidationΒΆ

  • All configuration files validated against JSON schema before any AWS operation

  • Config filenames cannot contain path separators (path traversal prevention)

  • Scenario validator blocks dangerous security group rules before template generation

  • Port values validated to be within 1-65535 range

IAM Least PrivilegeΒΆ

The generated IAM policy (create-policy action) follows least privilege principles:

  • EC2 permissions scoped to the deployment region and account

  • SSM permissions split into two statements:

    • Read-only on /vpc/ path β€” to resolve VPC ID

    • Full CRUD on /sg/ path β€” to store and delete SG IDs

  • CloudFormation permissions scoped to the specific stack name pattern

Known VulnerabilitiesΒΆ

See SECURITY.md for the current vulnerability status of the base Docker image.


Deployment ArchitectureΒΆ

Docker ContainerΒΆ

SG Provisioner is distributed as a Docker container via AWS Marketplace. The container is built using a multi-stage build to keep the runtime image minimal and secure.

Multi-Stage Build:

Stage 1: Builder
  - Base: python:3.13-slim
  - Installs uv package manager
  - Resolves and installs all Python dependencies into .venv

Stage 2: Runtime
  - Base: python:3.13-slim (clean image)
  - Copies only the installed packages from Stage 1
  - Copies Cython-compiled sg_provisioner package (.so files)
  - Removes source .py files for compiled modules (IP protection)
  - Copies common library, schemas, templates, and HTML documentation
  - Runs as non-root user (sguser, UID 1000)

Container ContentsΒΆ

Path

Contents

/app/sg_provisioner/

Compiled application modules (.so files)

/app/common/

Shared library (plain Python)

/app/schemas/

Validation schema and scenario YAML files

/app/templates/

CloudFormation template placeholders

/app/docs/

Built HTML documentation (Sphinx)

/app/configs/

Mount point for customer configuration files

/app/policies/

Mount point for generated IAM policies

/app/reports/

Mount point for logs and HTML reports

Volume MountsΒΆ

Host Path

Container Path

Mode

Purpose

~/.aws

/home/sguser/.aws

:ro

AWS credentials

$(pwd)/sg/configs

/app/configs

:ro

Customer configuration files

$(pwd)/sg/policies

/app/policies

rw

Generated IAM policies

$(pwd)/sg/templates

/app/templates

rw

Generated CloudFormation templates

$(pwd)/sg/reports

/app/reports

rw

Execution logs and HTML reports

EntrypointΒΆ

The container entrypoint (entrypoint.sh) invokes the SG Provisioner CLI, which:

  1. Validates the AWS Marketplace license

  2. Parses CLI arguments (-con, -act, --force, --verbose)

  3. Instantiates SgManager and executes the requested action

Health CheckΒΆ

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD python -c "import sys; sys.exit(0)" || exit 1

Deployment ModelsΒΆ

Interactive (Developer):

docker run --rm \
  -v ~/.aws:/home/sguser/.aws:ro \
  -v $(pwd)/sg/configs:/app/configs:ro \
  -v $(pwd)/sg/reports:/app/reports \
  sg-provisioner:latest \
  -con my-config.yaml -act validate-config

CI/CD Pipeline:

- name: Deploy security groups
  run: |
    docker run --rm \
      -v ~/.aws:/home/sguser/.aws:ro \
      -v $(pwd)/sg/configs:/app/configs:ro \
      -v $(pwd)/sg/templates:/app/templates \
      -v $(pwd)/sg/reports:/app/reports \
      sg-provisioner:latest \
      -con my-config.yaml -act create-security-groups --force

EC2/ECS with IAM Role:

docker run --rm \
  -v $(pwd)/sg/configs:/app/configs:ro \
  -v $(pwd)/sg/reports:/app/reports \
  sg-provisioner:latest \
  -con my-config.yaml -act create-security-groups --force

Copyright Β© 2025 Axon Tech Labs. All rights reserved.

See LICENSE.txt for terms and conditions.