Skip to content

Commit 0ae3ae9

Browse files
author
Jill Grant
authored
Merge pull request #271752 from fred-cardoso/main
Creates a new article regarding the management of environment variables on Container Apps
2 parents 184be56 + 147aadd commit 0ae3ae9

File tree

8 files changed

+153
-1
lines changed

8 files changed

+153
-1
lines changed

articles/container-apps/TOC.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@
111111
href: tutorial-ci-cd-runners-jobs.md
112112
displayName: Deploy Azure DevOps (Azure Pipelines) agents and GitHub Actions runners
113113
- name: Revision management
114-
href: revisions-manage.md
114+
href: revisions-manage.md
115+
- name: Environment Variables
116+
href: environment-variables.md
115117
- name: Use health probes
116118
href: health-probes.md
117119
- name: Storage mounts
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
---
2+
title: Manage environment variables on Azure Container Apps
3+
description: Learn to manage environment variables in Azure Container Apps.
4+
services: container-apps
5+
author: fred-cardoso
6+
ms.service: container-apps
7+
ms.topic: how-to
8+
ms.date: 04/10/2024
9+
ms.author: fredcardoso
10+
---
11+
12+
# Manage environment variables on Azure Container Apps
13+
14+
In Azure Container Apps, you're able to set runtime environment variables. These variables can be set as manually entries or as references to [secrets](manage-secrets.md).
15+
These environment variables are loaded onto your Container App during runtime.
16+
17+
## Configure environment variables
18+
19+
You can configure the Environment Variables upon the creation of the Container App or later by creating a new revision.
20+
21+
### [Azure portal](#tab/portal)
22+
23+
If you're creating a new Container App through the [Azure portal](https://portal.azure.com), you can set up the environment variables on the Container section:
24+
25+
:::image type="content" source="media/environment-variables/creating-a-container-app.png" alt-text="Screenshot of Container App creation page.":::
26+
27+
### [Azure CLI](#tab/cli)
28+
29+
You can create your Container App with enviroment variables using the [az containerapp create](/cli/azure/containerapp#az-containerapp-create) command by passing the environment variables as space-separated 'key=value' entries using the `--env-vars` parameter.
30+
31+
```azurecli
32+
az containerapp create -n my-containerapp -g MyResourceGroup \
33+
--image my-app:v1.0 --environment MyContainerappEnv \
34+
--secrets mysecret=secretvalue1 anothersecret="secret value 2" \
35+
--env-vars GREETING="Hello, world" ANOTHERENV=anotherenv
36+
```
37+
38+
If you want to reference a secret, you have to ensure that the secret you want to reference is already created, see [Manage secrets](manage-secrets.md). You can use the secret name and pass it to the value field but starting with `secretref:`
39+
40+
```azurecli
41+
az containerapp update \
42+
-n <APP NAME>
43+
-g <RESOURCE_GROUP_NAME>
44+
--set-env-vars <VAR_NAME>=secretref:<SECRET_NAME>
45+
```
46+
47+
### [PowerShell](#tab/powershell)
48+
49+
If you want to use PowerShell you have to, first, create an in-memory object called [EnvironmentVar](/dotnet/api/Microsoft.Azure.PowerShell.Cmdlets.App.Models.EnvironmentVar) using the [New-AzContainerAppEnvironmentVarObject](/powershell/module/az.app/new-azcontainerappenvironmentvarobject) PowerShell cmdlet.
50+
51+
To use this cmdlet, you have to pass the name of the environment variable using the `-Name` parameter and the value using the `-Value` parameter, respectively.
52+
53+
```azurepowershell
54+
$envVar = New-AzContainerAppEnvironmentVarObject -Name "envVarName" -Value "envVarvalue"
55+
```
56+
57+
If you want to reference a secret, you have to ensure that the secret you want to reference is already created, see [Manage secrets](manage-secrets.md). You can use the secret name and pass it to the `-SecretRef` parameter:
58+
59+
```azurepowershell
60+
$envVar = New-AzContainerAppEnvironmentVarObject -Name "envVarName" -SecretRef "secretName"
61+
```
62+
63+
Then you have to create another in-memory object called [Container](/dotnet/api/Microsoft.Azure.PowerShell.Cmdlets.App.Models.Container) using the [New-AzContainerAppTemplateObject](/powershell/module/az.app/new-azcontainerapptemplateobject) PowerShell cmdlet.
64+
65+
On this cmdlet, you have to pass the name of your container image (not the container app!) you desire using the `-Name` parameter, the fully qualified image name using the `-Image` parameter and reference the environment object you defined previously on the variable `$envVar`.
66+
67+
```azurepowershell
68+
$containerTemplate = New-AzContainerAppTemplateObject -Name "container-app-name" -Image "repo/imagename:tag" -Env $envVar
69+
```
70+
71+
> [!NOTE]
72+
> Please note that there are other settings that you might need to define inside the template object to avoid overriding them like resources, volume mounts, etc. Please check the full documentation about this template on [New-AzContainerAppTemplateObject](/powershell/module/az.app/new-azcontainerapptemplateobject).
73+
74+
Finally, you can update your Container App based on the new template object you created using the [Update-AzContainerApp](/powershell/module/az.app/update-azcontainerapp) PowerShell cmdlet.
75+
76+
In this last cmdlet, you only need to pass the template object you defined on the `$containerTemplate` variable on the previous step using the `-TemplateContainer` parameter.
77+
78+
```azurepowershell
79+
Update-AzContainerApp -TemplateContainer $containerTemplate
80+
```
81+
82+
---
83+
84+
## Add environment variables on existing container apps
85+
86+
After the Container App is created, the only way to update the Container App environment variables is by creating a new revision with the needed changes.
87+
88+
### [Azure portal](#tab/portal)
89+
90+
1. In the [Azure portal](https://portal.azure.com), search for Container Apps and then select your app.
91+
92+
:::image type="content" source="media/environment-variables/container-apps-portal.png" alt-text="Screenshot of the Azure portal search bar with Container App as one of the results.":::
93+
94+
1. In the app's left menu, select Revisions and replicas > Create new revision
95+
96+
:::image type="content" source="media/environment-variables/create-new-revision.png" alt-text="Screenshot of Container App Revision creation page.":::
97+
98+
1. Then you have to edit the current existing container image:
99+
100+
:::image type="content" source="media/environment-variables/edit-revision.png" alt-text="Screenshot of Container App Revision container image settings page.":::
101+
102+
1. In the Environment variables section, you can Add new Environment variables by clicking on Add.
103+
104+
1. Then set the Name of your Environment variable and the Source (it can be a reference to a [secret](manage-secrets.md)).
105+
106+
:::image type="content" source="media/environment-variables/secret-env-var.png" alt-text="Screenshot of Container App Revision container image environment settings section.":::
107+
108+
1. If you select the Source as manual, you can manually input the Environment variable value.
109+
110+
:::image type="content" source="media/environment-variables/manual-env-var.png" alt-text="Screenshot of Container App Revision container image environment settings section with one of the environments source selected as Manual.":::
111+
112+
### [Azure CLI](#tab/cli)
113+
114+
You can update your Container App with the [az containerapp update](/cli/azure/containerapp#az-containerapp-update) command.
115+
116+
This example creates an environment variable with a manual value (not referencing a secret). Replace the \<PLACEHOLDERS\> with your values.
117+
118+
```azurecli
119+
az containerapp update \
120+
-n <APP NAME>
121+
-g <RESOURCE_GROUP_NAME>
122+
--set-env-vars <VAR_NAME>=<VAR_VALUE>
123+
```
124+
125+
If you want to create multiple environment variables, you can insert space-separated values in the 'key=value' format.
126+
127+
If you want to reference a secret, you have to ensure that the secret you want to reference is already created, see [Manage secrets](manage-secrets.md). You can use the secret name and pass it to the value field but starting with `secretref:`, see the following example:
128+
129+
```azurecli
130+
az containerapp update \
131+
-n <APP NAME>
132+
-g <RESOURCE_GROUP_NAME>
133+
--set-env-vars <VAR_NAME>=secretref:<SECRET_NAME>
134+
```
135+
136+
### [PowerShell](#tab/powershell)
137+
138+
Similarly to what you need to do upon creating a new Container App you have to create an object called [EnvironmentVar](/dotnet/api/Microsoft.Azure.PowerShell.Cmdlets.App.Models.EnvironmentVar), which is contained within a [Container](/dotnet/api/Microsoft.Azure.PowerShell.Cmdlets.App.Models.Container). This [Container](/dotnet/api/Microsoft.Azure.PowerShell.Cmdlets.App.Models.Container) is then used with the [New-AzContainerApp](/powershell/module/az.app/new-azcontainerapp) PowerShell cmdlet.
139+
140+
In this cmdlet, you only need to pass the template object you defined previously as described on the [Configuring environment variables](#configuring-environment-variables) section.
141+
142+
```azurepowershell
143+
Update-AzContainerApp -TemplateContainer $containerTemplate
144+
```
145+
146+
---
147+
148+
## Next steps
149+
150+
- [Revision management](revisions-manage.md)
17.3 KB
Loading
19.5 KB
Loading
38.6 KB
Loading
10.6 KB
Loading
8.44 KB
Loading
6.61 KB
Loading

0 commit comments

Comments
 (0)