← All projects

Python FastApi and AWS ECS infrastructure

ElasticScan: asynchronous security scanning on AWS

An asynchronous scanning service that separates durable API acceptance from background execution, with an ECS deployment foundation managed through Terraform.

RoleBackend development and cloud infrastructure
Year2026
StatusActive lab

Context

Security scans can take minutes, while an HTTP request needs a predictable response. ElasticScan explores how to accept work reliably, execute it independently, and let users retrieve progress and results without keeping a connection open.

I am building the project around FastAPI, PostgreSQL, and an event-driven workflow using Amazon SNS and SQS. The initial scanner is OWASP ZAP baseline: bounded crawling followed by passive analysis against explicitly authorized targets. OIDC access tokens identify callers, and team-scoped authorization controls access to jobs.

The project combines backend implementation with an AWS deployment foundation. Local development uses Docker Compose, PostgreSQL, Keycloak, and LocalStack. Terraform defines the API’s ECS-on-EC2 infrastructure. The complete scanning platform is still in progress; the infrastructure definition does not yet provision every backend dependency.

Approach

ElasticScan architecture: Terraform-defined AWS API foundation and the asynchronous scanning workflow

The upper section shows the API infrastructure defined in Terraform. The lower section shows the application workflow implemented in source and local tooling; its dispatcher, workers, database, identity provider, and messaging still need complete AWS deployment integration.

Accept first, execute asynchronously

The API validates the caller, resolves an authorized team, and checks the target policy. It then stores a job and its publication intent in one PostgreSQL transaction. Only after that transaction commits does POST /scan return HTTP 202 with a job identifier.

This transactional outbox avoids a failure window where a job could be saved without recording that it needs dispatch. A separate dispatcher publishes committed intent to SNS, with bounded retries, jittered backoff, and expired-claim recovery. Publication can still be duplicated after an uncertain outcome, so the consumer must tolerate repeated deliveries.

SNS routes messages to scanner-specific SQS queues. The ZAP worker validates each message against the saved job, acquires a durable execution claim, and invokes the scanner through an adapter. It persists a normalized outcome, confirms the terminal state, and then acknowledges the delivery. Clients retrieve individual jobs or filtered team listings through the API.

Keeping the API, dispatcher, and worker separate allows their capacity and failure handling to evolve independently. The adapter also keeps scanner-specific process management outside HTTP and queue-handling code.

Run the API on ECS with Terraform

The staging configuration composes reusable networking, ALB, Auto Scaling, application-configuration, and ECS modules. It defines a VPC across two Availability Zones, public and private subnets, an internet-facing ALB, and an ECS cluster backed by an EC2 Auto Scaling group. Private subnets share one NAT gateway, a deliberate cost-versus-availability tradeoff.

The API uses bridge networking with a fixed host port. The Auto Scaling group owns ALB instance registration, while ECS manages task placement and host capacity through a capacity provider. Each API replica requires its own host; rolling replacements require spare host capacity.

ECR stores images under immutable release tags. Terraform accepts an API image tag or digest, creates the corresponding task-definition revision, and updates the service. The planned CI/CD flow is to test, build and push an image, then plan and apply that release through Terraform. That automation is a next step, rather than an implemented pipeline.

Reliability and security

I designed the failure boundaries alongside the main workflow:

  • Durable state: atomic acceptance, immutable job ownership, guarded lifecycle transitions, and versioned Alembic migrations.
  • Duplicate delivery: renewable execution leases and per-attempt claim tokens fence stale writers. Workers acknowledge only confirmed terminal outcomes. This protects logical job state without claiming exactly-once physical scanner execution.
  • Bounded execution: scanner deadlines, process-group cleanup, configurable worker CPU and memory limits, and independent recovery of abandoned claims. The current worker policy permits one automatic scanner execution attempt per job.
  • Controlled retries: selected transient infrastructure operations receive bounded retries; scanner execution and uncertain database writes are not blindly repeated. Queue redrive and bounded dead-letter observation support investigation.
  • Identity and isolation: trusted JWT validation, team-scoped queries, and identical not-found responses for unknown and cross-team job identifiers.
  • Runtime configuration: SSM Parameter Store and Secrets Manager references, separate ECS execution and application roles, a nonroot API container, and a read-only API root filesystem.

Target validation defaults to deny and checks approved origins and public IPv4/IPv6 resolution. Complete redirect, DNS-change, and enforceable scanner egress controls remain unfinished. The current AWS API design also needs external health verification: ASG-managed ALB health is separate from ECS deployment health, and task replacement does not provide fully coordinated ALB draining.

Result

The project now has authenticated submission and retrieval, durable outbox publication, a queue-consumer implementation, bounded ZAP execution, guarded result persistence, and recovery logic. Terraform defines the AWS API foundation, including image selection and ECS deployment rollback.

Verification combines unit tests, disposable PostgreSQL schemas, LocalStack messaging checks, Keycloak authentication checks, and isolated real-ZAP fixtures. A partial integration slice joins the workflow using a simulated scanner. These checks provide evidence for individual boundaries; they do not establish a fully deployed AWS scanning service or a production performance benchmark.

The main lesson has been that asynchronous processing is as much about ownership and uncertainty as it is about queues: deciding when acceptance is durable, who may execute a job, and when a message is safe to acknowledge.

Next steps include deploying the remaining AWS backend services, enforcing scanner egress boundaries, completing cancellation and dead-letter reconciliation, adding production observability, and automating releases with shared Terraform state and locking.