Skip to content

About

IaC for deploying n8n to Azure App Service with PostgreSQL backend.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

n8n Azure Deployment

This project provides infrastructure as code (IaC) for deploying n8n (workflow automation tool) to Azure App Service using Docker containers, with Azure Database for PostgreSQL as the backend database.

Architecture

The deployment includes:

  • Azure App Service (Linux): Hosts n8n in a Docker container
  • Azure Database for PostgreSQL Flexible Server: Stores n8n workflow data
  • Azure Key Vault: Securely stores secrets and credentials
  • Bicep Templates: Infrastructure as Code for reproducible deployments

Latest Release

See RELEASE_NOTES.md for public release highlights and security hardening updates as of March 30, 2026.

Prerequisites

Before deploying, ensure you have:

  1. Azure Subscription: An active Azure subscription with appropriate permissions
  2. Azure CLI: Installed and configured (Download)
  3. PowerShell: Version 7.0 or higher (for Windows)
  4. Git: For source control

Azure CLI Setup

  1. Install Azure CLI from here
  2. Login to Azure:
    az login
  3. Verify your subscription:
    az account list --output table
  4. Set your default subscription (if needed):
    az account set --subscription "your-subscription-id"

Quick Start

1. Clone the Repository

git clone <repository-url>
cd SelfHostedn8n

2. Deploy Infrastructure

Run the deployment script:

.\scripts\Deploy-Infrastructure.ps1 -SubscriptionId "your-subscription-id" -Environment dev -CompanyName "acme"

The script will:

  • Generate a PostgreSQL administrator password and store it in the key vault
  • Generate n8n basic authentication password and store it in key vault
  • Generate an n8n encryption key automatically and store it in the key vault
  • Create all required Azure resources
  • Display deployment outputs including the n8n URL

3. Access n8n

After deployment completes, you'll receive:

  • App Service URL: https://<app-service-name>.azurewebsites.net
  • Credentials: Basic auth username (default: admin) and password stored in Azure Key Vault

Open the URL in your browser and log in with the basic auth credentials.

Detailed Deployment Guide

Deployment Parameters

The deployment script accepts the following parameters:

Parameter Description Default
SubscriptionId Azure subscription ID Required
ResourceGroupName Resource group name Auto-generated
Location Azure region centralus
Environment Environment name (dev/staging/prod) dev
CompanyName Company/org identifier for naming Optional
PostgresAdminPassword PostgreSQL admin password Auto-generated
N8nEncryptionKey n8n encryption key Auto-generated
N8nBasicAuthPassword n8n basic auth password Auto-generated
ParameterFile Path to Bicep parameter file Optional

Using Parameter Files

You can use predefined parameter files for different environments:

Development:

.\scripts\Deploy-Infrastructure.ps1 `
    -SubscriptionId "your-subscription-id" `
    -Environment dev `
    -CompanyName "acme" `
    -ParameterFile "infrastructure\parameters\dev.bicepparam"

Note on CompanyName: The CompanyName parameter helps make resource names globally unique. It will be sanitized to lowercase alphanumeric characters and hyphens. For example, "Acme Corp" becomes "acme-corp" in resource names. This is especially useful when multiple teams or organizations deploy to the same Azure subscription.

Production:

.\scripts\Deploy-Infrastructure.ps1 `
    -SubscriptionId "your-subscription-id" `
    -Environment prod `
    -ParameterFile "infrastructure\parameters\prod.bicepparam"

Manual Bicep Deployment

You can also deploy directly using Azure CLI:

az deployment group create `
    --resource-group "rg-n8n-dev" `
    --template-file "infrastructure\main.bicep" `
    --parameters location="centralus" `
    --parameters environment="dev" `
    --parameters postgresAdminPassword="YourSecurePassword" `
    --parameters n8nEncryptionKey="YourEncryptionKey" `
    --parameters n8nBasicAuthPassword="YourAuthPassword"

Configuration

n8n Configuration

The deployment configures n8n with the following settings:

  • Database: PostgreSQL (automatically configured)
  • Protocol: HTTPS
  • Authentication: Basic Auth (when user management is disabled)
  • Metrics: Enabled
  • Data Pruning: Enabled (7 days retention)

Important - Network Access: The App Service is configured to allow traffic from all networks by default. This is required for n8n to function properly, as webhooks and external integrations need to receive incoming requests from the internet. Access control should be handled through n8n's built-in authentication (basic auth or user management) rather than App Service IP restrictions.

Environment Variables

Key environment variables configured in App Service:

Database Configuration:

  • DB_TYPE: postgresdb
  • DB_POSTGRESDB_HOST: PostgreSQL server FQDN
  • DB_POSTGRESDB_DATABASE: n8n
  • PGSSLMODE: require (ensures SSL connection to PostgreSQL)
  • DB_POSTGRESDB_POOL_SIZE_MIN: 2 (minimum connection pool size)
  • DB_POSTGRESDB_POOL_SIZE_MAX: 10 (maximum connection pool size)

