DEV Community

Cover image for Self-Hosted N8N on AWS ECS with AWS CDK Typescript

Self-Hosted N8N on AWS ECS with AWS CDK Typescript

πŸ“‹ Abstract

This repository provides Infrastructure as Code (IaC) solution for deploying a self-hosted N8N workflow automation platform on AWS using the AWS Cloud Development Kit (CDK). The solution implements a scalable, cost-optimized, and highly available architecture leveraging AWS ECS Fargate, PostgreSQL RDS database instance, and ElastiCache Redis for queue management.

πŸ“‘ Table of Contents

πŸ—οΈ Architecture Overview

Architecture Flow

Key Components:

  • VPC with Multi-AZ Subnets: Isolated network with public, private, and isolated subnets across multiple availability zones
  • Network Load Balancer (NLB): Distributes HTTPS traffic to N8N main service instances
  • ECS Fargate Cluster: Runs containerized N8N services with auto-scaling capabilities
  • RDS PostgreSQL: Multi-AZ managed database for workflow data persistence
  • ElastiCache Redis: In-memory cache for BullMQ job queue management
  • AWS Cloud Map: Service discovery for internal service-to-service communication

πŸ” Deep-Dive the Solution

Detailed Architecture Diagram

πŸš€ Deploy CDK Stack and Test

πŸ“‹ Prerequisites

Before deploying, ensure you have:

  • AWS CLI installed and configured (aws configure)
  • Node.js 16+ and pnpm installed
  • AWS CDK CLI installed (npm install -g aws-cdk)
  • An AWS account with appropriate permissions
  • A Route53 hosted zone (optional, for custom domain)

βš™οΈ Environment Setup

  1. Clone the repository:
git clone <repository-url>
cd aws-cdk-self-hosted-n8n-infra
Enter fullscreen mode Exit fullscreen mode
  1. Install dependencies:
pnpm install
Enter fullscreen mode Exit fullscreen mode
  1. Configure environment variables: Create a .env file in the root directory:
CDK_DEFAULT_ACCOUNT=your-aws-account-id
CDK_DEFAULT_REGION=your-aws-region
HOSTED_ZONE_NAME=example.com
HOSTED_ZONE_ID=Z1234567890ABC
Enter fullscreen mode Exit fullscreen mode

πŸš€ Deployment Steps

  1. Synthesize CloudFormation template:
pnpm run synth
Enter fullscreen mode Exit fullscreen mode
  1. Review the changes:
pnpm run diff
Enter fullscreen mode Exit fullscreen mode
  1. Deploy the stack:
pnpm run deploy
Enter fullscreen mode Exit fullscreen mode

βœ… Verify Deployment

After successful deployment, verify the resources in AWS Console:

1. ECS Cluster:

ECS Cluster

The ECS cluster shows both N8N services running with the configured task definitions and capacity providers.

2. Main Service:

Main Service

The N8N main service displays running tasks, auto-scaling configuration, and health status.

3. Worker Service:

Worker Service

The worker service shows active workers processing queued jobs with concurrency settings.

4. Network Load Balancer:

NLB

The NLB shows the listener configuration and DNS name which is used to create alias record in route53.

5. Target Group:

Target Group

The target group displays registered ECS tasks with health check status and routing configuration.

πŸ§ͺ Testing N8N Workflows

1. Access N8N UI:

Navigate to your NLB DNS name or custom domain (if configured):

https://n8n.simflexcloud.com
Enter fullscreen mode Exit fullscreen mode

N8N Interface

The N8N interface shows the workflow editor where you can create, test, and monitor automation workflows.

2. Create a Test Workflow:

Example workflow: Send an inspirational quote from internet to Slack daily

3. Verify Execution:

Slack Integration

The Slack integration demonstrates successful workflow execution with the message being delivered to the configured Slack channel.

🧹 Cleanup Stack

To avoid ongoing AWS charges, destroy all resources:

# Destroy the CDK stack
pnpm run destroy
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ Conclusion

This CDK solution provides a scalable, and cost-optimized infrastructure as code (AWS CDK) for running N8N on AWS ECS.


πŸ“š References

Top comments (0)