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
Adding sudo to bash commands, making AZ CLI to use the interactive option, adding a clarification in the json template to replace the values accordingly given ubuntu is being used in the sample template
Copy file name to clipboardExpand all lines: articles/virtual-machines/linux/build-image-with-packer.md
+18-16Lines changed: 18 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ ms.service: virtual-machines
6
6
ms.subservice: imaging
7
7
ms.topic: how-to
8
8
ms.workload: infrastructure
9
-
ms.date: 05/07/2019
9
+
ms.date: 04/11/2023
10
10
ms.author: cynthn
11
11
ms.collection: linux
12
12
---
@@ -20,23 +20,23 @@ Each virtual machine (VM) in Azure is created from an image that defines the Lin
20
20
> [!NOTE]
21
21
> Azure now has a service, Azure Image Builder, for defining and creating your own custom images. Azure Image Builder is built on Packer, so you can even use your existing Packer shell provisioner scripts with it. To get started with Azure Image Builder, see [Create a Linux VM with Azure Image Builder](image-builder.md).
22
22
23
-
24
23
## Create Azure resource group
24
+
25
25
During the build process, Packer creates temporary Azure resources as it builds the source VM. To capture that source VM for use as an image, you must define a resource group. The output from the Packer build process is stored in this resource group.
26
26
27
27
Create a resource group with [az group create](/cli/azure/group). The following example creates a resource group named *myResourceGroup* in the *eastus* location:
28
28
29
-
```azurecli
29
+
```azurecli-interactive
30
30
az group create -n myResourceGroup -l eastus
31
31
```
32
32
33
-
34
33
## Create Azure credentials
34
+
35
35
Packer authenticates with Azure using a service principal. An Azure service principal is a security identity that you can use with apps, services, and automation tools like Packer. You control and define the permissions as to what operations the service principal can perform in Azure.
36
36
37
37
Create a service principal with [az ad sp create-for-rbac](/cli/azure/ad/sp) and output the credentials that Packer needs:
38
38
39
-
```azurecli
39
+
```azurecli-interactive
40
40
az ad sp create-for-rbac --role Contributor --scopes /subscriptions/<subscription_id> --query "{ client_id: appId, client_secret: password, tenant_id: tenant }"
41
41
```
42
42
@@ -52,14 +52,14 @@ An example of the output from the preceding commands is as follows:
52
52
53
53
To authenticate to Azure, you also need to obtain your Azure subscription ID with [az account show](/cli/azure/account):
54
54
55
-
```azurecli
55
+
```azurecli-interactive
56
56
az account show --query "{ subscription_id: id }"
57
57
```
58
58
59
59
You use the output from these two commands in the next step.
60
60
61
-
62
61
## Define Packer template
62
+
63
63
To build images, you create a template as a JSON file. In the template, you define builders and provisioners that carry out the actual build process. Packer has a [provisioner for Azure](https://www.packer.io/docs/builders/azure.html) that allows you to define Azure resources, such as the service principal credentials created in the preceding step.
64
64
65
65
Create a file named *ubuntu.json* and paste the following content. Enter your own values for the following parameters:
@@ -73,7 +73,6 @@ Create a file named *ubuntu.json* and paste the following content. Enter your ow
73
73
|*managed_image_resource_group_name*| Name of resource group you created in the first step |
74
74
|*managed_image_name*| Name for the managed disk image that is created |
75
75
76
-
77
76
```json
78
77
{
79
78
"builders": [{
@@ -114,6 +113,10 @@ Create a file named *ubuntu.json* and paste the following content. Enter your ow
114
113
}]
115
114
}
116
115
```
116
+
117
+
> [!NOTE]
118
+
> Replace the `image_publisher`, `image_offer`, `image_sku` values and `inline` commands accordingly.
119
+
117
120
You can also create a filed named *ubuntu.pkr.hcl* and paste the following content with your own values as used for the above parameters table.
118
121
119
122
```HCL
@@ -148,27 +151,26 @@ build {
148
151
}
149
152
```
150
153
151
-
152
154
This template builds an Ubuntu 16.04 LTS image, installs NGINX, then deprovisions the VM.
153
155
154
156
> [!NOTE]
155
157
> If you expand on this template to provision user credentials, adjust the provisioner command that deprovisions the Azure agent to read `-deprovision` rather than `deprovision+user`.
156
158
> The `+user` flag removes all user accounts from the source VM.
157
159
158
-
159
160
## Build Packer image
161
+
160
162
If you don't already have Packer installed on your local machine, [follow the Packer installation instructions](https://www.packer.io/docs/install).
161
163
162
164
Build the image by specifying your Packer template file as follows:
163
165
164
166
```bash
165
-
./packer build ubuntu.json
167
+
sudo ./packer build ubuntu.json
166
168
```
167
169
168
170
You can also build the image by specifying the *ubuntu.pkr.hcl* file as follows:
169
171
170
172
```bash
171
-
packer build ubuntu.pkr.hcl
173
+
sudo packer build ubuntu.pkr.hcl
172
174
```
173
175
174
176
An example of the output from the preceding commands is as follows:
It takes a few minutes for Packer to build the VM, run the provisioners, and clean up the deployment.
236
238
237
-
238
239
## Create VM from Azure Image
240
+
239
241
You can now create a VM from your Image with [az vm create](/cli/azure/vm). Specify the Image you created with the `--image` parameter. The following example creates a VM named *myVM* from *myPackerImage* and generates SSH keys if they don't already exist:
240
242
241
-
```azurecli
243
+
```azurecli-interactive
242
244
az vm create \
243
245
--resource-group myResourceGroup \
244
246
--name myVM \
@@ -253,18 +255,18 @@ It takes a few minutes to create the VM. Once the VM has been created, take note
253
255
254
256
To allow web traffic to reach your VM, open port 80 from the Internet with [az vm open-port](/cli/azure/vm):
255
257
256
-
```azurecli
258
+
```azurecli-interactive
257
259
az vm open-port \
258
260
--resource-group myResourceGroup \
259
261
--name myVM \
260
262
--port 80
261
263
```
262
264
263
265
## Test VM and NGINX
266
+
264
267
Now you can open a web browser and enter `http://publicIpAddress` in the address bar. Provide your own public IP address from the VM create process. The default NGINX page is displayed as in the following example:
0 commit comments