Skip to main content

Certification track: Associate Cloud Engineer (ACE)

Moodle on Google Cloud Run

Moodle on Google Cloud Run

Moodle is the world's most popular open-source Learning Management System (LMS), used by universities, schools, corporations, and online training providers worldwide. This module deploys Moodle on Cloud Run v2 on top of the App_CloudRun foundation, which provisions and manages the shared Google Cloud infrastructure.

This guide focuses on the cloud services Moodle uses and how to explore and operate them from the Google Cloud Console and the command line. For the mechanics common to every Cloud Run application — service identity, ingress and load balancing, scaling and concurrency, CI/CD, Cloud Armor, IAP, Binary Authorization, VPC Service Controls, backups, and the deployment lifecycle — refer to the App_CloudRun foundation guide rather than repeating them here.


1. Overview

Moodle runs as a PHP 8.3/Apache container on Cloud Run v2. The deployment wires together a focused set of Google Cloud services:

CapabilityGoogle Cloud serviceNotes
ComputeCloud Run v2PHP 8.3/Apache service, 1 vCPU / 2 GiB by default, request-based autoscaling
DatabaseCloud SQL for PostgreSQL 15Required — Moodle does not support MySQL in this deployment
Shared filesFilestore (NFS)Moodle moodledata directory shared across all instances; mandatory
Object storageCloud StorageA data bucket and any additional user-defined buckets
Cache & sessionsRedisEnabled by default; required for multi-instance PHP session consistency
SecretsSecret ManagerAuto-generated cron password, SMTP password, and database password
SchedulerCloud SchedulerAuto-provisioned cron job (every minute) against /admin/cron.php
IngressCloud Run URL / Cloud Load BalancingDefault run.app URL, optional external HTTPS load balancer + custom domain

Sensible defaults worth knowing up front:

  • PostgreSQL 15 is mandatory. The database engine is fixed and MOODLE_DB_TYPE = "pgsql" is hardcoded; selecting MySQL or NONE breaks startup.
  • NFS is mandatory. The Moodle moodledata directory must be a shared writable filesystem accessible across all instances. enable_nfs defaults to true and requires execution_environment = "gen2".
  • The startup probe is HTTP targeting /health.php. Unlike some PHP applications that issue HTTP→HTTPS redirects, Apache in this deployment binds on port 8080 without redirect, so an HTTP probe against /health.php returns 200 directly.
  • Redis is enabled by default. Without a shared session store, users are logged out when a request reaches a different Cloud Run instance.
  • A Cloud Scheduler job is auto-provisioned. It fires every minute to /admin/cron.php using a secure, auto-generated cron password stored in Secret Manager.
  • The cron password and SMTP password are generated automatically and stored in Secret Manager; you never set them in plain text.

2. Google Cloud Services & How to Explore Them

All commands assume PROJECT and REGION are set. Service and resource names are reported in the deployment Outputs.

A. Cloud Run — the Moodle service

Moodle runs as a stateless Cloud Run v2 service. Request-based autoscaling adds instances under load and, with min_instance_count = 0, scales to zero between sessions (development) or keeps a warm instance when set to 1 (production).

  • Console: Cloud Run → select the Moodle service for logs, revisions, metrics, and configuration.
  • CLI:
    gcloud run services list --region "$REGION" --project "$PROJECT"
    gcloud run services describe <service-name> --region "$REGION" --project "$PROJECT"
    gcloud run revisions list --service <service-name> --region "$REGION" --project "$PROJECT"
    # Stream recent logs:
    gcloud logging read \
    'resource.type="cloud_run_revision" AND resource.labels.service_name="<service-name>"' \
    --project "$PROJECT" --limit 50 --order asc

See App_CloudRun for scaling, concurrency, and traffic split details.

B. Cloud SQL for PostgreSQL 15

