Skip to content

Commit 877116c

Browse files
Merge pull request #234118 from divargas-msft/patch-5
[Doc-a-thon] Updating build-image-with-packer.md
2 parents 5d83816 + a24352e commit 877116c

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

articles/virtual-machines/linux/build-image-with-packer.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.service: virtual-machines
66
ms.subservice: imaging
77
ms.topic: how-to
88
ms.workload: infrastructure
9-
ms.date: 05/07/2019
9+
ms.date: 04/11/2023
1010
ms.author: cynthn
1111
ms.collection: linux
1212
---
@@ -20,23 +20,23 @@ Each virtual machine (VM) in Azure is created from an image that defines the Lin
2020
> [!NOTE]
2121
> 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).
2222
23-
2423
## Create Azure resource group
24+
2525
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.
2626

2727
Create a resource group with [az group create](/cli/azure/group). The following example creates a resource group named *myResourceGroup* in the *eastus* location:
2828

29-
```azurecli
29+
```azurecli-interactive
3030
az group create -n myResourceGroup -l eastus
3131
```
3232

33-
3433
## Create Azure credentials
34+
3535
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.
3636

3737
Create a service principal with [az ad sp create-for-rbac](/cli/azure/ad/sp) and output the credentials that Packer needs:
3838

39-
```azurecli
39+
```azurecli-interactive
4040
az ad sp create-for-rbac --role Contributor --scopes /subscriptions/<subscription_id> --query "{ client_id: appId, client_secret: password, tenant_id: tenant }"
4141
```
4242

@@ -52,15 +52,15 @@ An example of the output from the preceding commands is as follows:
5252

5353
To authenticate to Azure, you also need to obtain your Azure subscription ID with [az account show](/cli/azure/account):
5454

55-
```azurecli
55+
```azurecli-interactive
5656
az account show --query "{ subscription_id: id }"
5757
```
5858

5959
You use the output from these two commands in the next step.
6060

61-
6261
## Define Packer template
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.
62+
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://developer.hashicorp.com/packer/plugins/builders/azure) that allows you to define Azure resources, such as the service principal credentials created in the preceding step.
6464

6565
Create a file named *ubuntu.json* and paste the following content. Enter your own values for the following parameters:
6666

@@ -73,7 +73,6 @@ Create a file named *ubuntu.json* and paste the following content. Enter your ow
7373
| *managed_image_resource_group_name* | Name of resource group you created in the first step |
7474
| *managed_image_name* | Name for the managed disk image that is created |
7575

76-
7776
```json
7877
{
7978
"builders": [{
@@ -90,7 +89,7 @@ Create a file named *ubuntu.json* and paste the following content. Enter your ow
9089
"os_type": "Linux",
9190
"image_publisher": "Canonical",
9291
"image_offer": "UbuntuServer",
93-
"image_sku": "16.04-LTS",
92+
"image_sku": "20.04-LTS",
9493

9594
"azure_tags": {
9695
"dept": "Engineering",
@@ -114,6 +113,10 @@ Create a file named *ubuntu.json* and paste the following content. Enter your ow
114113
}]
115114
}
116115
```
116+
117+
> [!NOTE]
118+
> Replace the `image_publisher`, `image_offer`, `image_sku` values and `inline` commands accordingly.
119+
117120
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.
118121

119122
```HCL
@@ -126,7 +129,7 @@ source "azure-arm" "autogenerated_1" {
126129
client_secret = "0e760437-bf34-4aad-9f8d-870be799c55d"
127130
image_offer = "UbuntuServer"
128131
image_publisher = "Canonical"
129-
image_sku = "16.04-LTS"
132+
image_sku = "20.04-LTS"
130133
location = "East US"
131134
managed_image_name = "myPackerImage"
132135
managed_image_resource_group_name = "myResourceGroup"
@@ -148,27 +151,26 @@ build {
148151
}
149152
```
150153

151-
152154
This template builds an Ubuntu 16.04 LTS image, installs NGINX, then deprovisions the VM.
153155

154156
> [!NOTE]
155157
> 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`.
156158
> The `+user` flag removes all user accounts from the source VM.
157159
158-
159160
## Build Packer image
161+
160162
If you don't already have Packer installed on your local machine, [follow the Packer installation instructions](https://www.packer.io/docs/install).
161163

162164
Build the image by specifying your Packer template file as follows:
163165

164166
```bash
165-
./packer build ubuntu.json
167+
sudo ./packer build ubuntu.json
166168
```
167169

168170
You can also build the image by specifying the *ubuntu.pkr.hcl* file as follows:
169171

170172
```bash
171-
packer build ubuntu.pkr.hcl
173+
sudo packer build ubuntu.pkr.hcl
172174
```
173175

174176
An example of the output from the preceding commands is as follows:
@@ -234,11 +236,11 @@ ManagedImageLocation: eastus
234236

235237
It takes a few minutes for Packer to build the VM, run the provisioners, and clean up the deployment.
236238

237-
238239
## Create VM from Azure Image
240+
239241
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:
240242

241-
```azurecli
243+
```azurecli-interactive
242244
az vm create \
243245
--resource-group myResourceGroup \
244246
--name myVM \
@@ -253,18 +255,18 @@ It takes a few minutes to create the VM. Once the VM has been created, take note
253255

254256
To allow web traffic to reach your VM, open port 80 from the Internet with [az vm open-port](/cli/azure/vm):
255257

256-
```azurecli
258+
```azurecli-interactive
257259
az vm open-port \
258260
--resource-group myResourceGroup \
259261
--name myVM \
260262
--port 80
261263
```
262264

263265
## Test VM and NGINX
266+
264267
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:
265268

266269
![NGINX default site](./media/build-image-with-packer/nginx.png)
267270

268-
269271
## Next steps
270272
You can also use existing Packer provisioner scripts with [Azure Image Builder](image-builder.md).

0 commit comments

Comments
 (0)