How to Manage Functions in App Platform

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.

Create Functions

  1. In the Apps section of the DigitalOcean Control Panel, select your app, then click the Create dropdown and choose Create Resources From Source Code to add a Function.
Resources screen
  1. 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.

  2. 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.

Create function

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.

Create function settings
  1. Optionally, if you want to specify additional resources, click the Add Additional Resource From Source link, repeat the procedure and click Next.

  2. If you need to use environment variables, click Edit to the right of the resource name and supply them. Click the Encrypt checkbox to ensure a variable’s value is obscured 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.

    Resources env variables
  3. Review the resources, environment variables and pricing, and click Create Resources.

Edit Functions

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.

Enable Trusted Sources

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. We also do not support trusted sources with the default Node.js runtime, but as a workaround, you can use the alternative Node.js runtime called nodejs-lambda:14.

Log Forwarding

Functions can forward console and error logs to a third-party logging service. We currently support Logtail and Datadog. For Logtail, create an HTTP source and use the generated token for the configuration.

To configure your function component to forward logs, set an environment variable called FUNCTIONS_LOG_DESTINATION_JSON containing the JSON-marshaled version of the log_destinations field in the app spec. To configure the log destinations manually, you need to construct the JSON manually.

Here is an example with one service:

 LOG_DESTINATIONS='[{datadog: {endpoint: "", api_key: "YOUR_API_KEY"}}]'

We do not currently support the syslog sources from Logtail. As a workaround, for Logtail, create an HTTP source and use the generated token for the configuration.

Destroy Functions

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.