App Platform is a Platform-as-a-Service (PaaS) offering that allows developers to publish code directly to DigitalOcean servers without worrying about the underlying infrastructure.
Functions are blocks of code that run on demand without the need to manage any infrastructure. You can develop on your local machine, test your code from the command line using doctl
, then deploy to App Platform — no servers required.
You can add a function to an app using the CLI’s app update command or the API’s app update endpoint. To add a function, update the app’s spec with the function’s specifications and submit the spec using the following command or endpoint.
Select the repository service to retrieve your source code from, either: GitHub, GitLab, DigitalOcean Container Registry, Docker Hub, or a sample app. If you are connecting to your repository from DigitalOcean for the first time, you will need to click the Manage Access button to provide DigitalOcean access to the repositories or containers, then refresh this page.
Select the repository from the Repository drop-down list. Next, select which branch of the source repo you’d like to use as the source. Optionally, for monorepos, specify the folder that contains the source in the Source Directory field. For containers, specify the image tag to use. Click Next.
App Platform will inspect the code, select an appropriate resource type and also detect any appropriate build and run commands.
You cannot change a Function to another resource type. To change the Function specifications, click the edit icon to open the Settings page of the Function. Here, you can edit the name, and the request route. Click Expand in the Functions section to see the packages and functions to be deployed. Click Save to confirm your changes.
Optionally, to specify additional resources, click the Add Additional Resource From Source link, repeat the procedure, and click Next.
To use environment variables, click Edit to the right of the resource name and enter them. Click the Encrypt checkbox to obscure a variable’s value in all build, deploy, and application logs.
To specify an app-level environment variable, click Edit to the right of Global. Click Save and then click Next.
For Functions components only, you must also add the environment variables to your function’s project.yml
configuration file. The project.yml
file must be in the component’s Source Directory, if specified in step 3.
See the Templating section of the Functions project configuration documentation for examples and more information on configuring App Platform environment variables in project.yml
.
Also see How to Use Environment Variables in App Platform for more information on setting and using environment variables in App Platform.
Review the resources, environment variables, and pricing, and click Create Resources.
In the Apps section of the DigitalOcean Control Panel, select your app and click on the Settings tab. Click the Function you’d like to edit.
You can use the configuration settings you see here to change the Function’s scaling behavior, modify environment variables, edit commands, and more.
To find the live URL of each function in your Function component, click to the Settings tab of your app and click the Function component you’d like to inspect.
The Functions table displays information about all of the functions deployed in the component. The Name of each function is a clickable link that opens the function’s URL in a new window in your browser.
curl
, or your programming language’s HTTP client library to test the URL with properly formatted HTTP requests.You can also click the Copy button next to the function name to copy the live URL to your clipboard.
Functions in App Platform supports trusted sources with DigitalOcean Managed Databases. You can add your app as a trusted source on managed database clusters to enable secure connection from the app to the database.
When you add an app as a trusted source for a database, all components of the app, including any functions components, have secure access to the database cluster.
To configure the functions component to connect to the database cluster, set environment variables from the managed database configuration:
DATABASE_URL
for the connection URL.DATABASE_PASSWORD
for the password.DATABASE_USER
for the username to use to connect.DATABASE_NAME
for the name of the database cluster.When using functions to connect to databases, we recommend reusing the database connection across multiple function invocations or closing the connection with every invocation to avoid hitting the database’s connection limit. The function environment is reused across function invocations, so you can initialize the connection in a persistent global variable.
We do not currently support trusted sources with MongoDB or Postgres connection pools.
Functions components on App Platform can forward console and error logs to a third-party logging service. We currently support Logtail and Datadog:
Enabling log forwarding requires two steps:
project.yml
file to properly handle the LOG_DESTINATIONS
environment variable.You can configure log forwarding for function components using the control panel interface or by updating your app’s app spec file.
To configure a function component’s log forwarding through the control panel, you need to navigate to the component’s Settings page. Start at the Apps page, then click on the App you’d like to update. Select Settings and click on the function component.
Click the Edit button in the Log Forwarding section, then press the Add Destination button. This opens the Add Log Destination dialog:
Fill out a Name, select your Log Destination Type, and then fill in the details needed for your destination type. Press Add Log Destination to complete the process.
Adding a log forwarding destination to your function component also adds an environment variable to your component with the key of LOG_DESTINATIONS
. You can view this new environment variable in the Environment Variables section of your function component’s settings page. If you remove this environment variable, log forwarding for your function stops working.
You can add log forwarding destinations to function components by updating the App’s app spec file. See the log_destinations
section of the App Spec reference for details. Here is an example Logtail destination:
log_destinations:
- name: <example-destination>
logtail:
token: <your-token>
Place the log forwarding destination in the portion of your app spec that configures the function component you’re enabling log forwarding for.
Additionally, in the same component section, add the following environment variable:
envs:
- key: LOG_DESTINATIONS
scope: RUN_AND_BUILD_TIME
value: ${_self.FUNCTIONS_LOG_DESTINATION_JSON}
The value of ${_self.FUNCTIONS_LOG_DESTINATION_JSON}
is provided by App Platform. It is a JSON structure with configuration details for log forwarding destinations.
project.yml
FileTo complete the setup, add the LOG_DESTINATIONS
environment variable to your function’s project.yml
file:
environment:
LOG_DESTINATIONS: ${LOG_DESTINATIONS}
This passes the component-level LOG_DESTINATIONS
environment variable to the actual Function process. After you update project.yml
, redeploy your function component.
In the Apps section of the DigitalOcean Control Panel, select your app and click on the Settings tab. Click on the Function you’d like to destroy.
You can destroy the Function by clicking the Destroy Component button on the bottom of the page and entering its name to confirm your selection.
All Functions product limits also apply to function components in App Platform. See the following link for details.