Deploying Java Function in Azure that uses Azure CLI
Configure an Azure function, Azure agent and deploy the functions by using Azure CLI and Visual Studio Code Editor.
Before you begin
- Downloaded the artifact corresponding to the Azure API Management gateway Function-as-a-Service agent component from the Download resources page.
- Federated API management, version 11.1.7
- An active Azure account with Azure API Management service subscription.
- Verified whether Azure API Management service and federated API management for which you want to establish connectivity by using the agent are up and running.
- Created a Service principle in Azure with the following roles assigned:
- Cloud Application Administrator (for running stand-alone spring boot application and authenticating Azure SDK)
- Assigned the website Contributor and API Management Service Reader roles at the subscription level. For details about assigning roles, see Azure documentation. The documentation explains how to assign the role at the Resource group level. Follow the steps and assign the website Contributor role at the subscription level.
-
Assigned the Service principle to your Azure API Management Service and the client ID and client secret is generated. The generated credentials must be added to the following Environment variables, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET. To assign the Service principle, use the Azure portal or Azure CLI. For details, see Using Azure Portal, Using Azure CLI.
- Installed Microsoft Azure CLI, latest version. For details, see Azure Command-Line Interface (CLI).
- Installed Node.js, latest version. (Node.js is required for packaging and deploying the agent code in Azure Functions.).
- Installed Gradle 7.4.2.
Create Functions
- Search for Azure Function App in the Search bar in the Azure services console.
- Click Create.
- Select Consumption as the hosting option.
- Click Select.
- Edit the following details under the Basics tab in the
Instance Details section:
- Function App Name. Enter a name that describes the purpose of your function. For example, azureagent
- Runtime stack. Choose Java as the language to write your function.
- Version. Choose 17.0 as the version.
- Operating System. Choose Windows.
- Click Review + Create.
If you require extra configurations such as Storage, Networking, you can navigate to the respective sections and configure.
-
Click Create.
This step takes a few minutes to create the Function, azureagent.
Once created, azureagent is listed in the Function App.
Configure the Environment variables
Environment variables include Azure API Management service, agent, and federated API management configurations. These configurations are required to connect Azure API Management Service with federated API management.
- Open azureagent in the Function App.
- Click Settings > Environment variables from the left navigation pane.
- Click Add and specify the following in the Add/edit
application settings page:
- Name. Enter the name of the environment variable.
- Value. Specify a value for the property.
-
Click Apply.
For details about the environment variables and recommended values, see Environment variables applicable for Azure API Management Service.
- Click Apply in the Environment variables page.
The next step is to deploy your Azure agent code into Azure Functions.
Deploying Azure agent code into Azure Functions by using Azure CLI
You can deploy by using Azure CLI.
- Open Command Prompt.
- Run the following command to verify whether Azure CLI is installed:
az –-versionIf Azure CLI version is listed, it indicates that Azure CLI is installed successfully.
- Run the following command to logon to your Azure account.
az login - Enter valid Azure account credentials in which, your Azure API Management service (that you want to connect with federated API management) is hosted. If you have logged on to your Azure account successfully, all the subscriptions under that account display in an order.
- Type the number of the subscription that you want to deploy the SDK agent code in Azure
Functions.
Azure CLI authorizes the Azure account.
- Run the following command to verify whether node is installed in your system:
node –-versionIf node version is listed, it indicates that the node is installed successfully. Else, reinstall the node.
- Run the following command to install the NPM package, which is required to package and deploy
the agent code in Azure Functions:
npm install -g azure-functions-core-tools@4 --unsafe-perm truewhere, Azure-functions-core-tools@4 is the NPM package.Note: Make sure to install the latest NPM package from NPM registry. - Click Download resources and download the artifact corresponding to the Azure API Management gateway Function-as-a-Service agent component.
- Open command prompt and run the following command:
func azure functionapp publish <functions-app-name> --javaWhere, functions-app-name is the name of the Functions App that you created.
For example: func Azure functionapp publishes azureagent –java. This step deploys the artifacts in Azure Functions App, azureagent.
Configuring event-based trigger for AssetsHandler function by using Azure Subscription service
- Go to the Home page in your Azure account.
- Search for Subscriptions in the Search bar in the Azure services console.
- Select the subscription name in which your Azure API Management service is hosted.
- Click Resource providers from the left navigation pane.
- Select Microsoft.EventGrid from the dropdown.
- Enable Microsoft.EventGrid and click Register.
Configure the Event subscription in your Azure API Management Service
- Open the Azure API Management Service from which you want to establish a connection to federated API management.
- Click Events.
- Click + Event Subscription.
- The Create Event Subscription page appears.
- Enter the following details under the Basics tab in the Create
Event Subscription page:
- Name. Name of the event subscription.
- Event Types. Select the following in the Filter to Event Types checkbox.
- API Management Service Created
- API Management Service Updated
- API Management Service Deleted Each time that the corresponding activity occurs in the Azure API Management service. An event is created that triggers the AssetsHandler function.
- Endpoint Type. Select Azure Function.
- Endpoint. Associate this event subscription with the Azure Function, azureagent.
Edit the following fields as required in the Select Azure Function screen:
- Subscription. Select the subscription in which your Azure account is hosted.
- Resource group. Select the corresponding resource group name.
- Function app. Select the Function App, to which the event subscription must associate with. For example, azureagent.
- Function. Choose the function as AssetHandler.
- Click Confirm Selection.
- Click Create.
This triggers the AssetsHandler function whenever the activities that are chosen in the Event subscription occurs in the Azure API Management Service. When the Functions trigger based on the cron expression and event subscription, the application logics of the respective functions are executed automatically, establishing the connection between the Azure API Management Service and federated API management. Also, if you want to troubleshoot the executions of the Azure functions, the tab within the Azure function enables you to monitor the logs and the Metrics section in your Azure API Management Service enables you to analyze all the metrics.
- Verify whether Azure API Management Service (runtime) is registered with the federated API management
and if the APIs and metrics are published and synchronized with federated API management respectively.
- Open the federated API management application.
- Click the Runtimes tab.
- Check whether the runtime is registered with federated API management by verifying whether the AZURE_API_MANAGEMENT_SERVICE_NAME (specified in the .env file) is listed in the Runtimes tab of the Catalog page. If the runtime is listed, it indicates that the runtime is successfully registered with the federated API management.
- Check whether the runtime's health status is sent to federated API management by verifying the status of that corresponding runtime in the Status column. The status appears green only if the runtime is up and the heartbeats (status) are received successfully by federated API management.
- Check whether the APIs are published from the runtime to federated API management by clicking Action menu > Details of the corresponding runtime that you want to verify whether its APIs are published to federated API management.
- Click Relations tab.
A list of all the APIs associated with the runtime appears. You can also view the runtimes and its associated APIs in the APIs tab of the Catalog page.
- Check whether the API metrics are synchronized from the runtime to federated API management by
clicking the
icon under the Action column corresponding to the runtime for which, you want to verify whether the metrics are published to federated API management.
- Click Insights tab.
The Runtime-specific monitor page renders the metrics of that runtime pertaining to a specified time interval. It lists the metrics such as Transactions, Error rate, Availability, Response time. For details, see Monitoring performance of runtimes in federated API management.