title | description | ms.topic | ms.date | ROBOTS |
---|---|---|---|---|
Create a Java function using Visual Studio Code - Azure Functions |
Learn how to create a Java function, then publish the local project to serverless hosting in Azure Functions using the Azure Functions extension in Visual Studio Code. |
quickstart |
11/03/2020 |
NOINDEX,NOFOLLOW |
[!INCLUDE functions-language-selector-quickstart-vs-code]
Use Visual Studio Code to create a Java function that responds to HTTP requests. Test the code locally, then deploy it to the serverless environment of Azure Functions.
Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.
If Visual Studio Code isn't your preferred development tool, check out our similar tutorials for Java developers using Maven, Gradle and IntelliJ IDEA.
Before you get started, make sure you have the following requirements in place:
-
An Azure account with an active subscription. Create an account for free.
-
The Java Developer Kit, version 8 or 11.
-
Apache Maven, version 3.0 or above.
-
Visual Studio Code on one of the supported platforms.
-
The Azure Functions extension for Visual Studio Code.
-
Choose the Azure icon in the Activity bar, then in the Azure: Functions area, select the Create new project... icon.
-
Choose a directory location for your project workspace then choose Select.
-
Provide the following information at the prompts:
-
Select a language for your function project: Choose
Java
. -
Select a version of Java: Choose
Java 8
orJava 11
, the Java version on which your functions run in Azure. Choose a Java version that you've verified locally. -
Provide a group ID: Choose
com.function
. -
Provide an artifact ID: Choose
myFunction
. -
Provide a version: Choose
1.0-SNAPSHOT
. -
Provide a package name: Choose
com.function
. -
Provide an app name: Choose
myFunction-12345
. -
Authorization level: Choose
Anonymous
, which enables anyone to call your function endpoint. -
Select how you would like to open your project: Choose
Add to workspace
.
-
Can't create a function project?
The most common issues to resolve when creating a local Functions project are:
- You do not have the Azure Functions extension installed.
-
Press F5 to start the function app project.
-
In the Terminal, see the URL endpoint of your function running locally.
-
With Core Tools running, go to the Azure: Functions area. Under Functions, expand Local Project > Functions. Right-click (Windows) or Ctrl - click (macOS) the
HttpExample
function and choose Execute Function Now....:::image type="content" source="../../includes/media/functions-run-function-test-local-vs-code/execute-function-now.png" alt-text="Execute function now from Visual Studio Code":::
-
In Enter request body you see the request message body value of
{ "name": "Azure" }
. Press Enter to send this request message to your function. -
When the function executes locally and returns a response, a notification is raised in Visual Studio Code. Information about the function execution is shown in Terminal panel.
-
Press Ctrl + C to stop Core Tools and disconnect the debugger.
Can't run the function locally?
The most common issues to resolve when running a local Functions project are:
- You do not have the Core Tools installed.
- If you have trouble running on Windows, make sure that the default terminal shell for Visual Studio Code isn't set to WSL Bash.
To publish your app, sign in to Azure. If you're already signed in, go to the next section.
-
Choose the Azure icon in the Activity bar, then in the Azure: Functions area, choose Sign in to Azure....
-
When prompted in the browser, choose your Azure account and sign in using your Azure account credentials.
-
After you've successfully signed in, close the new browser window and go back to Visual Studio Code.
Your first deployment of your code includes creating a Function resource in your Azure subscription.
-
Choose the Azure icon in the Activity bar, then in the Azure: Functions area, choose the Deploy to function app... button.
-
Provide the following information at the prompts:
-
Select folder: Choose the folder that contains your function app.
-
Select subscription: Choose the subscription to use. You won't see this if you only have one subscription.
-
Select Function App in Azure: Choose
Create new Function App
. -
Enter a globally unique name for the function app: Type a name that is unique across Azure in a URL path. The name you type is validated to ensure global uniqueness.
- Select a location for new resources: For better performance, choose a region near you.
-
-
A notification is displayed after your function app is created and the deployment package is applied. Select View Output to see the creation and deployment results.
Can't publish the function?
This section created the Azure resources and deployed your local code to the Function app. If that didn't succeed:
- Review the Output for error information. The bell icon in the lower right corner is another way to view the output.
- Did you publish to an existing function app? That action overwrites the content of that app in Azure.
What resources were created?
When completed, the following Azure resources are created in your subscription, using names based on your function app name:
- Resource group: A resource group is a logical container for related resources in the same region.
- Azure Storage account: A Storage resource maintains state and other information about your project.
- Consumption plan: A consumption plan defines the underlying host for your serverless function app.
- Function app: A function app provides the environment for executing your function code and group functions as a logical unit.
- Application Insights: Application Insights tracks usage of your serverless function.
-
Back in the Azure: Functions area in the side bar, expand your subscription, your new function app, and Functions. Right-click (Windows) or Ctrl - click (macOS) the
HttpExample
function and choose Execute Function Now....:::image type="content" source="../../includes/media/functions-vs-code-run-remote/execute-function-now.png" alt-text="Execute function now in Azure from Visual Studio Code":::
-
In Enter request body you see the request message body value of
{ "name": "Azure" }
. Press Enter to send this request message to your function. -
When the function executes in Azure and returns a response, a notification is raised in Visual Studio Code.
If you don't plan to continue to the next step, delete the function app and its resources to avoid incurring any further costs.
- In Visual Studio Code, select the Azure icon in the Activity bar, then select the Functions area in the side bar.
- Select the function app, then right-click and select Delete Function app....
Expand the function by adding an output binding. This binding writes the string from the HTTP request to a message in an Azure Queue Storage queue.
[!div class="nextstepaction"] Connect to an Azure Storage queue