n8n Configuration:

  • N8N_ENCRYPTION_KEY: Your encryption key
  • N8N_PROTOCOL: https
  • N8N_PORT: 5678
  • N8N_HOST: App Service hostname
  • WEBHOOK_URL: Full webhook URL
  • N8N_BASIC_AUTH_ACTIVE: true (when user management disabled)
  • N8N_USER_MANAGEMENT_DISABLED: true (when using basic auth)

Azure App Service Proxy Configuration:

  • N8N_PROXY_HOPS: 1 (for reverse proxy support)
  • TRUST_PROXY: true (trust Azure App Service proxy headers)

Note: The template automatically configures PostgreSQL firewall rules and SSL settings. The firewall rule AllowAzureServices (0.0.0.0 to 0.0.0.0) allows all Azure services to connect to PostgreSQL, which is required for the App Service to access the database. Connection pool settings help optimize database performance and reduce connection timeouts.

Customizing Configuration

To modify n8n settings, update the App Service application settings:

az webapp config appsettings set `
    --resource-group "rg-n8n-dev" `
    --name "n8n-dev-xxxxx" `
    --settings N8N_METRICS="false"

Or use the Azure Portal:

  1. Navigate to your App Service
  2. Go to Configuration > Application settings
  3. Add or modify settings
  4. Click Save

Managing the Deployment

View Deployment Outputs

.\scripts\Get-DeploymentOutputs.ps1 -ResourceGroupName "rg-n8n-dev-20240101"

Update Infrastructure

To update the infrastructure:

  1. Modify the Bicep files as needed
  2. Run the deployment script again (it will update existing resources)

Remove Infrastructure

WARNING: This will permanently delete all resources!

.\scripts\Remove-Infrastructure.ps1 -ResourceGroupName "rg-n8n-dev-20240101"

Or with force flag (no confirmation):

.\scripts\Remove-Infrastructure.ps1 -ResourceGroupName "rg-n8n-dev-20240101" -Force

Security Best Practices

Production Deployment

For production deployments:

  1. Disable PostgreSQL Public Access:

    • Set postgresPublicAccessEnabled = false in parameter file
    • Use private endpoints or VPN for database access
  2. Enable User Management:

    • Set n8nUserManagementEnabled = true
    • This disables basic auth and enables n8n's built-in user management
  3. Use Strong Passwords:

    • PostgreSQL password: Minimum 8 characters with complexity
    • n8n encryption key: At least 32 random characters
    • Store all secrets in Azure Key Vault
  4. Enable HTTPS Only:

    • Already configured in the template
    • Ensure custom domains use SSL certificates
  5. Network Security:

    • App Service Network Access: The App Service must allow traffic from all networks (no IP restrictions) for n8n to function properly. Webhooks and external integrations require public access. If you need to restrict access, use n8n's built-in authentication (basic auth or user management) instead of App Service IP restrictions.
    • Consider using Azure Private Endpoints for database connections
    • Use Azure Firewall if needed for additional network security
    • PostgreSQL Firewall Rules: The template automatically creates a firewall rule allowing Azure services. For production, consider:
      • Using private endpoints instead of public access
      • Replacing the 0.0.0.0 firewall rule with specific App Service outbound IP addresses
      • Verifying firewall rules are correctly configured after deployment

Key Vault Integration

Secrets are automatically stored in Azure Key Vault. To retrieve them:

# Get PostgreSQL password
az keyvault secret show `
    --vault-name "kv-n8n-dev-xxxxx" `
    --name "postgres-admin-password" `
    --query "value" `
    --output tsv

# Get n8n encryption key
az keyvault secret show `
    --vault-name "kv-n8n-dev-xxxxx" `
    --name "n8n-encryption-key" `
    --query "value" `
    --output tsv

Troubleshooting

App Service Not Starting

  1. Check App Service logs:

    az webapp log tail --name "n8n-dev-xxxxx" --resource-group "rg-n8n-dev"
  2. Check deployment logs in Azure Portal:

    • Navigate to App Service > Deployment Center > Logs
  3. Verify environment variables are set correctly

Database Connection Issues

Important: The Bicep template automatically configures PostgreSQL firewall rules and SSL settings. If you encounter connection errors, verify these are configured correctly.

  1. Verify PostgreSQL server is running:

    az postgres flexible-server show `
        --name "n8n-dev-postgres-xxxxx" `
        --resource-group "rg-n8n-dev"
  2. Check firewall rules (critical for App Service connectivity):

    az postgres flexible-server firewall-rule list `
        --name "n8n-dev-postgres-xxxxx" `
        --resource-group "rg-n8n-dev"

    The template automatically creates a firewall rule named AllowAzureServices (0.0.0.0 to 0.0.0.0) that allows all Azure services to connect. If this rule is missing, the App Service will not be able to connect to PostgreSQL.

    Common Error: no pg_hba.conf entry for host "X.X.X.X", user "n8nadmin", database "n8n", no encryption

    This error indicates either:

    • Missing firewall rule (App Service IP not allowed)
    • SSL not configured (connection must use SSL)

    Solution: Ensure the firewall rule exists and PGSSLMODE=require is set in App Service settings.

  3. Verify SSL configuration in App Service:

    az webapp config appsettings list `
        --name "n8n-dev-xxxxx" `
        --resource-group "rg-n8n-dev" `
        --query "[?name=='PGSSLMODE'].value"

    Should return require. If missing, add it:

    az webapp config appsettings set `
        --name "n8n-dev-xxxxx" `
        --resource-group "rg-n8n-dev" `
        --settings PGSSLMODE="require"
  4. Test connection from App Service:

    • Use SSH to connect to App Service container
    • Test PostgreSQL connection

Access Issues

  1. Verify App Service Network Access:

    The App Service must allow traffic from all networks. If IP restrictions are configured, n8n webhooks and external integrations will fail.

    Check for IP restrictions:

    az webapp config access-restriction show `
        --name "n8n-dev-xxxxx" `
        --resource-group "rg-n8n-dev"

    If restrictions exist, remove them or ensure they allow all traffic:

    # Remove all IP restrictions (allow all traffic)
    az webapp config access-restriction remove `
        --name "n8n-dev-xxxxx" `
        --resource-group "rg-n8n-dev" `
        --rule-name "<rule-name>"

    Important: Use n8n's built-in authentication (basic auth or user management) for access control instead of App Service IP restrictions, as webhooks require public access.

  2. Verify basic auth credentials are correct:

    az webapp config appsettings list `
        --name "n8n-dev-xxxxx" `
        --resource-group "rg-n8n-dev" `
        --query "[?name=='N8N_BASIC_AUTH_USER' || name=='N8N_BASIC_AUTH_ACTIVE']"
  3. Check App Service authentication settings

  4. Review n8n logs for authentication errors

Cost Optimization

Development Environment

  • App Service Plan: B1 (Basic tier)
  • PostgreSQL: Standard_B1ms (Burstable)
  • Storage: 32 GB

Estimated cost: ~$30-50/month

Production Environment

  • App Service Plan: P1V2 (Premium V2)
  • PostgreSQL: Standard_D2s_v3 (General Purpose)
  • Storage: 128 GB

Estimated cost: ~$200-300/month

Cost Saving Tips

  1. Use Azure Reserved Instances for App Service Plans
  2. Enable auto-shutdown for development environments
  3. Use Azure Cost Management to monitor spending
  4. Scale down during non-business hours (use Azure Automation)

Monitoring

Application Insights (Optional)

To add Application Insights monitoring:

  1. Create Application Insights resource
  2. Add instrumentation key to App Service settings:
    az webapp config appsettings set `
        --resource-group "rg-n8n-dev" `
        --name "n8n-dev-xxxxx" `
        --settings APPLICATIONINSIGHTS_CONNECTION_STRING="your-connection-string"

