Azure Functions Configuration in Azure - microsoft-campus-community/workshop-shopping-list-bot GitHub Wiki

Prerequisite: This page assumes that you have a Cosmos DB up and running in Azure. If you do not have one yet, follow the steps to create one first. It also assumes that you have an Azure Function App (page how to create Function App in Azure) running in the cloud.

The Azure Functions use Cosmos DB to persist the items in shopping lists. Now that you have the Azure Functions and a Cosmos DB running in the cloud, it is time to connect both. Otherwise, how is the Azure Function supposed to know where to find the Cosmos DB? Yes, you might have already configured the connection from the Azure Functions to the Cosmos DB locally. However, remember that you never want to check in connection strings to GitHub, so the connection information is lost from your local setup to deployment. It needs to be configured in the cloud again.

Get the Connection String in Azure

  1. Open the Azure Portal.
  2. Navigate to the resource group you use for this workshop and open the Azure Cosmos DB account in it. Screenshot of Azure Portal with resource group for this workshop open and red arrow pointing to 'Azure Cosmos DB account' resource
  3. Open the 'Quick start' page, navigate to the 'Node.js' tab and copy the PRIMARY CONNECTION STRING to your clipboard. Screenshot of Azure Portal with 'Quick start' page of a Cosmos DB resource open and red circle around copy to clipboard button for 'PRIMARY CONNECTION STRING'.

Set Connection String in Function App in Azure

  1. Open the Azure Portal.
  2. Navigate to the resource group you use for this workshop and open the resource of type Function App in it.
  3. In the navigation on the left side, go to Configuration below 'Settings'. Screenshot of Azure Portal with Function App open and a red arrow pointing to 'Configuration' in left side navigation.
  4. Click '+ New Application setting' under the 'Application settings' section to create a new environment variable available to the Functions running in Azure. Screenshot of Azure Portal with Configuration page of Function App open and red circle around '+ New application setting' button in 'Application settings' section.
  5. In the 'Add/Edit application setting' prompt, you will define the environment variable the Azure Functions can use to connect to the Cosmos DB.
    • As 'Name' type in SHOPPING_LIST_COSMOSDB, which is how the connection string is accessed in the code for the Azure Functions (process.env.SHOPPING_LIST_COSMOSDB).
    • For the value, paste the Cosmos DB connection string you retrieved in the Get the Connection String in Azure paragraph.
    • Click the 'OK' button to add the application setting. Screenshot of Azure Portal with Function App 'Add/Edit application setting' open, 'Name' and 'Value' text fields filled out and red circle around 'OK' button.
  6. Now that the added application setting shows up in the list, you should not forget to click 'Save'. When prompted that the Function App will restart, click 'continue'. This will make the new application setting available to your Function App. Screenshot of Azure Portal with Function App 'Configuration' page open and red circle around 'Save' button.

At this point, your Azure Functions running in the cloud have the environment variable with the Cosmos DB connection string available. The shopping list API is now fully functional.

NEXT: Use the CI Pipeline for your Azure Function