Moodle stores all application data (courses, users, grades, activity logs) in a managed Cloud SQL for PostgreSQL 15 instance. Each Cloud Run instance connects privately through the Cloud SQL Auth Proxy sidecar over a Unix socket, so no public IP is exposed. On first deploy an initialization job creates the database and user and enables the pg_trgm extension for Moodle's full-text search.

  • Console: SQL → select the instance for connections, backups, flags, and metrics.
  • CLI:
    gcloud sql instances list --project "$PROJECT"
    gcloud sql instances describe <instance-name> --project "$PROJECT"
    # Open an interactive shell to inspect schema/data:
    gcloud sql connect <instance-name> --user=<db-user> --database=<db-name> --project "$PROJECT"

The instance name, database name, user, and the Secret Manager secret holding the password are all surfaced in the Outputs. For the connection model, automated backups, and password rotation, see App_CloudRun.

C. Filestore (NFS) and Cloud Storage

Moodle's moodledata directory is written to a Filestore (NFS) share mounted into every Cloud Run instance so all instances see the same uploaded files, course materials, and user submissions. A dedicated Cloud Storage data bucket is also provisioned; the service account is granted access automatically.

NFS volume mounts require execution_environment = "gen2" (the default).

  • Console: Filestore → Instances for the NFS share; Cloud Storage → Buckets for the data bucket.
  • CLI:
    gcloud filestore instances list --project "$PROJECT"
    gcloud storage buckets list --project "$PROJECT"
    gcloud storage ls gs://<data-bucket>/ # bucket name is in the Outputs

See App_CloudRun for NFS provisioning, GCS Fuse, and CMEK options.

D. Redis cache

Redis backs Moodle's PHP session handling and application cache. When no external Redis host is configured and NFS is enabled, the NFS host IP is used as the Redis endpoint — suitable for development. For production with multiple instances, set redis_host to a Cloud Memorystore instance IP.

  • Console: Memorystore → Redis (if using a managed instance).
  • CLI:
    redis-cli -h <redis-host> ping           # from a host with network access
    redis-cli -h <redis-host> info keyspace

E. Secret Manager

The Moodle cron password and SMTP password are generated automatically by Moodle_Common and stored as Secret Manager secrets. The database password is generated and managed by the foundation. All three are injected into Cloud Run instances at runtime; plaintext never appears in configuration files.

  • Console: Security → Secret Manager.
  • CLI:
    gcloud secrets list --project "$PROJECT"
    gcloud secrets versions access latest --secret=<secret-name> --project "$PROJECT"

The database password secret name is in the Outputs. After deployment, update the SMTP password secret with your real SMTP credential:

echo -n "your-smtp-password" | \
gcloud secrets versions add <smtp-password-secret> --data-file=- --project "$PROJECT"

See App_CloudRun for rotation details.

F. Cloud Scheduler

A Cloud Scheduler job is auto-provisioned on every deployment to drive Moodle's internal task queue. It fires every minute and authenticates using the auto-generated MOODLE_CRON_PASSWORD.

  • Console: Cloud Scheduler → Jobs.
  • CLI:
    gcloud scheduler jobs list --project "$PROJECT"
    gcloud scheduler jobs describe <job-name> --location "$REGION" --project "$PROJECT"
    # Manually trigger a cron run:
    gcloud scheduler jobs run <job-name> --location "$REGION" --project "$PROJECT"

G. Networking & ingress

By default Moodle is accessible via the Cloud Run-managed run.app URL. An optional HTTPS load balancer with Cloud Armor and a custom domain can be enabled. Setting application_domains automatically injects MOODLE_REVERSE_PROXY = "true" and ENABLE_REVERSE_PROXY = "TRUE" so Moodle generates correct HTTPS URLs behind the load balancer.

  • Console: Cloud Run → select the service → Networking; Network services → Load balancing (if using Cloud Armor).
  • CLI:
    gcloud run services describe <service-name> --region "$REGION" --project "$PROJECT" \
    --format="value(status.url)"
    gcloud compute forwarding-rules list --project "$PROJECT" # if LB is enabled

See App_CloudRun for custom domains, Cloud CDN, and static IP details.

H. Cloud Logging & Monitoring

