How aws-samples/dify-self-hosted-on-aws works
Main Components
• AWS CDK Deployment/Management: Represent the CDK source (bin/ and lib/ directories) which contains the “Dify on AWS” stack definitions.
• VPC & Networking:
• VPC (network boundaries) with possibly public and private subnets.
• VPC Endpoints (as defined in lib/constructs/vpc-endpoints.ts) to route traffic without direct internet access.
• NAT instances (or NAT Gateway alternatives) if not using full NAT gateways.
• Compute (ECS Fargate):
• API and Worker Container: (defined in lib/constructs/dify-services/api.ts) hosting backend functionality.
• Web Container: (from lib/constructs/dify-services/web.ts) serving the frontend.
• Sidecar/Additional Containers: For external knowledge API and sandbox environments (see lib/constructs/dify-services/docker folder).
• Data Services:
• Aurora Serverless (Postgres), defined in lib/constructs/postgres.ts.
• ElastiCache (Redis), defined in lib/constructs/redis.ts.
• Load Balancing and Routing:
• Application Load Balancer (ALB) possibly tied with CloudFront for distributed content (see lib/constructs/alb-with-cloudfront.ts and lib/constructs/alb.ts).
• Web ACL (lib/constructs/web-acl.ts) for security enforcement.
• Additional Components:
• Email Setup (SMTP/SES) as configured in the CDK stack.
• Environment and configuration management using EnvironmentProps (lib/environment-props.ts) and VPC/VPC endpoints.
• Integration points for AWS Bedrock and external services like Notion.
Relationships and Interactions
• The AWS CDK code (your “deployment engine”) provisions and configures all AWS services. It creates the VPC as the underlying network, establishes ECS clusters that run containerized services (API, web, and sidecars).
• The ALB (and optional CloudFront) fronts incoming HTTP/HTTPS requests – routing them to respective ECS services.
• ECS services are connected to data stores (Aurora for the business data and Redis for caching).
• There is a bidirectional interaction between the API service and external services for knowledge base connections (i.e. the External Knowledge API container and integration with AWS Bedrock) as well as possible interactions with Notion for data import.
• Environment configuration and secrets (managed possibly via AWS Secrets Manager) help secure the integration credentials and other sensitive settings.
Architectural Patterns and Design Principles
• Infrastructure as Code (IaC) using AWS CDK.
• Service-oriented or microservice pattern within ECS where each containerized service is an independent unit (API, web, external knowledge API, sandbox).
• Separation of concerns: networking (VPC, endpoints), compute (ECS Fargate tasks), data (Aurora/Redis), and edge routing (ALB/CloudFront).
• Scalability and cost effectiveness: Aurora Serverless, Fargate spot capacity, NAT instances.
• Extensibility: Options for closed network deployments and integrations with external systems (Bedrock, Notion).
Creation Process
• In networking (VPC with subnets and endpoints) on one side.
• Optionally, include a side panel for “deployment” – illustrating the CDK codebase which orchestrates this entire setup.