Deploy a Function

Functions get deployed as part of a DX project. A project can have one or more functions, along with org configurations like permission sets, and Apex code for invocation. The project can also include flows, lightning web components, and other kinds of force app elements. When you deploy your project, you'll deploy your org changes to your org, as usual, but the functions in your project get deployed to a compute environment that you create.

If you haven't deployed to your org before, you'll need to do the following configuration steps before deploying to Salesforce Functions:

  • Connect your Dev Hub org with your Salesforce Functions account (once).
  • Connect your CLI session with your Salesforce org and Functions accounts (once per CLI session).
  • Create scratch or sandbox orgs from your Dev Hub org as needed.
  • Set up the Functions permission set in your project with object permissions that your function needs. Push this permission set to your scratch org or sandbox org (as needed based on what org resources your function needs permission to).
  • Create a compute environment that's associated with the org you're planning to deploy to (once per function and org combination).
  • Add your Salesforce Functions project to git and commit changes.

After completing these steps, you can deploy your function (and Functions permission set changes, and Apex class changes, if needed) as many times as you need to.

If you have not already logged in to your Dev Hub org, log in and set it as your default Dev Hub.

See Enable Functions on Dev Hub Orgs for the steps to enable Functions in your Dev Hub org.

You also need to connect your CLI session to the same Salesforce Functions account you connected your Dev Hub org to. Once you’ve done this, any CLI commands that communicate with Salesforce Functions will use this authenticated session. To connect your CLI session use the following command:

This command opens a browser page where you can log in to your Salesforce Functions account using the same credentials you used to connect your org.

This connection can expire after 8 hours and may need to be re-established by running sf login functions again

A Salesforce Functions compute environment is where your functions get deployed and run. Before you can deploy, you need a compute environment associated with your org. See Create Compute Environment for Org for the steps to create a compute environment associated with your org.

Before you deploy, make sure to commit your latest project changes to git. See Add Project to git for more details on tracking your project changes with git.

To deploy your Salesforce Functions project, first change to the root directory of your DX project. Then use sf deploy functions with the org alias that fits your use case (scratch, sandbox, or production org). Note that the org must be already associated with a compute environment, otherwise the deploy will not succeed.

The deploy command will build and deploy your function to your compute environment and configure the function to be public. It can take several minutes to deploy your functions to Salesforce Functions.

Your function code must be committed to git before you deploy, as the deploy will use the latest committed code from the current git branch.

Initial deployments can take longer because you are uploading the full container image. Be aware of any network bandwidth restrictions (e.g. VPN or network security restrictions) you might have before deploying your functions.