Cloud Run instance stdout/stderr flow to Cloud Logging; request metrics and Cloud SQL metrics flow to Cloud Monitoring. Optional uptime checks and alert policies are available.

  • Console: Logging → Logs Explorer; Monitoring → Dashboards / Alerting.
  • CLI:
    gcloud logging read \
    'resource.type="cloud_run_revision" AND resource.labels.service_name="<service-name>"' \
    --project "$PROJECT" --limit 50

3. Moodle Application Behaviour

  • First-deploy database setup. Two initialization jobs run before the Cloud Run service becomes live. The db-init job creates the Moodle database and user, enables the pg_trgm extension, and grants privileges (idempotent, safe to re-run). The nfs-init job creates the required Moodle subdirectories (filedir, temp, cache, localcache) on the NFS share and sets www-data ownership.
  • Automatic cron scheduling. A Cloud Scheduler job fires every minute targeting /admin/cron.php?password=<MOODLE_CRON_PASSWORD>. This drives all Moodle scheduled tasks: course backups, email notifications, badge processing, and activity completions. The job is always created and cannot be disabled.
  • Health path. Startup and liveness probes use HTTP /health.php, which returns HTTP 200 when PHP is operational. The startup probe allows up to 10 minutes (failure_threshold = 20, period_seconds = 30) for first-boot schema creation and plugin registration.
  • SMTP outbound email. SMTP settings are injected as environment variables. Override the defaults using environment_variables (see Group 6). The SMTP password is auto-generated and stored in Secret Manager; update the secret with your real credential after deployment.
  • wwwroot resolution. Moodle's config.php resolves wwwroot from the APP_URL environment variable, falling back to CLOUDRUN_SERVICE_URL. When application_domains is set, MOODLE_REVERSE_PROXY and ENABLE_REVERSE_PROXY are automatically set to "true" / "TRUE" so Moodle generates correct HTTPS URLs behind the load balancer.
  • Scale-to-zero. min_instance_count = 0 is the default; the first request after idle will cold-start the PHP/Apache container. Set min_instance_count = 1 in production to eliminate cold-start latency for students.
  • Admin login. The initial admin username and email are configurable via environment_variables. The admin password is set during Moodle's first install via admin/cli/install_database.php.

4. Configuration Variables

Variables are grouped exactly as they appear on the deployment platform. Only settings specific to or notable for Moodle are listed; every other input is inherited from App_CloudRun with its standard behaviour and defaults.

Group 1 — Project & Identity

VariableDefaultDescription
project_id(required)Target Google Cloud project.
regionus-central1Region for the service and regional resources.

Group 2 — Deployment Environment

VariableDefaultDescription
tenant_deployment_iddemoShort suffix that makes resource names unique per environment.
support_users[]Emails granted project access and monitoring alerts.
resource_labels{}Labels applied to all resources for cost/ownership tracking.

Group 3 — Application Identity

VariableDefaultDescription
application_namemoodleBase name for resources. Do not change after first deploy.
display_nameMoodle LMSFriendly name shown in the Console.
descriptionMoodle LMS - Online learning and course management platformService description annotation.
application_version4.5.1Container image version tag; increment to roll out a new version.

Group 4 — Runtime & Scaling

VariableDefaultDescription
deploy_applicationtrueSet false to provision infrastructure only.
cpu_limit1000mCPU per instance. Increase to 2000m for production with concurrent students.
memory_limit2GiMemory per instance; 2 GiB provides headroom for PHP with OPcache.
min_instance_count0Minimum live instances. Set to 1 to eliminate cold-start latency.
max_instance_count3Maximum concurrent instances.
container_port8080Moodle/Apache listens on port 8080.
execution_environmentgen2Required for NFS volume mounts.
timeout_seconds300Maximum request duration; increase up to 3600 for large file uploads.
enable_image_mirroringfalseDisabled — Moodle uses a custom Dockerfile with no external prebuilt image.

Group 5 — Networking

VariableDefaultDescription
ingress_settingsall"all" allows public internet traffic. Use "internal-and-cloud-load-balancing" when Cloud Armor is in front.
vpc_egress_settingPRIVATE_RANGES_ONLYRoutes only RFC 1918 traffic via the VPC connector.

Group 6 — Environment Variables & Secrets

VariableDefaultDescription
environment_variables{}Extra non-secret settings. Override auto-injected SMTP defaults here (e.g., MOODLE_SMTP_HOST, MOODLE_ADMIN_EMAIL).
secret_environment_variables{}Map of env var → Secret Manager secret name.
secret_propagation_delay30Seconds to wait after secret creation before proceeding.

Group 7 — Backup & Recovery

VariableDefaultDescription
backup_schedule0 2 * * *Automated backup cron (UTC).
backup_retention_days7Retention; raise to 30–90 for production/compliance.
enable_backup_import / backup_source / backup_urirestore optionsRestore from a backup on deploy.

Group 8 — CI/CD & GitHub Integration

Standard App_CloudRun Cloud Build / Cloud Deploy integration — see App_CloudRun. Key inputs: enable_cicd_trigger, github_repository_url, github_token, enable_cloud_deploy.

Group 9 — Custom SQL Scripts

enable_custom_sql_scripts, custom_sql_scripts_bucket, custom_sql_scripts_path, custom_sql_scripts_use_root — run SQL from a GCS bucket after provisioning. Use this to install additional PostgreSQL extensions or seed data. See App_CloudRun.

Group 10 — Security, Storage & Images

VariableDefaultDescription
enable_cloud_armorfalseAttach a Cloud Armor WAF + HTTPS load balancer.
application_domains[]Custom domain names; also activates MOODLE_REVERSE_PROXY = "true".
enable_cdnfalseEnable Cloud CDN on the load balancer.
create_cloud_storagetrueProvision the data bucket.
storage_buckets[{ name_suffix = "data" }]Additional GCS buckets to provision.

Group 11 — Filesystem (NFS) & Cloud Storage

VariableDefaultDescription
enable_nfstrueShared Filestore volume for Moodle moodledata (keep enabled — required for all deployments).
nfs_mount_path/mnt/nfsContainer path where the NFS share is mounted; injected as MOODLE_DATA_DIR.
gcs_volumes[]GCS Fuse volume mounts for themes or plugins.

Group 12 — Database Backend

VariableDefaultDescription
database_typePOSTGRES_15Fixed at PostgreSQL 15 — do not change.
db_namemoodleDatabase name. Immutable after first deploy.
db_usermoodleApplication database user. Immutable after first deploy.
database_password_length32Generated password length.
enable_auto_password_rotationfalseZero-downtime DB password rotation.

Group 13 — Jobs

VariableDefaultDescription
initialization_jobs[]Leave empty to use the built-in db-init and nfs-init jobs.
cron_jobs[]Supplemental Cloud Run Jobs triggered by Cloud Scheduler (the Moodle cron scheduler job is always created separately).

Group 14 — Observability & Health

VariableDefaultDescription
startup_probeHTTP /health.php, 20 failures × 30 sUp to 10 minutes for Moodle to complete first-boot setup.
liveness_probeHTTP /health.php, 120 s initial delayPeriodic health check after startup.
uptime_check_configdisabled, path /Optional Cloud Monitoring uptime check.
alert_policies[]Optional metric alert policies.

Group 21 — Redis Cache

VariableDefaultDescription
enable_redistrueUse Redis for PHP sessions and Moodle application cache.
redis_host""Leave empty to use the NFS host IP (development only); set to a Cloud Memorystore IP for production.
redis_port6379Redis port (string type).
redis_auth""Optional Redis auth password (sensitive).

Group 22 — VPC Service Controls & Audit Logging

VariableDefaultDescription
enable_vpc_scfalseEnforce a VPC-SC perimeter (requires organization_id).
vpc_cidr_ranges / vpc_sc_dry_run(set)Access level CIDRs / dry-run mode.
enable_audit_loggingfalseDetailed Cloud Audit Logs.

