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 a "Functions" permission set in your project with object permissions that your Function will need and 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 Functions project to git and commit changes
Once these steps are done, 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 org, log in.
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 Function 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 Salesforce Functions, and configure the Function to be public. It can take several minutes to deploy 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 Functions.