github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/website/source/docs/builders/azure-setup.html.md (about) 1 --- 2 description: | 3 In order to build VMs in Azure, Packer needs various configuration options. 4 These options and how to obtain them are documented on this page. 5 layout: docs 6 page_title: 'Setup - Azure - Builders' 7 sidebar_current: 'docs-builders-azure-setup' 8 --- 9 10 # Authorizing Packer Builds in Azure 11 12 In order to build VMs in Azure Packer needs 6 configuration options to be specified: 13 14 - `subscription_id` - UUID identifying your Azure subscription (where billing is handled) 15 16 - `client_id` - UUID identifying the Active Directory service principal that will run your Packer builds 17 18 - `client_secret` - service principal secret / password 19 20 - `object_id` - service principal object id (OSType = Windows Only) 21 22 - `resource_group_name` - name of the resource group where your VHD(s) will be stored 23 24 - `storage_account` - name of the storage account where your VHD(s) will be stored 25 26 -> Behind the scenes Packer uses the OAuth protocol to authenticate against Azure Active Directory and authorize requests to the Azure Service Management API. These topics are unnecessarily complicated so we will try to ignore them for the rest of this document.<br /><br />You do not need to understand how OAuth works in order to use Packer with Azure, though the Active Directory terms "service principal" and "role" will be useful for understanding Azure's access policies. 27 28 In order to get all of the items above, you will need a username and password for your Azure account. 29 30 ## Device Login 31 32 Device login is an alternative way to authorize in Azure Packer. Device login only requires you to know your 33 Subscription ID. (Device login is only supported for Linux based VMs.) Device login is intended for those who are first 34 time users, and just want to ''kick the tires.'' We recommend the SPN approach if you intend to automate Packer, or for 35 deploying Windows VMs. 36 37 > Device login is for **interactive** builds, and SPN is **automated** builds. 38 39 There are three pieces of information you must provide to enable device login mode. 40 41 1. SubscriptionID 42 2. Resource Group - parent resource group that Packer uses to build an image. 43 3. Storage Account - storage account where the image will be placed. 44 45 > Device login mode is enabled by not setting client\_id and client\_secret. 46 47 > Device login mode is for the public cloud only, and Linux VMs only. 48 49 The device login flow asks that you open a web browser, navigate to <http://aka.ms/devicelogin>, and input the supplied 50 code. This authorizes the Packer for Azure application to act on your behalf. An OAuth token will be created, and stored 51 in the user's home directory (~/.azure/packer/oauth-TenantID.json). This token is used if the token file exists, and it 52 is refreshed as necessary. The token file prevents the need to continually execute the device login flow. 53 54 ## Install the Azure CLI 55 56 To get the credentials above, we will need to install the Azure CLI. Please refer to Microsoft's official [installation guide](https://azure.microsoft.com/en-us/documentation/articles/xplat-cli-install/). 57 58 -> The guides below also use a tool called [`jq`](https://stedolan.github.io/jq/) to simplify the output from the Azure CLI, though this is optional. If you use homebrew you can simply `brew install node jq`. 59 60 If you already have node.js installed you can use `npm` to install `azure-cli`: 61 62 ``` shell 63 $ npm install -g azure-cli --no-progress 64 ``` 65 66 ## Guided Setup 67 68 The Packer project includes a [setup script](https://github.com/hashicorp/packer/blob/master/contrib/azure-setup.sh) that can help you setup your account. It uses an interactive bash script to log you into Azure, name your resources, and export your Packer configuration. 69 70 ## Manual Setup 71 72 If you want more control or the script does not work for you, you can also use the manual instructions below to setup your Azure account. You will need to manually keep track of the various account identifiers, resource names, and your service principal password. 73 74 ### Identify Your Tenant and Subscription IDs 75 76 Login using the Azure CLI 77 78 ``` shell 79 $ azure config mode arm 80 $ azure login -u USERNAME 81 ``` 82 83 Get your account information 84 85 ``` shell 86 $ azure account list --json | jq -r '.[].name' 87 $ azure account set ACCOUNTNAME 88 $ azure account show --json | jq -r ".[] | .id" 89 ``` 90 91 -> Throughout this document when you see a command pipe to `jq` you may instead omit `--json` and everything after it, but the output will be more verbose. For example you can simply run `azure account list` instead. 92 93 This will print out one line that look like this: 94 95 4f562e88-8caf-421a-b4da-e3f6786c52ec 96 97 This is your `subscription_id`. Note it for later. 98 99 ### Create a Resource Group 100 101 A [resource group](https://azure.microsoft.com/en-us/documentation/articles/resource-group-overview/#resource-groups) is used to organize related resources. Resource groups and storage accounts are tied to a location. To see available locations, run: 102 103 ``` shell 104 $ azure location list 105 # ... 106 107 $ azure group create -n GROUPNAME -l LOCATION 108 ``` 109 110 Your storage account (below) will need to use the same `GROUPNAME` and `LOCATION`. 111 112 ### Create a Storage Account 113 114 We will need to create a storage account where your Packer artifacts will be stored. We will create a `LRS` storage account which is the least expensive price/GB at the time of writing. 115 116 ``` shell 117 $ azure storage account create \ 118 -g GROUPNAME \ 119 -l LOCATION \ 120 --sku-name LRS \ 121 --kind storage STORAGENAME 122 ``` 123 124 -> `LRS` is meant as a literal "LRS" and not as a variable. 125 126 Make sure that `GROUPNAME` and `LOCATION` are the same as above. 127 128 ### Create an Application 129 130 An application represents a way to authorize access to the Azure API. Note that you will need to specify a URL for your application (this is intended to be used for OAuth callbacks) but these do not actually need to be valid URLs. 131 132 ``` shell 133 $ azure ad app create \ 134 -n APPNAME \ 135 -i APPURL \ 136 --home-page APPURL \ 137 -p PASSWORD 138 ``` 139 140 Password is your `client_secret` and can be anything you like. I recommend using `openssl rand -base64 24`. 141 142 ### Create a Service Principal 143 144 You cannot directly grant permissions to an application. Instead, you create a service principal associated with the application and assign permissions to the service principal. 145 146 First, get the `APPID` for the application we just created. 147 148 ``` shell 149 $ azure ad app list --json \ 150 | jq '.[] | select(.displayName | contains("APPNAME")) | .appId' 151 # ... 152 153 $ azure ad sp create --applicationId APPID 154 ``` 155 156 ### Grant Permissions to Your Application 157 158 Finally, we will associate the proper permissions with our application's service principal. We're going to assign the `Owner` role to our Packer application and change the scope to manage our whole subscription. (The `Owner` role can be scoped to a specific resource group to further reduce the scope of the account.) This allows Packer to create temporary resource groups for each build. 159 160 ``` shell 161 $ azure role assignment create \ 162 --spn APPURL \ 163 -o "Owner" \ 164 -c /subscriptions/SUBSCRIPTIONID 165 ``` 166 167 There are a lot of pre-defined roles and you can define your own with more granular permissions, though this is out of scope. You can see a list of pre-configured roles via: 168 169 ``` shell 170 $ azure role list --json \ 171 | jq ".[] | {name:.Name, description:.Description}" 172 ``` 173 174 ### Configuring Packer 175 176 Now (finally) everything has been setup in Azure. Let's get our configuration keys together: 177 178 Get `subscription_id`: 179 180 ``` shell 181 $ azure account show --json \ 182 | jq ".[] | .id" 183 ``` 184 185 Get `client_id` 186 187 ``` shell 188 $ azure ad app list --json \ 189 | jq '.[] | select(.displayName | contains("APPNAME")) | .appId' 190 ``` 191 192 Get `client_secret` 193 194 This cannot be retrieved. If you forgot this, you will have to delete and re-create your service principal and the associated permissions. 195 196 Get `object_id` (OSTYpe=Windows only) 197 198 ``` shell 199 azure ad sp show -n CLIENT_ID 200 ``` 201 202 Get `resource_group_name` 203 204 ``` shell 205 $ azure group list 206 ``` 207 208 Get `storage_account` 209 210 ``` shell 211 $ azure storage account list 212 ```