|
| 1 | +--- |
| 2 | +title: Configure application settings for Azure Static Web Apps |
| 3 | +description: Learn to configure application settings for Azure Static Web Apps |
| 4 | +services: static-web-apps |
| 5 | +author: burkeholland |
| 6 | +ms.service: static-web-apps |
| 7 | +ms.topic: how-to |
| 8 | +ms.date: 05/08/2020 |
| 9 | +ms.author: buhollan |
| 10 | +--- |
| 11 | + |
| 12 | +# Configure application settings for Azure Static Web Apps |
| 13 | + |
| 14 | +Application settings hold configuration settings for values that may change, such as database connection strings. Adding application settings allows you to modify the configuration input to your app, without having to change application code. |
| 15 | + |
| 16 | +Application settings are: |
| 17 | + |
| 18 | +- Encrypted at rest |
| 19 | +- Copied to [staging](review-publish-pull-requests.md) and production environments |
| 20 | + |
| 21 | +Application settings are also sometimes referred to as environment variables. |
| 22 | + |
| 23 | +> [!IMPORTANT] |
| 24 | +> The application settings described in this article only apply to the backend API of an Azure Static Web App. |
| 25 | +> |
| 26 | +> For information on using environment variables with your frontend web application, see the docs for your [JavaScript framework](#javascript-frameworks-and-libraries) or [Static site generator](#static-site-generators). |
| 27 | +
|
| 28 | +## Prerequisites |
| 29 | + |
| 30 | +- An Azure Static Web Apps application |
| 31 | +- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest) |
| 32 | + |
| 33 | +## Types of application settings |
| 34 | + |
| 35 | +There are typically two aspects to an Azure Static Web Apps application. The first is the web application, or static content, which is represented by HTML, CSS, JavaScript, and images. The second is the back-end API, which is powered by an Azure Functions application. |
| 36 | + |
| 37 | +This article demonstrates how to manage application settings for the back-end API in Azure Functions. |
| 38 | + |
| 39 | +The application settings described in this article cannot be used or referenced in static web applications. However, many front-end frameworks and static site generators allow the use of environment variables during development. At build time, these variables are replaced by their values in the generated HTML or JavaScript. Since data in HTML and JavaScript is easily discoverable by site visitor, you want to avoid putting sensitive information in the front-end application. Settings that hold sensitive data are best located in the API portion of your application. |
| 40 | + |
| 41 | +For information about how to use environment variables with your JavaScript framework or library, refer to the following articles for more detail. |
| 42 | + |
| 43 | +### JavaScript frameworks and libraries |
| 44 | + |
| 45 | +- [Angular](https://angular.io/guide/build#configuring-application-environments) |
| 46 | +- [React](https://create-react-app.dev/docs/adding-custom-environment-variables/) |
| 47 | +- [Svelte](https://linguinecode.com/post/how-to-add-environment-variables-to-your-svelte-js-app) |
| 48 | +- [Vue](https://cli.vuejs.org/guide/mode-and-env.html) |
| 49 | + |
| 50 | +### Static site generators |
| 51 | + |
| 52 | +- [Gatsby](https://www.gatsbyjs.org/docs/environment-variables/) |
| 53 | +- [Hugo](https://gohugo.io/getting-started/configuration/) |
| 54 | +- [Jekyll](https://jekyllrb.com/docs/configuration/environments/) |
| 55 | + |
| 56 | +## About API App settings |
| 57 | + |
| 58 | +APIs in Azure Static Web Apps are powered by Azure Functions, which allows you to define application settings in the _local.settings.json_ file. This file defines application settings in the `Values` property of the configuration. |
| 59 | + |
| 60 | +The following sample _local.settings.json_ shows how to add a value for the `DATABASE_CONNECTION_STRING`. |
| 61 | + |
| 62 | +```json |
| 63 | +{ |
| 64 | + "IsEncrypted": false, |
| 65 | + "Values": { |
| 66 | + "AzureWebJobsStorage": "", |
| 67 | + "FUNCTIONS_WORKER_RUNTIME": "node", |
| 68 | + "DATABASE_CONNECTION_STRING": "<YOUR_DATABASE_CONNECTION_STRING>" |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Settings defined in the `Values` property can be referenced from code as environment variables, available from the `process.env` object. |
| 74 | + |
| 75 | +```js |
| 76 | +const connectionString = process.env.DATABASE_CONNECTION_STRING; |
| 77 | +``` |
| 78 | + |
| 79 | +The `local.settings.json` file is not tracked by the GitHub repository because sensitive information, like database connection strings, are often included in the file. Since the local settings remain on your machine, you need to manually upload your settings to Azure. |
| 80 | + |
| 81 | +Generally, uploading your settings is done infrequently, and isn't required with every build. |
| 82 | + |
| 83 | +## Uploading application settings |
| 84 | + |
| 85 | +You can configure application settings via the Azure portal or with the Azure CLI. |
| 86 | + |
| 87 | +### Using the Azure portal |
| 88 | + |
| 89 | +The Azure portal provides an interface for creating, updating and deleting application settings. |
| 90 | + |
| 91 | +1. Navigate to the [Azure portal](https://portal.azure.com). |
| 92 | + |
| 93 | +1. In the search bar, search for and select **Static Web Apps**. |
| 94 | + |
| 95 | +1. Click on the **Configuration** option in the sidebar. |
| 96 | + |
| 97 | +1. Select the environment that you want to apply the application settings to. Staging environments are automatically created when a pull request is generated, and are promoted into production once the pull request is merged. You can set application settings per environment. |
| 98 | + |
| 99 | +1. Click on the **Add Button** to add a new app setting. |
| 100 | + |
| 101 | + :::image type="content" source="media/application-settings/configuration.png" alt-text="Azure Static Web Apps configuration view"::: |
| 102 | + |
| 103 | +1. Enter a **Name** and **Value** |
| 104 | + |
| 105 | +1. Click **OK** |
| 106 | + |
| 107 | +### Using the Azure CLI |
| 108 | + |
| 109 | +You can use the `az rest` command to do bulk uploads of your settings to Azure. The command accepts application settings as JSON objects in a parent property called `properties`. |
| 110 | + |
| 111 | +The easiest way to create a JSON file with the appropriate values is to create a modified version of your _local.settings.json_ file. |
| 112 | + |
| 113 | +1. To ensure your new file with sensitive data isn't exposed publicly, add the following entry into your _.gitignore_ file. |
| 114 | + |
| 115 | + ```bash |
| 116 | + local.settings*.json |
| 117 | + ``` |
| 118 | + |
| 119 | +2. Next, make a copy of your _local.settings.json_ file and name it _local.settings.properties.json_. |
| 120 | + |
| 121 | +3. Inside the new file, remove all other data from the file except for the application settings and rename `Values` to `properties`. |
| 122 | + |
| 123 | + Your file should now look similar to the following example: |
| 124 | + |
| 125 | + ```json |
| 126 | + { |
| 127 | + "properties": { |
| 128 | + "DATABASE_CONNECTION_STRING": "<YOUR_DATABASE_CONNECTION_STRING>" |
| 129 | + } |
| 130 | + } |
| 131 | + ``` |
| 132 | + |
| 133 | +The Azure CLI command requires a number of values specific to your account to run the upload. From the _Overview_ window of your Static Web Apps resource, you have access to the following information: |
| 134 | + |
| 135 | +1. Static site name |
| 136 | +2. Resource group name |
| 137 | +3. Subscription ID |
| 138 | + |
| 139 | +:::image type="content" source="media/application-settings/overview.png" alt-text="Azure Static Web Apps Overview"::: |
| 140 | + |
| 141 | +4. From a terminal or command line, execute the following command. Make sure to replace the placeholders of `<YOUR_STATIC_SITE_NAME>`, `<YOUR_RESOURCE_GROUP_NAME>`, and `<YOUR_SUBSCRIPTION_ID>` with your values from the _Overview_ window. |
| 142 | + |
| 143 | + ```bash |
| 144 | + az rest --method put --headers "Content-Type=application/json" --uri "/subscriptions/<YOUR_SUBSCRIPTION_ID>/resourceGroups/<YOUR_RESOURCE_GROUP_NAME>/providers/Microsoft.Web/staticSites/<YOUR_STATIC_SITE_NAME>/config/functionappsettings?api-version=2019-12-01-preview" --body @local.settings.properties.json |
| 145 | + ``` |
| 146 | + |
| 147 | +> [!IMPORTANT] |
| 148 | +> The "local.settings.properties.json" file must be in the same directory where this command is run. This file can be named anything you like. The name is not significant. |
| 149 | +
|
| 150 | +### View application settings with the Azure CLI |
| 151 | + |
| 152 | +Application settings are available to view through the Azure CLI. |
| 153 | + |
| 154 | +1. From a terminal or command line, execute the following command. Make sure to replace the placeholders `<YOUR_SUBSCRIPTION_ID>`, `<YOUR_RESOURCE_GROUP_NAME>`, `<YOUR_STATIC_SITE_NAME>` with your values. |
| 155 | + |
| 156 | + ```bash |
| 157 | + az rest --method post --uri "/subscriptions/<YOUR_SUBSCRIPTION_ID>/resourceGroups/<YOUR_RESOURCE_GROUP_NAME>/providers/Microsoft.Web/staticSites/<YOUR_STATIC_SITE_NAME>/listFunctionAppSettings?api-version=2019-12-01-preview" |
| 158 | + ``` |
| 159 | + |
| 160 | +## Next steps |
| 161 | + |
| 162 | +> [!div class="nextstepaction"] |
| 163 | +> [Setup local development](local-development.md) |
0 commit comments