github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/website/source/docs/builders/amazon-chroot.html.md (about) 1 --- 2 description: | 3 The amazon-chroot Packer builder is able to create Amazon AMIs backed by an 4 EBS volume as the root device. For more information on the difference between 5 instance storage and EBS-backed instances, storage for the root device section 6 in the EC2 documentation. 7 layout: docs 8 page_title: 'Amazon chroot - Builders' 9 sidebar_current: 'docs-builders-amazon-chroot' 10 --- 11 12 # AMI Builder (chroot) 13 14 Type: `amazon-chroot` 15 16 The `amazon-chroot` Packer builder is able to create Amazon AMIs backed by an 17 EBS volume as the root device. For more information on the difference between 18 instance storage and EBS-backed instances, see the ["storage for the root 19 device" section in the EC2 20 documentation](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ComponentsAMIs.html#storage-for-the-root-device). 21 22 The difference between this builder and the `amazon-ebs` builder is that this 23 builder is able to build an EBS-backed AMI without launching a new EC2 instance. 24 This can dramatically speed up AMI builds for organizations who need the extra 25 fast build. 26 27 ~> **This is an advanced builder** If you're just getting started with 28 Packer, we recommend starting with the [amazon-ebs 29 builder](/docs/builders/amazon-ebs.html), which is much easier to use. 30 31 The builder does *not* manage AMIs. Once it creates an AMI and stores it in your 32 account, it is up to you to use, delete, etc. the AMI. 33 34 ## How Does it Work? 35 36 This builder works by creating a new EBS volume from an existing source AMI and 37 attaching it into an already-running EC2 instance. Once attached, a 38 [chroot](https://en.wikipedia.org/wiki/Chroot) is used to provision the system 39 within that volume. After provisioning, the volume is detached, snapshotted, and 40 an AMI is made. 41 42 Using this process, minutes can be shaved off the AMI creation process because a 43 new EC2 instance doesn't need to be launched. 44 45 There are some restrictions, however. The host EC2 instance where the volume is 46 attached to must be a similar system (generally the same OS version, kernel 47 versions, etc.) as the AMI being built. Additionally, this process is much more 48 expensive because the EC2 instance must be kept running persistently in order to 49 build AMIs, whereas the other AMI builders start instances on-demand to build 50 AMIs as needed. 51 52 ## Configuration Reference 53 54 There are many configuration options available for the builder. They are 55 segmented below into two categories: required and optional parameters. Within 56 each category, the available configuration keys are alphabetized. 57 58 ### Required: 59 60 - `access_key` (string) - The access key used to communicate with AWS. [Learn 61 how to set this.](/docs/builders/amazon.html#specifying-amazon-credentials) 62 63 - `ami_name` (string) - The name of the resulting AMI that will appear when 64 managing AMIs in the AWS console or via APIs. This must be unique. To help 65 make this unique, use a function like `timestamp` (see [template 66 engine](/docs/templates/engine.html) for more info) 67 68 - `secret_key` (string) - The secret key used to communicate with AWS. [Learn 69 how to set this.](/docs/builders/amazon.html#specifying-amazon-credentials) 70 71 - `source_ami` (string) - The source AMI whose root volume will be copied and 72 provisioned on the currently running instance. This must be an EBS-backed AMI 73 with a root volume snapshot that you have access to. Note: this is not used 74 when `from_scratch` is set to true. 75 76 ### Optional: 77 78 - `ami_description` (string) - The description to set for the 79 resulting AMI(s). By default this description is empty. This is a 80 [template engine](/docs/templates/engine.html) 81 where the `SourceAMI` variable is replaced with the source AMI ID and 82 `BuildRegion` variable is replaced with name of the region where this 83 is built. 84 85 - `ami_groups` (array of strings) - A list of groups that have access to 86 launch the resulting AMI(s). By default no groups have permission to launch 87 the AMI. `all` will make the AMI publicly accessible. 88 89 - `ami_product_codes` (array of strings) - A list of product codes to 90 associate with the AMI. By default no product codes are associated with 91 the AMI. 92 93 - `ami_regions` (array of strings) - A list of regions to copy the AMI to. 94 Tags and attributes are copied along with the AMI. AMI copying takes time 95 depending on the size of the AMI, but will generally take many minutes. 96 97 - `ami_users` (array of strings) - A list of account IDs that have access to 98 launch the resulting AMI(s). By default no additional users other than the 99 user creating the AMI has permissions to launch it. 100 101 - `ami_virtualization_type` (string) - The type of virtualization for the AMI 102 you are building. This option is required to register HVM images. Can be 103 "paravirtual" (default) or "hvm". 104 105 - `chroot_mounts` (array of array of strings) - This is a list of devices 106 to mount into the chroot environment. This configuration parameter 107 requires some additional documentation which is in the "Chroot Mounts" 108 section below. Please read that section for more information on how to 109 use this. 110 111 - `command_wrapper` (string) - How to run shell commands. This defaults to 112 `{{.Command}}`. This may be useful to set if you want to set environmental 113 variables or perhaps run it with `sudo` or so on. This is a configuration 114 template where the `.Command` variable is replaced with the command to 115 be run. Defaults to "{{.Command}}". 116 117 - `copy_files` (array of strings) - Paths to files on the running EC2 instance 118 that will be copied into the chroot environment prior to provisioning. Defaults 119 to `/etc/resolv.conf` so that DNS lookups work. Pass an empty list to skip 120 copying `/etc/resolv.conf`. You may need to do this if you're building 121 an image that uses systemd. 122 123 - `custom_endpoint_ec2` (string) - this option is useful if you use 124 another cloud provider that provide a compatible API with aws EC2, 125 specify another endpoint like this "<https://ec2.another.endpoint>..com" 126 127 - `device_path` (string) - The path to the device where the root volume of the 128 source AMI will be attached. This defaults to "" (empty string), which 129 forces Packer to find an open device automatically. 130 131 - `ena_support` (boolean) - Enable enhanced networking (ENA but not SriovNetSupport) 132 on HVM-compatible AMIs. If true, add `ec2:ModifyInstanceAttribute` to your AWS IAM policy. 133 Note: you must make sure enhanced networking is enabled on your instance. See [Amazon's 134 documentation on enabling enhanced networking](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enhanced-networking.html#enabling_enhanced_networking). Default `false`. 135 136 - `force_deregister` (boolean) - Force Packer to first deregister an existing 137 AMI if one with the same name already exists. Default `false`. 138 139 - `force_delete_snapshot` (boolean) - Force Packer to delete snapshots associated with 140 AMIs, which have been deregistered by `force_deregister`. Default `false`. 141 142 - `encrypt_boot` (boolean) - Instruct packer to automatically create a copy of the 143 AMI with an encrypted boot volume (discarding the initial unencrypted AMI in the 144 process). Packer will always run this operation, even if the base 145 AMI has an encrypted boot volume to start with. Default `false`. 146 147 - `kms_key_id` (string) - The ID of the KMS key to use for boot volume encryption. 148 This only applies to the main `region`, other regions where the AMI will be copied 149 will be encrypted by the default EBS KMS key. 150 151 - `from_scratch` (boolean) - Build a new volume instead of starting from an 152 existing AMI root volume snapshot. Default `false`. If true, `source_ami` is 153 no longer used and the following options become required: 154 `ami_virtualization_type`, `pre_mount_commands` and `root_volume_size`. The 155 below options are also required in this mode only: 156 157 - `ami_block_device_mappings` (array of block device mappings) - Add one or 158 more [block device mappings](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html) 159 to the AMI. These will be attached when booting a new instance from your 160 AMI. Your options here may vary depending on the type of VM you use. The 161 block device mappings allow for the following configuration: 162 163 - `delete_on_termination` (boolean) - Indicates whether the EBS volume is 164 deleted on instance termination. Default `false`. **NOTE**: If this 165 value is not explicitly set to `true` and volumes are not cleaned up by 166 an alternative method, additional volumes will accumulate after 167 every build. 168 169 - `device_name` (string) - The device name exposed to the instance (for 170 example, `/dev/sdh` or `xvdh`). Required when specifying `volume_size`. 171 172 - `encrypted` (boolean) - Indicates whether to encrypt the volume or not 173 174 - `iops` (integer) - The number of I/O operations per second (IOPS) that the 175 volume supports. See the documentation on 176 [IOPs](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_EbsBlockDevice.html) 177 for more information 178 179 - `no_device` (boolean) - Suppresses the specified device included in the 180 block device mapping of the AMI 181 182 - `snapshot_id` (string) - The ID of the snapshot 183 184 - `virtual_name` (string) - The virtual device name. See the documentation on 185 [Block Device 186 Mapping](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_BlockDeviceMapping.html) 187 for more information 188 189 - `volume_size` (integer) - The size of the volume, in GiB. Required if not 190 specifying a `snapshot_id` 191 192 - `volume_type` (string) - The volume type. gp2 for General Purpose (SSD) 193 volumes, io1 for Provisioned IOPS (SSD) volumes, and standard for Magnetic 194 volumes 195 196 - `region_kms_key_ids` (map of strings) - a map of regions to copy the ami to, 197 along with the custom kms key id to use for encryption for that region. 198 Keys must match the regions provided in `ami_regions`. If you just want to 199 encrypt using a default ID, you can stick with `kms_key_id` and `ami_regions`. 200 If you want a region to be encrypted with that region's default key ID, you can 201 use an empty string `""` instead of a key id in this map. (e.g. `"us-east-1": ""`) 202 However, you cannot use default key IDs if you are using this in conjunction with 203 `snapshot_users` -- in that situation you must use custom keys. 204 205 - `root_device_name` (string) - The root device name. For example, `xvda`. 206 207 - `mfa_code` (string) - The MFA [TOTP](https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm) 208 code. This should probably be a user variable since it changes all the time. 209 210 - `mount_path` (string) - The path where the volume will be mounted. This is 211 where the chroot environment will be. This defaults to 212 `/mnt/packer-amazon-chroot-volumes/{{.Device}}`. This is a configuration template 213 where the `.Device` variable is replaced with the name of the device where 214 the volume is attached. 215 216 - `mount_partition` (integer) - The partition number containing the 217 / partition. By default this is the first partition of the volume. 218 219 - `mount_options` (array of strings) - Options to supply the `mount` command 220 when mounting devices. Each option will be prefixed with `-o` and supplied 221 to the `mount` command ran by Packer. Because this command is ran in a 222 shell, user discretion is advised. See [this manual page for the mount 223 command](http://linuxcommand.org/man_pages/mount8.html) for valid file 224 system specific options 225 226 - `pre_mount_commands` (array of strings) - A series of commands to execute 227 after attaching the root volume and before mounting the chroot. This is not 228 required unless using `from_scratch`. If so, this should include any 229 partitioning and filesystem creation commands. The path to the device is 230 provided by `{{.Device}}`. 231 232 - `profile` (string) - The profile to use in the shared credentials file for 233 AWS. See Amazon's documentation on [specifying 234 profiles](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-profiles) 235 for more details. 236 237 - `post_mount_commands` (array of strings) - As `pre_mount_commands`, but the 238 commands are executed after mounting the root device and before the extra 239 mount and copy steps. The device and mount path are provided by 240 `{{.Device}}` and `{{.MountPath}}`. 241 242 - `root_volume_size` (integer) - The size of the root volume in GB for the 243 chroot environment and the resulting AMI. Default size is the snapshot size 244 of the `source_ami` unless `from_scratch` is `true`, in which case 245 this field must be defined. 246 247 - `skip_region_validation` (boolean) - Set to true if you want to skip 248 validation of the `ami_regions` configuration option. Default `false`. 249 250 - `snapshot_tags` (object of key/value strings) - Tags to apply to snapshot. 251 They will override AMI tags if already applied to snapshot. This is a 252 [template engine](/docs/templates/engine.html) 253 where the `SourceAMI` variable is replaced with the source AMI ID and 254 `BuildRegion` variable is replaced with name of the region where this 255 is built. 256 257 - `snapshot_groups` (array of strings) - A list of groups that have access to 258 create volumes from the snapshot(s). By default no groups have permission to create 259 volumes form the snapshot(s). `all` will make the snapshot publicly accessible. 260 261 - `snapshot_users` (array of strings) - A list of account IDs that have access to 262 create volumes from the snapshot(s). By default no additional users other than the 263 user creating the AMI has permissions to create volumes from the backing snapshot(s). 264 265 - `source_ami_filter` (object) - Filters used to populate the `source_ami` field. 266 Example: 267 268 ``` json 269 "source_ami_filter": { 270 "filters": { 271 "virtualization-type": "hvm", 272 "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*", 273 "root-device-type": "ebs" 274 }, 275 "owners": ["099720109477"], 276 "most_recent": true 277 } 278 ``` 279 280 This selects the most recent Ubuntu 16.04 HVM EBS AMI from Canonical. 281 NOTE: This will fail unless *exactly* one AMI is returned. In the above 282 example, `most_recent` will cause this to succeed by selecting the newest image. 283 284 - `filters` (map of strings) - filters used to select a `source_ami`. 285 NOTE: This will fail unless *exactly* one AMI is returned. 286 Any filter described in the docs for [DescribeImages](http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeImages.html) 287 is valid. 288 289 - `owners` (array of strings) - This scopes the AMIs to certain Amazon account IDs. 290 This is helpful to limit the AMIs to a trusted third party, or to your own account. 291 292 - `most_recent` (bool) - Selects the newest created image when true. 293 This is most useful for selecting a daily distro build. 294 295 - `sriov_support` (boolean) - Enable enhanced networking (SriovNetSupport but not ENA) 296 on HVM-compatible AMIs. If true, add `ec2:ModifyInstanceAttribute` to your AWS IAM 297 policy. Note: you must make sure enhanced networking is enabled on your instance. See [Amazon's 298 documentation on enabling enhanced networking](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enhanced-networking.html#enabling_enhanced_networking). 299 Default `false`. 300 301 - `tags` (object of key/value strings) - Tags applied to the AMI. This is a 302 [template engine](/docs/templates/engine.html) 303 where the `SourceAMI` variable is replaced with the source AMI ID and 304 `BuildRegion` variable is replaced with name of the region where this 305 is built. 306 307 ## Basic Example 308 309 Here is a basic example. It is completely valid except for the access keys: 310 311 ``` json 312 { 313 "type": "amazon-chroot", 314 "access_key": "YOUR KEY HERE", 315 "secret_key": "YOUR SECRET KEY HERE", 316 "source_ami": "ami-e81d5881", 317 "ami_name": "packer-amazon-chroot {{timestamp}}" 318 } 319 ``` 320 321 ## Chroot Mounts 322 323 The `chroot_mounts` configuration can be used to mount specific devices within 324 the chroot. By default, the following additional mounts are added into the 325 chroot by Packer: 326 327 - `/proc` (proc) 328 - `/sys` (sysfs) 329 - `/dev` (bind to real `/dev`) 330 - `/dev/pts` (devpts) 331 - `/proc/sys/fs/binfmt_misc` (binfmt\_misc) 332 333 These default mounts are usually good enough for anyone and are sane defaults. 334 However, if you want to change or add the mount points, you may using the 335 `chroot_mounts` configuration. Here is an example configuration which only 336 mounts `/prod` and `/dev`: 337 338 ``` json 339 { 340 "chroot_mounts": [ 341 ["proc", "proc", "/proc"], 342 ["bind", "/dev", "/dev"] 343 ] 344 } 345 ``` 346 347 `chroot_mounts` is a list of a 3-tuples of strings. The three components of the 348 3-tuple, in order, are: 349 350 - The filesystem type. If this is "bind", then Packer will properly bind the 351 filesystem to another mount point. 352 353 - The source device. 354 355 - The mount directory. 356 357 ## Parallelism 358 359 A quick note on parallelism: it is perfectly safe to run multiple *separate* 360 Packer processes with the `amazon-chroot` builder on the same EC2 instance. In 361 fact, this is recommended as a way to push the most performance out of your AMI 362 builds. 363 364 Packer properly obtains a process lock for the parallelism-sensitive parts of 365 its internals such as finding an available device. 366 367 ## Gotchas 368 369 One of the difficulties with using the chroot builder is that your provisioning 370 scripts must not leave any processes running or packer will be unable to unmount 371 the filesystem. 372 373 For debian based distributions you can setup a 374 [policy-rc.d](http://people.debian.org/~hmh/invokerc.d-policyrc.d-specification.txt) 375 file which will prevent packages installed by your provisioners from starting 376 services: 377 378 ``` json 379 { 380 "type": "shell", 381 "inline": [ 382 "echo '#!/bin/sh' > /usr/sbin/policy-rc.d", 383 "echo 'exit 101' >> /usr/sbin/policy-rc.d", 384 "chmod a+x /usr/sbin/policy-rc.d" 385 ] 386 }, 387 388 // ... 389 390 { 391 "type": "shell", 392 "inline": [ 393 "rm -f /usr/sbin/policy-rc.d" 394 ] 395 } 396 ``` 397 398 ## Building From Scratch 399 400 This example demonstrates the essentials of building an image from scratch. A 401 15G gp2 (SSD) device is created (overriding the default of standard/magnetic). 402 The device setup commands partition the device with one partition for use as an 403 HVM image and format it ext4. This builder block should be followed by 404 provisioning commands to install the os and bootloader. 405 406 ``` json 407 { 408 "type": "amazon-chroot", 409 "ami_name": "packer-from-scratch {{timestamp}}", 410 "from_scratch": true, 411 "ami_virtualization_type": "hvm", 412 "pre_mount_commands": [ 413 "parted {{.Device}} mklabel msdos mkpart primary 1M 100% set 1 boot on print", 414 "mkfs.ext4 {{.Device}}1" 415 ], 416 "root_volume_size": 15, 417 "root_device_name": "xvda", 418 "ami_block_device_mappings": [ 419 { 420 "device_name": "xvda", 421 "delete_on_termination": true, 422 "volume_type": "gp2" 423 } 424 ] 425 } 426 ```