You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/container-instances/container-instances-managed-identity.md
+50-12Lines changed: 50 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -62,13 +62,19 @@ az group create --name myResourceGroup --location eastus
62
62
Use the [az keyvault create](/cli/azure/keyvault?view=azure-cli-latest#az-keyvault-create) command to create a key vault. Be sure to specify a unique key vault name.
63
63
64
64
```azurecli-interactive
65
-
az keyvault create --name mykeyvault --resource-group myResourceGroup --location eastus
65
+
az keyvault create \
66
+
--name mykeyvault \
67
+
--resource-group myResourceGroup \
68
+
--location eastus
66
69
```
67
70
68
71
Store a sample secret in the key vault using the [az keyvault secret set](/cli/azure/keyvault/secret?view=azure-cli-latest#az-keyvault-secret-set) command:
69
72
70
73
```azurecli-interactive
71
-
az keyvault secret set --name SampleSecret --value "Hello Container Instances" --description ACIsecret --vault-name mykeyvault
74
+
az keyvault secret set \
75
+
--name SampleSecret \
76
+
--value "Hello Container Instances" \
77
+
--description ACIsecret --vault-name mykeyvault
72
78
```
73
79
74
80
Continue with the following examples to access the key vault using either a user-assigned or system-assigned managed identity in Azure Container Instances.
@@ -80,7 +86,9 @@ Continue with the following examples to access the key vault using either a user
80
86
First create an identity in your subscription using the [az identity create](/cli/azure/identity?view=azure-cli-latest#az-identity-create) command. You can use the same resource group used to create the key vault, or use a different one.
81
87
82
88
```azurecli-interactive
83
-
az identity create --resource-group myResourceGroup --name myACIId
89
+
az identity create \
90
+
--resource-group myResourceGroup \
91
+
--name myACIId
84
92
```
85
93
86
94
To use the identity in the following steps, use the [az identity show](/cli/azure/identity?view=azure-cli-latest#az-identity-show) command to store the identity's service principal ID and resource ID in variables.
@@ -100,13 +108,20 @@ Run the following [az container create](/cli/azure/container?view=azure-cli-late
100
108
The `--assign-identity` parameter passes your user-assigned managed identity to the group. The long-running command keeps the container running. This example uses the same resource group used to create the key vault, but you could specify a different one.
Within a few seconds, you should get a response from the Azure CLI indicating that the deployment has completed. Check its status with the [az container show](/cli/azure/container?view=azure-cli-latest#az-container-show) command.
107
120
108
121
```azurecli-interactive
109
-
az container show --resource-group myResourceGroup --name mycontainer
122
+
az container show \
123
+
--resource-group myResourceGroup \
124
+
--name mycontainer
110
125
```
111
126
112
127
The `identity` section in the output looks similar to the following, showing the identity is set in the container group. The `principalID` under `userAssignedIdentities` is the service principal of the identity you created in Azure Active Directory:
@@ -132,15 +147,22 @@ The `identity` section in the output looks similar to the following, showing the
132
147
Run the following [az keyvault set-policy](/cli/azure/keyvault?view=azure-cli-latest) command to set an access policy on the key vault. The following example allows the user-assigned identity to get secrets from the key vault:
133
148
134
149
```azurecli-interactive
135
-
az keyvault set-policy --name mykeyvault --resource-group myResourceGroup --object-id $spID --secret-permissions get
150
+
az keyvault set-policy \
151
+
--name mykeyvault \
152
+
--resource-group myResourceGroup \
153
+
--object-id $spID \
154
+
--secret-permissions get
136
155
```
137
156
138
157
### Use user-assigned identity to get secret from key vault
139
158
140
159
Now you can use the managed identity within the running container instance to access the key vault. First launch a bash shell in the container:
141
160
142
161
```azurecli-interactive
143
-
az container exec --resource-group myResourceGroup --name mycontainer --exec-command "/bin/bash"
162
+
az container exec \
163
+
--resource-group myResourceGroup \
164
+
--name mycontainer \
165
+
--exec-command "/bin/bash"
144
166
```
145
167
146
168
Run the following commands in the bash shell in the container. To get an access token to use Azure Active Directory to authenticate to key vault, run the following command:
@@ -187,13 +209,20 @@ The `--assign-identity` parameter with no additional value enables a system-assi
187
209
rgID=$(az group show --name myResourceGroup --query id --output tsv)
188
210
189
211
# Create container group with system-managed identity
Within a few seconds, you should get a response from the Azure CLI indicating that the deployment has completed. Check its status with the [az container show](/cli/azure/container?view=azure-cli-latest#az-container-show) command.
194
221
195
222
```azurecli-interactive
196
-
az container show --resource-group myResourceGroup --name mycontainer
223
+
az container show \
224
+
--resource-group myResourceGroup \
225
+
--name mycontainer
197
226
```
198
227
199
228
The `identity` section in the output looks similar to the following, showing that a system-assigned identity is created in Azure Active Directory:
Run the following [az keyvault set-policy](/cli/azure/keyvault?view=azure-cli-latest) command to set an access policy on the key vault. The following example allows the system-managed identity to get secrets from the key vault:
221
250
222
251
```azurecli-interactive
223
-
az keyvault set-policy --name mykeyvault --resource-group myResourceGroup --object-id $spID --secret-permissions get
252
+
az keyvault set-policy \
253
+
--name mykeyvault \
254
+
--resource-group myResourceGroup \
255
+
--object-id $spID \
256
+
--secret-permissions get
224
257
```
225
258
226
259
### Use container group identity to get secret from key vault
227
260
228
261
Now you can use the managed identity to access the key vault within the running container instance. First launch a bash shell in the container:
229
262
230
263
```azurecli-interactive
231
-
az container exec --resource-group myResourceGroup --name mycontainer --exec-command "/bin/bash"
264
+
az container exec \
265
+
--resource-group myResourceGroup \
266
+
--name mycontainer \
267
+
--exec-command "/bin/bash"
232
268
```
233
269
234
270
Run the following commands in the bash shell in the container. First log in to the Azure CLI using the managed identity:
@@ -240,7 +276,9 @@ az login --identity
240
276
From the running container, retrieve the secret from the key vault:
241
277
242
278
```bash
243
-
az keyvault secret show --name SampleSecret --vault-name mykeyvault --query value
0 commit comments