How to Create Functions in the Development Namespace

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.

The functions development namespace is a single namespace which you can use to test functions before deployment to App Platform. You don’t need to create an App Platform component to write and test functions in the development namespace.

You can also create a simple function directly in the control panel for experimentation. For continued development of production-ready functions, we recommend our command-line workflow.

To create functions from the command line, first make sure you have the latest version of doctl installed, authorized to access your DigitalOcean account, and set up with the serverless extension:

Install and configure doctl, the official DigitalOcean command-line client (CLI).

Initialize a Sample Function

To help you get started developing a function, doctl can initalize a properly structured project directory with sample configuration files and function code.

On your local machine, from the directory where you want to create your project and write your functions, use the serverless init subcommand to initialize a sample project.

The doctl serverless init command creates a directory with sample code and configuration. The -l or --language flag specifies which programming language the sample project should use. You can see the available langauges in the supported runtimes documentation or by running doctl serverless status --languages.

On your local machine, from the directory where you want to create your project and write your functions, use the serverless init subcommand to initialize a sample project. This command creates a JavaScript project called example-project, but you can choose a different runtime and project name:

doctl serverless init --language js example-project

The output confirms that the project was initialized:

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

Once initialized, the project directory contians a project.yml configuration file in it, a packages directory containing the sample package, a hello function directory, and the sample “Hello world” function code:

├── packages
│   └── sample
│       └── hello
│           └── hello.js
└── project.yml

Once you create a project with at least one function, you can deploy it to the development namespace to run and test it.

Deploy a Function in the Development Namespace

The development namespace is a single namespace designed for testing functions before deploying them to App Platform.

The doctl serverless deploy command deploys code to the development namespace. The command takes one argument: a path to the project directory you just created.

Run the deploy command, substituting your own project name for example-project:

doctl serverless deploy example-project

The command outputs information about the deployment:

Deploying '/home/sammy/example-project'
  to namespace 'fn-feb132ee-706a-4f13-9c81-f24a3330260b'
  on host ''
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.

You can create single-file functions in the development namespace directly from the DigitalOcean Control Panel in your browser. The functions source editor in the control panel is meant to support experimentation and testing for single-file functions without using the command line.

To access the development namespace, from the DigitalOcean Control Panel, click Functions in the left menu to go to the Functions page.

Next, click Launch to go to the Overview page of the development namespace. If you already have functions in the development namespace, they are listed here.

The overview tab of the development namespace with two example functions listed.

To create a new function in the development namespace, click the Actions button in the top right. In the menu that opens, click Create Function to open the New Function window.

The New Function window with runtime, function name, and optional package name fields.

In this window, fill out the following fields:

  • Runtime. This is the runtime environment for your function, like Python or Node.js.

  • Function name. This is name of your function, which determines how it appears in the control panel and the URL at which you can access the function.

  • Optionally, Package Name. Packages are an optional way to organize functions within a namespace. The package also determines the URL at which you can access the function.

After you fill in the fields, click Create. This brings you to the Source tab for the new function, where you can edit the source code of the function, change the input to the function, and run the function to see the output and logs.

To start, the source code of new functions in the development namespace contain a simple “Hello, World!” example.

Iterative Development and Deployment

Once you’ve created a function locally and deployed it to the development namespace, you can test and iterate:

Improve your serverless development with this guidance on invoking, iterating on, and observing DigitalOcean Functions.

Then, when you’re ready, you can deploy your functions to production:

After you create and test a function in the development namespace, use App Platform to deploy to production.