Functions Quickstart

Functions are blocks of code that run on demand without the need to manage any infrastructure. Develop on your local machine, test your code from the command line (using doctl), then deploy to App Platform — no servers required. Learn more about functions.


You can start writing a function using either the doctl command line tool or our control panel. The control panel is great for experimentation, but a command line workflow is the intended route for in-depth development of functions.

Make sure you have doctl installed, authorized to access your DigitalOcean account, and set up with the serverless extension. See How To Install and Configure doctl for instructions.

Run the serverless status subcommand to verify that you’re properly set up to create and deploy functions:

doctl serverless status

If you receive output about the namespace you’re connected to, you’re all set:

Connected to function namespace 'fn-feb132ee-706a-4f13-9c81-f24a3330260b' on API host 'https://faas-nyc1-78edc.doserverless.co'

If you receive an error, be sure you completed all the steps of How To Install and Configure doctl and try again before continuing.

Create a Local Function Directory

On your local machine, navigate to the directory where you’d like to put your function code, then use the serverless init subcommand to initialize a sample project. The -l or --language flag specifies which programming language the sample project should use. The options are go, javascript, php, and python.

Create a sample project in the language of your choice:

doctl serverless init --language js <example-project>

Be sure to replace <example-project> with your project name. A directory will be created with sample code and configuration files:

A local sandbox area 'example-project' was created for you.

The directory will have a project.yml file in it, as well as a packages directory containing the sample package, the hello function directory, and the sample “hello world” function code:

example-project/
├── packages
│   └── sample
│       └── hello
│           └── hello.js
└── project.yml

Next we’ll deploy the function.

Deploy a Function

Use serverless deploy to deploy the sample “hello world” function. The subcommand takes one argument, a path to the project directory you just created.

Note
The serverless deploy command overwrites previously deployed functions in a namespace if their project/function names match the new deploy. This occurs even if the functions were created in separate projects.

Run the deploy command now, being sure to substitute your own project name for <example-project>:

doctl serverless deploy <example-project>

The command will output information about the deployment:

Deploying '/home/sammy/example-project'
  to namespace 'fn-feb132ee-706a-4f13-9c81-f24a3330260b'
  on host 'https://faas-nyc1-78edc.doserverless.co'
Deployment status recorded in 'example-project/.nimbella'

Deployed functions ('doctl sbx fn get <funcName> --url' for URL):
  - sample/hello

The function is now deployed to the cloud as sample/hello.

Invoke a Function

Deployed functions can be invoked using the serverless functions invoke command:

doctl serverless functions invoke sample/hello

The function will return a JSON response:

{
  "body": "Hello stranger!"
}

The sample function accepts a name parameter to customize the greeting. You can add key:value parameters to serverless functions invoke using the -p flag:

doctl serverless functions invoke sample/hello -p name:sammy

The response will be customized based on your input:

{
  "body": "Hello sammy!"
}

Destroy a Function

Use the serverless undeploy subcommand to remove functions from the cloud:

doctl serverless undeploy sample/hello

The command will verify that the process was successful:

The requested resources have been undeployed

Your function is now undeployed from Functions.

Create a Function

  1. Go to https://cloud.digitalocean.com/functions and click Launch.
  2. Click the Actions button and select Create Function
  3. The New Function dialog will pop up. Select the programming language runtime you’d like to use under Runtime, then type the name of your function in Function Name. Package Name is optional.
  4. Click Create to create the function.
  5. You will be taken to the Source tab, where you can run and edit a sample “Hello World” function written in the language you chose.
  6. Press the Run button to run the function. The function output will appear in the Output area below the code, and any logging information will go to the Logs section.

Destroy a Function

  1. Go to https://cloud.digitalocean.com/functions and click Launch.

  2. If you have any functions, they’ll be listed by name on the Overview tab:

    Screenshot of the Functions Overview page, with two functions listed in a table
  3. Click the “more menu” button to the right of the function you’d like to destroy, then choose Destroy Function.

  4. You will be prompted to confirm the action. Enter the function name to confirm, the press Destroy.

The Functions development environment enables you to connect your local workspace to the cloud. Using the doctl CLI command, you can deploy functions from your development machine to the Functions cloud, then test and iterate before deploying as an App Platform component.