Skip to main content

Dify Common Shared Configuration Module

The Dify Common module defines the Dify LLM application platform configuration for the RAD Modules ecosystem. It is a shared configuration module — it creates a small set of GCP resources (Secret Manager secret) and produces config, secret_ids, storage_buckets, secret_values, and path outputs consumed by the platform-specific wrapper modules (Dify CloudRun and Dify GKE).

1. Overview

Purpose: To centralise all Dify-specific configuration (custom container image, PostgreSQL 15 + pgvector setup, Redis/Celery environment variables, storage bucket, SECRET_KEY generation, and initialisation job) in a single module shared by both Cloud Run and GKE deployments.

Architecture:

Layer 3: Application Wrappers
├── Dify_CloudRun ──┐
└── Dify_GKE ──┤── instantiate Dify_Common

Dify_Common (this module)
Creates: Secret Manager secret (SECRET_KEY)
Produces: config, secret_ids, storage_buckets, secret_values, path

Layer 2: Platform Modules
├── App_CloudRun (serverless deployment)
└── App_GKE (Kubernetes deployment)

Layer 1: App_Common (networking, database, storage, secrets, IAM)

Key characteristics:

  • Uses PostgreSQL 15 with the pgvector extension enabled — Dify uses pgvector as its default vector store.
  • Creates one GCP resource: a Secret Manager secret for the SECRET_KEY (64-character random value).
  • Configures three Redis connection paths: Celery broker (db 1), Celery backend (db 1), and event bus (db 0).
  • The GOOGLE_STORAGE_BUCKET_NAME is set to <resource_prefix>-storage — this bucket is provisioned by App CloudRun/App GKE and used by Dify for file storage via the Google Storage driver.
  • The db-init.sh script creates the PostgreSQL database and user idempotently using postgres:15-alpine.
  • The $(NFS_SERVER_IP) placeholder in Redis configuration is resolved at runtime by App CloudRun/App GKE when redis_host is not provided.

2. Outputs

config

The application configuration object passed to the platform module via application_config.

FieldValue / Description
app_name"dify"
display_namevar.display_name
descriptionvar.description
container_image"langgenius/dify-api"
application_versionvar.application_version (default: "0.15.0")
image_source"custom" — a custom wrapper image is built via Cloud Build
enable_image_mirroringvar.enable_image_mirroring
container_build_configdockerfile_path = "Dockerfile", context_path = ".", build_args = {}
container_port5001 — Dify API server port
database_type"POSTGRES_15" — Dify requires PostgreSQL
db_namevar.db_name (default: "dify_db")
db_uservar.db_user (default: "dify_user")
enable_cloudsql_volumevar.enable_cloudsql_volume (default: true)
cloudsql_volume_mount_path"/cloudsql"
gcs_volumesvar.gcs_volumes
enable_postgres_extensionstrue — always enabled
postgres_extensions["vector"] — pgvector for vector similarity search
container_resourcesCPU: var.cpu_limit, Memory: var.memory_limit
min_instance_countvar.min_instance_count
max_instance_countvar.max_instance_count
startup_probevar.startup_probe (HTTP /health, 30 s delay)
liveness_probevar.liveness_probe (HTTP /health, 30 s delay)
initialization_jobsDefault db-init job or custom override
additional_services[] — web service is defined at the wrapper level

environment_variables (within config)

The following environment variables are set automatically by Dify Common:

VariableValueDescription
DIFY_BIND_ADDRESS"0.0.0.0"API bind address
DIFY_PORT"5001"API server port
SERVER_WORKER_AMOUNT"2"Gunicorn worker count
GUNICORN_TIMEOUT"360"Gunicorn request timeout
DEPLOY_ENV"PRODUCTION"Deployment environment
MIGRATION_ENABLED"true"Runs Flask-Migrate on startup
DB_TYPE"postgresql"Database type
DB_USERNAME"$(DB_USER)"Platform-injected database user
DB_DATABASE"$(DB_NAME)"Platform-injected database name
REDIS_HOSTResolved Redis host (NFS IP or redis_host)Redis connection
REDIS_PORTvar.redis_portRedis port
REDIS_PASSWORDvar.redis_auth (if set)Redis authentication
REDIS_USE_SSL"false"Redis SSL mode
REDIS_DB"0"Redis database index
CELERY_BROKER_URLredis://<auth>@<host>:<port>/1Celery task broker
CELERY_BACKENDredis://<auth>@<host>:<port>/1Celery result backend
BROKER_USE_SSL"false"Celery broker SSL mode
EVENT_BUS_REDIS_URLredis://<auth>@<host>:<port>/0SSE/WebSocket event bus
STORAGE_TYPE"google-storage"Storage backend
GOOGLE_STORAGE_BUCKET_NAME"<resource_prefix>-storage"GCS bucket for Dify files
VECTOR_STORE"pgvector"Vector store backend
PGVECTOR_HOST"$(DB_IP)"pgvector host (TCP IP)
PGVECTOR_PORT"5432"pgvector port
PGVECTOR_USER"$(DB_USER)"pgvector user
PGVECTOR_PASSWORD"$(DB_PASSWORD)"pgvector password
PGVECTOR_DATABASE"$(DB_NAME)"pgvector database
CONSOLE_API_URLvar.service_urlDify console API URL
CONSOLE_WEB_URLvar.service_urlDify console web URL
SERVICE_API_URLvar.service_urlDify service API URL
APP_API_URLvar.service_urlDify app API URL
APP_WEB_URLvar.service_urlDify app web URL
FILES_URLvar.service_urlDify files URL
WEB_API_CORS_ALLOW_ORIGINS"*"CORS origins (restrict in production)
CONSOLE_CORS_ALLOW_ORIGINS"*"Console CORS origins
CHECK_UPDATE_URL""Disables update check
LOG_LEVEL"INFO"Application log level