Logging

View logs in real-time:

az webapp log tail --name "n8n-dev-xxxxx" --resource-group "rg-n8n-dev"

Download logs:

az webapp log download `
    --name "n8n-dev-xxxxx" `
    --resource-group "rg-n8n-dev" `
    --log-file "logs.zip"

Backup and Recovery

Database Backups

PostgreSQL Flexible Server includes automated backups:

  • Retention: 7 days (configurable)
  • Backup frequency: Daily

Manual Backup

az postgres flexible-server backup create `
    --resource-group "rg-n8n-dev" `
    --server-name "n8n-dev-postgres-xxxxx" `
    --backup-name "manual-backup-$(Get-Date -Format 'yyyyMMdd')"

Restore Database

az postgres flexible-server restore `
    --resource-group "rg-n8n-dev" `
    --name "n8n-dev-postgres-restored" `
    --source-server "n8n-dev-postgres-xxxxx" `
    --restore-time "2024-01-01T00:00:00Z"

Updating n8n

To update to a newer version of n8n:

  1. Update the Docker image tag in main.bicep:

    linuxFxVersion: 'DOCKER|n8nio/n8n:latest'
  2. Redeploy or update App Service:

    az webapp config set `
        --name "n8n-dev-xxxxx" `
        --resource-group "rg-n8n-dev" `
        --linux-fx-version "DOCKER|n8nio/n8n:1.0.0"
  3. Restart the App Service:

    az webapp restart --name "n8n-dev-xxxxx" --resource-group "rg-n8n-dev"

Support and Resources

License

This project is provided as-is for deploying n8n to Azure.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

Note: Remember to save all credentials securely. The encryption key and passwords are critical for accessing your n8n instance and cannot be recovered if lost.

About

IaC for deploying n8n to Azure App Service with PostgreSQL backend.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages