How to Set Up Functions on DigitalOcean App Platform
Add Functions as serverless components to an existing DigitalOcean App Platform app via Control Panel by selecting source code, configuring name/routes/envs, and deploying; structure code in packages dir for auto-detection. Use CLI/API for automation. Test via app URL with curl; troubleshoot builds by verifying source dir and specs.
Prerequisites
- DigitalOcean account with billing enabled
- Existing App Platform app or Git repository/container image
- Functions code structured in
packagesdirectory (e.g., Node.js handlers) - Repo read permissions for DigitalOcean
- Optional:
doctlCLI or API token
Step-by-Step Instructions
Create or Select Existing App
packages directory (e.g., packages/functions-name/index.js exporting module.exports = (event, context) => {...}).Navigate to App Overview
Add Function Component
Select Deployment Source
main; enable auto-redeploy), set Source directory for monorepos (e.g., /functions). Default: root (/).Configure Function Settings
api-functions). Confirm Branch and auto-redeploy. Set optional HTTP request routes (e.g., /api/*; defaults to root). Choose Instance size (default: 0.25 vCPU, 512 MiB RAM); edit for autoscaling (min:1, max:4; CPU:70%). Add Environment variables via Envs (e.g., API_KEY as SECRET, RUN_AND_BUILD_TIME).Review and Deploy
DEPLOYMENT_FAILED, DOMAIN_FAILED). Click Add resources to deploy automatically; monitor on Overview or Activity tab.Verify Deployment
https://your-app.ondigitalocean.app/api/endpoint). Test with curl: curl -X POST https://your-app.ondigitalocean.app/api/login \
-H "Content-Type: application/json" \
-d '{"username":"test","password":"test"}' Expect 200 OK JSON response.Edit Post-Deployment Settings
node index.js).Structure Code for Detection
packages/ dir: each subdir is a package with handler files (e.g., packages/api/index.js). Include package.json for dependencies. Supports Node.js, Python, etc.Common Issues & Troubleshooting
Build/Deployment Failure ("DEPLOYMENT_FAILED" or "Build failed" logs)
Verify source directory (e.g., set source_dir: /functions for monorepos) and Functions structure in packages/; check build logs.
Functions not detected
Ensure code exports handlers correctly (e.g., module.exports = async (event) => {...}) in packages/ dir with package.json.
Port binding or runtime errors
Bind to 0.0.0.0 if custom; verify env vars and specs.
No auto-redeploy on Git push
Enable in branch settings; confirm repo permissions granted to DigitalOcean.
Incorrect URL/routing
Set HTTP routes (e.g., /api/*); URL follows app_url/component_route/package/function.