5. Outputs

These values are returned on a successful deployment and are the quickest way to locate and explore the running resources.

OutputDescription
service_nameCloud Run service name.
service_urlDefault run.app URL to reach Moodle.
service_locationCloud Run service region.
stage_servicesURLs of stage-specific service revisions (when Cloud Deploy is enabled).
load_balancer_ipExternal load balancer IP (when Cloud Armor + custom domain is enabled).
load_balancer_urlHTTPS URL via the load balancer.
database_instance_nameCloud SQL instance name.
database_nameApplication database name.
database_userApplication database user.
database_password_secretSecret Manager secret holding the DB password.
database_host / database_portDB host (sensitive) / port.
storage_bucketsCreated Cloud Storage buckets.
network_name / network_exists / regionsVPC network, presence, available regions.
nfs_server_ipPrivate IP of the Filestore NFS server (sensitive).
nfs_instance_tagsGCE network tags on the NFS instance.
nfs_mount_pathContainer path where the NFS share is mounted.
nfs_share_pathExport path on the NFS server.
container_image / container_registryDeployed image and Artifact Registry repo.
monitoring_enabled / monitoring_notification_channelsMonitoring status and channels.
uptime_check_namesNames of the Cloud Monitoring uptime checks.
initialization_jobs / nfs_setup_jobNames of the setup and NFS init jobs.
deployment_id / tenant_id / resource_prefixNaming identifiers.
project_id / project_numberProject identifiers.
cicd_enabled / cicd_configurationCI/CD status and details (repo, trigger, registry).
vpc_sc_enabled / vpc_sc_perimeter_name / vpc_sc_dry_run_modeVPC-SC status.
audit_logging_enabled / artifact_registry_cmek_enabledAudit logging and CMEK status.

6. Configuration Pitfalls & Sensible Defaults

Risk: Critical (data loss / outage / security) — High (service degraded) — Medium (cost or partial degradation) — Low (minor).

SettingSensible valueRiskConsequence if wrong
database_typePOSTGRES_15CriticalMoodle requires PostgreSQL; MOODLE_DB_TYPE = "pgsql" is hardcoded — any other engine breaks startup.
enable_nfstrueCriticalWithout shared NFS storage, moodledata is not shared across instances and uploads are lost on restart.
execution_environmentgen2 (default)CriticalNFS volume mounts are not supported in gen1; the service will not start with NFS enabled.
db_name / db_userset onceCriticalImmutable after first deploy; renaming recreates the DB/user and destroys data.
enable_backup_importfalse unless restoringCriticalEnabling without a valid backup_uri fails the import job.
enable_redistrueHighWithout a shared session store, users on multi-instance deployments are logged out on each new instance.
redis_host"" (NFS) or explicitHighNo valid endpoint if Redis is on but NFS is off and no host is set.
memory_limit2GiHighToo little memory causes PHP OOM during course imports or large file uploads.
min_instance_count1 for productionHigh0 causes cold-start delays; the Cloud Scheduler cron job may time out waiting for the first request to warm the instance.
nfs_mount_path/mnt/nfsHighMust match MOODLE_DATA_DIR; changing after first deploy moves the data root and breaks the installation.
application_domains + MOODLE_REVERSE_PROXYset togetherHighWithout the reverse proxy flags, Moodle generates HTTP URLs behind an HTTPS load balancer, breaking links and logins.
enable_cloud_armor / enable_iapenable for admin-facingMediumThe admin UI is otherwise publicly reachable.
backup_retention_days7 (raise for prod)MediumToo short for compliance retention.
max_revisions_to_retain7LowUnlimited retained revisions can accumulate over time.

For the foundation behaviour referenced throughout — service identity, ingress and load balancing, scaling and concurrency, CI/CD, Cloud Armor, IAP, Binary Authorization, VPC-SC, backups, and image lifecycle — see App_CloudRun. Moodle-specific shared configuration is described in Moodle_Common.