secret_ids

KeySecretDescription
SECRET_KEY<resource_prefix>-secret-key64-character random JWT signing key

storage_buckets

FieldValue
name_suffix"dify-storage"
name"<resource_prefix>-storage"
locationvar.region
storage_class"STANDARD"
public_access_prevention"inherited"

path

The module directory path (path.module). Used by wrapper modules to resolve scripts_dir.


3. Variables

VariableTypeDefaultDescription
project_idstringGCP project ID. Required.
resource_prefixstringPrefix for resource naming. Must match the resource_prefix used by the calling module.
labelsmap(string){}Labels to apply to resources.
tenant_deployment_idstring"demo"Unique tenant/deployment identifier.
deployment_idstring""Random deployment ID suffix.
regionstring"us-central1"GCP region for resource deployment.
application_namestring"dify"Application name.
application_versionstring"latest"Application version tag.
display_namestring"Dify - LLM Application Platform"Application display name.
descriptionstring(Dify description)Application description.
db_namestring"dify"Database name.
db_userstring"dify"Database user.
cpu_limitstring"2000m"CPU limit for the container.
memory_limitstring"4Gi"Memory limit for the container.
min_instance_countnumber1Minimum number of instances.
max_instance_countnumber3Maximum number of instances.
startup_probeobject(HTTP /health, 30 s)Startup probe configuration.
liveness_probeobject(HTTP /health, 30 s)Liveness probe configuration.
environment_variablesmap(string){}Additional environment variables merged into the container spec.
enable_cloudsql_volumebooltrueEnable Cloud SQL Auth Proxy sidecar.
initialization_jobslist(any)[]Initialization jobs configuration.
service_urlstring""The URL where the service will be accessible. Used for all Dify service URL variables.
db_hoststringnullDatabase host (IP or socket path).
redis_hoststringnullRedis host. Defaults to NFS server IP or $(NFS_SERVER_IP) placeholder.
redis_portstring"6379"Redis port.
enable_redisbooltrueEnable Redis (required for Dify Celery task queue and caching).
redis_authstring""Redis authentication password. Sensitive.
nfs_server_ipstringnullNFS server IP used for Redis when no host is provided.
bucket_namestring""GCS bucket name for Dify storage.
service_account_emailstring""Service account email for GCS access.
enable_image_mirroringbooltrueEnable image mirroring to Artifact Registry.
gcs_volumeslist(object)[]GCS Fuse volume mounts.

4. Default Initialization Job

When initialization_jobs = [] (the default), Dify Common injects a single db-init job:

FieldValue
name"db-init"
description"Create Dify Database and User"
image"postgres:15-alpine"
cpu_limit"1000m"
memory_limit"512Mi"
timeout_seconds600
execute_on_applytrue
script_pathDify_Common/scripts/db-init.sh

The script creates the PostgreSQL database user and database idempotently. It runs via the Cloud SQL Auth Proxy Unix socket (Cloud Run) or via the Auth Proxy sidecar (GKE).

Provide a non-empty initialization_jobs list to replace this default with custom jobs entirely.


5. Redis URL Construction

Dify Common constructs Redis URLs based on the following logic:

  1. If enable_redis = false — all Redis variables are set to empty strings.
  2. If redis_host is provided — use it directly.
  3. If nfs_server_ip is provided — use it as the host.
  4. Otherwise — use the $(NFS_SERVER_IP) placeholder, which App CloudRun/App GKE resolves at runtime.

When redis_auth is set, the auth segment is included as :password@ in the URL. An empty redis_auth produces clean redis://host:port/db URLs without a malformed :@ segment.