github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/website/source/docs/builders/amazon-ebs.html.md (about)

     1  ---
     2  description: |
     3      The amazon-ebs Packer builder is able to create Amazon AMIs backed by EBS
     4      volumes for use in EC2. For more information on the difference between
     5      EBS-backed instances and instance-store backed instances, see the storage for
     6      the root device section in the EC2 documentation.
     7  layout: docs
     8  page_title: 'Amazon EBS - Builders'
     9  sidebar_current: 'docs-builders-amazon-ebsbacked'
    10  ---
    11  
    12  # AMI Builder (EBS backed)
    13  
    14  Type: `amazon-ebs`
    15  
    16  The `amazon-ebs` Packer builder is able to create Amazon AMIs backed by EBS
    17  volumes for use in [EC2](https://aws.amazon.com/ec2/). For more information on
    18  the difference between EBS-backed instances and instance-store backed instances,
    19  see the ["storage for the root device" section in the EC2
    20  documentation](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ComponentsAMIs.html#storage-for-the-root-device).
    21  
    22  This builder builds an AMI by launching an EC2 instance from a source AMI,
    23  provisioning that running machine, and then creating an AMI from that machine.
    24  This is all done in your own AWS account. The builder will create temporary
    25  keypairs, security group rules, etc. that provide it temporary access to the
    26  instance while the image is being created. This simplifies configuration quite a
    27  bit.
    28  
    29  The builder does *not* manage AMIs. Once it creates an AMI and stores it in your
    30  account, it is up to you to use, delete, etc. the AMI.
    31  
    32  -> **Note:** Temporary resources are, by default, all created with the prefix
    33  `packer`. This can be useful if you want to restrict the security groups and
    34  key pairs Packer is able to operate on.
    35  
    36  ## Configuration Reference
    37  
    38  There are many configuration options available for the builder. They are
    39  segmented below into two categories: required and optional parameters. Within
    40  each category, the available configuration keys are alphabetized.
    41  
    42  In addition to the options listed here, a
    43  [communicator](/docs/templates/communicator.html) can be configured for this
    44  builder.
    45  
    46  ### Required:
    47  
    48  -   `access_key` (string) - The access key used to communicate with AWS. [Learn
    49      how to set this.](/docs/builders/amazon.html#specifying-amazon-credentials)
    50  
    51  -   `ami_name` (string) - The name of the resulting AMI that will appear when
    52      managing AMIs in the AWS console or via APIs. This must be unique. To help
    53      make this unique, use a function like `timestamp` (see [template
    54      engine](/docs/templates/engine.html) for more info)
    55  
    56  -   `instance_type` (string) - The EC2 instance type to use while building the
    57      AMI, such as `t2.small`.
    58  
    59  -   `region` (string) - The name of the region, such as `us-east-1`, in which to
    60      launch the EC2 instance to create the AMI.
    61  
    62  -   `secret_key` (string) - The secret key used to communicate with AWS. [Learn
    63      how to set this.](/docs/builders/amazon.html#specifying-amazon-credentials)
    64  
    65  -   `source_ami` (string) - The initial AMI used as a base for the newly
    66      created machine. `source_ami_filter` may be used instead to populate this
    67      automatically.
    68  
    69  ### Optional:
    70  
    71  -   `ami_block_device_mappings` (array of block device mappings) - Add one or
    72      more [block device mappings](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html)
    73      to the AMI. These will be attached when booting a new instance from your
    74      AMI. To add a block device during the Packer build see
    75      `launch_block_device_mappings` below. Your options here may vary depending
    76      on the type of VM you use. The block device mappings allow for the following
    77      configuration:
    78  
    79      -   `delete_on_termination` (boolean) - Indicates whether the EBS volume is
    80          deleted on instance termination. Default `false`. **NOTE**: If this
    81          value is not explicitly set to `true` and volumes are not cleaned up by
    82          an alternative method, additional volumes will accumulate after
    83          every build.
    84  
    85      -   `device_name` (string) - The device name exposed to the instance (for
    86          example, `/dev/sdh` or `xvdh`). Required when specifying `volume_size`.
    87  
    88      -   `encrypted` (boolean) - Indicates whether to encrypt the volume or not
    89  
    90      -   `iops` (integer) - The number of I/O operations per second (IOPS) that the
    91          volume supports. See the documentation on
    92          [IOPs](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_EbsBlockDevice.html)
    93          for more information
    94  
    95      -   `no_device` (boolean) - Suppresses the specified device included in the
    96          block device mapping of the AMI
    97  
    98      -   `snapshot_id` (string) - The ID of the snapshot
    99  
   100      -   `virtual_name` (string) - The virtual device name. See the documentation on
   101          [Block Device
   102          Mapping](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_BlockDeviceMapping.html)
   103          for more information
   104  
   105      -   `volume_size` (integer) - The size of the volume, in GiB. Required if not
   106          specifying a `snapshot_id`
   107  
   108      -   `volume_type` (string) - The volume type. `gp2` for General Purpose (SSD)
   109          volumes, `io1` for Provisioned IOPS (SSD) volumes, and `standard` for Magnetic
   110          volumes
   111  
   112  -   `ami_description` (string) - The description to set for the
   113      resulting AMI(s). By default this description is empty. This is a
   114      [template engine](/docs/templates/engine.html)
   115      where the `SourceAMI` variable is replaced with the source AMI ID and
   116      `BuildRegion` variable is replaced with the value of `region`.
   117  
   118  -   `ami_groups` (array of strings) - A list of groups that have access to
   119      launch the resulting AMI(s). By default no groups have permission to launch
   120      the AMI. `all` will make the AMI publicly accessible. AWS currently doesn't
   121      accept any value other than `all`.
   122  
   123  -   `ami_product_codes` (array of strings) - A list of product codes to
   124      associate with the AMI. By default no product codes are associated with
   125      the AMI.
   126  
   127  -   `ami_regions` (array of strings) - A list of regions to copy the AMI to.
   128      Tags and attributes are copied along with the AMI. AMI copying takes time
   129      depending on the size of the AMI, but will generally take many minutes.
   130  
   131  -   `ami_users` (array of strings) - A list of account IDs that have access to
   132      launch the resulting AMI(s). By default no additional users other than the
   133      user creating the AMI has permissions to launch it.
   134  
   135  -   `ami_virtualization_type` (string) - The type of virtualization for the AMI
   136      you are building. This option must match the supported virtualization
   137      type of `source_ami`. Can be `paravirtual` or `hvm`.
   138  
   139  -   `associate_public_ip_address` (boolean) - If using a non-default VPC, public
   140      IP addresses are not provided by default. If this is toggled, your new
   141      instance will get a Public IP.
   142  
   143  -   `availability_zone` (string) - Destination availability zone to launch
   144      instance in. Leave this empty to allow Amazon to auto-assign.
   145  
   146  -   `custom_endpoint_ec2` (string) - this option is useful if you use
   147      another cloud provider that provide a compatible API with aws EC2,
   148      specify another endpoint like this "<https://ec2.another.endpoint>..com"
   149  
   150  -   `disable_stop_instance` (boolean) - Packer normally stops the build instance
   151      after all provisioners have run. For Windows instances, it is sometimes
   152      desirable to [run Sysprep](http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ami-create-standard.html)
   153      which will stop the instance for you. If this is set to true, Packer *will not*
   154      stop the instance and will wait for you to stop it manually. You can do this
   155      with a [windows-shell provisioner](https://www.packer.io/docs/provisioners/windows-shell.html).
   156  
   157      ``` json
   158      {
   159        "type": "windows-shell",
   160        "inline": ["\"c:\\Program Files\\Amazon\\Ec2ConfigService\\ec2config.exe\" -sysprep"]
   161      }
   162      ```
   163  
   164  -   `ebs_optimized` (boolean) - Mark instance as [EBS
   165      Optimized](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSOptimized.html).
   166      Default `false`.
   167  
   168  -   `ena_support` (boolean) - Enable enhanced networking (ENA but not SriovNetSupport)
   169      on HVM-compatible AMIs. If true, add `ec2:ModifyInstanceAttribute` to your AWS IAM policy.
   170      Note: you must make sure enhanced networking is enabled on your instance. See [Amazon's
   171      documentation on enabling enhanced networking](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enhanced-networking.html#enabling_enhanced_networking). Default `false`.
   172  
   173  -   `force_deregister` (boolean) - Force Packer to first deregister an existing
   174      AMI if one with the same name already exists. Default `false`.
   175  
   176  -   `force_delete_snapshot` (boolean) - Force Packer to delete snapshots associated with
   177      AMIs, which have been deregistered by `force_deregister`. Default `false`.
   178  
   179  -   `encrypt_boot` (boolean) - Instruct packer to automatically create a copy of the
   180      AMI with an encrypted boot volume (discarding the initial unencrypted AMI in the
   181      process). Packer will always run this operation, even if the base
   182      AMI has an encrypted boot volume to start with. Default `false`.
   183  
   184  -   `kms_key_id` (string) - The ID of the KMS key to use for boot volume encryption.
   185      This only applies to the main `region`, other regions where the AMI will be copied
   186      will be encrypted by the default EBS KMS key.
   187  
   188  -   `iam_instance_profile` (string) - The name of an [IAM instance
   189      profile](https://docs.aws.amazon.com/IAM/latest/UserGuide/instance-profiles.html)
   190      to launch the EC2 instance with.
   191  
   192  -   `launch_block_device_mappings` (array of block device mappings) - Add one or
   193      more block devices before the Packer build starts. These are not necessarily
   194      preserved when booting from the AMI built with Packer. See
   195      `ami_block_device_mappings`, above, for details.
   196  
   197  -   `mfa_code` (string) - The MFA [TOTP](https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm)
   198      code. This should probably be a user variable since it changes all the time.
   199  
   200  -   `profile` (string) - The profile to use in the shared credentials file for
   201      AWS. See Amazon's documentation on [specifying
   202      profiles](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-profiles)
   203      for more details.
   204  
   205  -   `region_kms_key_ids` (map of strings) - a map of regions to copy the ami to,
   206      along with the custom kms key id to use for encryption for that region.
   207      Keys must match the regions provided in `ami_regions`. If you just want to
   208      encrypt using a default ID, you can stick with `kms_key_id` and `ami_regions`.
   209      If you want a region to be encrypted with that region's default key ID, you can
   210      use an empty string `""` instead of a key id in this map. (e.g. `"us-east-1": ""`)
   211      However, you cannot use default key IDs if you are using this in conjunction with
   212      `snapshot_users` -- in that situation you must use custom keys.
   213  
   214  -   `run_tags` (object of key/value strings) - Tags to apply to the instance
   215      that is *launched* to create the AMI. These tags are *not* applied to the
   216      resulting AMI unless they're duplicated in `tags`. This is a
   217      [template engine](/docs/templates/engine.html)
   218      where the `SourceAMI` variable is replaced with the source AMI ID and
   219      `BuildRegion` variable is replaced with the value of `region`.
   220  
   221  -   `run_volume_tags` (object of key/value strings) - Tags to apply to the volumes
   222      that are *launched* to create the AMI. These tags are *not* applied to the
   223      resulting AMI unless they're duplicated in `tags`. This is a
   224      [template engine](/docs/templates/engine.html)
   225      where the `SourceAMI` variable is replaced with the source AMI ID and
   226      `BuildRegion` variable is replaced with the value of `region`.
   227  
   228  -   `security_group_id` (string) - The ID (*not* the name) of the security group
   229      to assign to the instance. By default this is not set and Packer will
   230      automatically create a new temporary security group to allow SSH access.
   231      Note that if this is specified, you must be sure the security group allows
   232      access to the `ssh_port` given below.
   233  
   234  -   `security_group_ids` (array of strings) - A list of security groups as
   235      described above. Note that if this is specified, you must omit the
   236      `security_group_id`.
   237  
   238  -   `shutdown_behavior` (string) - Automatically terminate instances on shutdown
   239      in case Packer exits ungracefully. Possible values are "stop" and "terminate",
   240      default is `stop`.
   241  
   242  -   `skip_region_validation` (boolean) - Set to true if you want to skip
   243      validation of the region configuration option. Default `false`.
   244  
   245  -   `snapshot_groups` (array of strings) - A list of groups that have access to
   246      create volumes from the snapshot(s). By default no groups have permission to create
   247      volumes form the snapshot(s). `all` will make the snapshot publicly accessible.
   248  
   249  -   `snapshot_users` (array of strings) - A list of account IDs that have access to
   250      create volumes from the snapshot(s). By default no additional users other than the
   251      user creating the AMI has permissions to create volumes from the backing snapshot(s).
   252  
   253  -   `snapshot_tags` (object of key/value strings) - Tags to apply to snapshot.
   254      They will override AMI tags if already applied to snapshot. This is a
   255      [template engine](/docs/templates/engine.html)
   256      where the `SourceAMI` variable is replaced with the source AMI ID and
   257      `BuildRegion` variable is replaced with the value of `region`.
   258  
   259  -   `source_ami_filter` (object) - Filters used to populate the `source_ami` field.
   260      Example:
   261  
   262      ``` json
   263      {
   264        "source_ami_filter": {
   265          "filters": {
   266            "virtualization-type": "hvm",
   267            "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*",
   268            "root-device-type": "ebs"
   269          },
   270          "owners": ["099720109477"],
   271          "most_recent": true
   272        }
   273      }
   274      ```
   275  
   276      This selects the most recent Ubuntu 16.04 HVM EBS AMI from Canonical.
   277      NOTE: This will fail unless *exactly* one AMI is returned. In the above
   278      example, `most_recent` will cause this to succeed by selecting the newest image.
   279  
   280      -   `filters` (map of strings) - filters used to select a `source_ami`.
   281          NOTE: This will fail unless *exactly* one AMI is returned.
   282          Any filter described in the docs for [DescribeImages](http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeImages.html)
   283          is valid.
   284  
   285      -   `owners` (array of strings) - This scopes the AMIs to certain Amazon account IDs.
   286          This is helpful to limit the AMIs to a trusted third party, or to your own account.
   287  
   288      -   `most_recent` (bool) - Selects the newest created image when true.
   289          This is most useful for selecting a daily distro build.
   290  
   291  -   `spot_price` (string) - The maximum hourly price to pay for a spot instance
   292      to create the AMI. Spot instances are a type of instance that EC2 starts
   293      when the current spot price is less than the maximum price you specify. Spot
   294      price will be updated based on available spot instance capacity and current
   295      spot instance requests. It may save you some costs. You can set this to
   296      `auto` for Packer to automatically discover the best spot price or to "0"
   297      to use an on demand instance (default).
   298  
   299  -   `spot_price_auto_product` (string) - Required if `spot_price` is set
   300      to `auto`. This tells Packer what sort of AMI you're launching to find the
   301      best spot price. This must be one of: `Linux/UNIX`, `SUSE Linux`, `Windows`,
   302      `Linux/UNIX (Amazon VPC)`, `SUSE Linux (Amazon VPC)`, `Windows (Amazon VPC)`
   303  
   304  -   `sriov_support` (boolean) - Enable enhanced networking (SriovNetSupport but not ENA)
   305      on HVM-compatible AMIs. If true, add `ec2:ModifyInstanceAttribute` to your AWS IAM
   306      policy. Note: you must make sure enhanced networking is enabled on your instance. See [Amazon's
   307      documentation on enabling enhanced networking](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enhanced-networking.html#enabling_enhanced_networking).
   308      Default `false`.
   309  
   310  -   `ssh_keypair_name` (string) - If specified, this is the key that will be
   311      used for SSH with the machine. The key must match a key pair name loaded
   312      up into Amazon EC2. By default, this is blank, and Packer will
   313      generate a temporary keypair unless
   314      [`ssh_password`](/docs/templates/communicator.html#ssh_password) is used.
   315      [`ssh_private_key_file`](/docs/templates/communicator.html#ssh_private_key_file)
   316      or `ssh_agent_auth` must be specified when `ssh_keypair_name` is utilized.
   317  
   318  -   `ssh_agent_auth` (boolean) - If true, the local SSH agent will be used to
   319      authenticate connections to the source instance. No temporary keypair will
   320      be created, and the values of `ssh_password` and `ssh_private_key_file` will
   321      be ignored. To use this option with a key pair already configured in the source
   322      AMI, leave the `ssh_keypair_name` blank. To associate an existing key pair
   323      in AWS with the source instance, set the `ssh_keypair_name` field to the name
   324      of the key pair.
   325  
   326  -   `ssh_private_ip` (boolean) - If true, then SSH will always use the private
   327      IP if available. Also works for WinRM.
   328  
   329  -   `subnet_id` (string) - If using VPC, the ID of the subnet, such as
   330      `subnet-12345def`, where Packer will launch the EC2 instance. This field is
   331      required if you are using an non-default VPC.
   332  
   333  -   `tags` (object of key/value strings) - Tags applied to the AMI and
   334      relevant snapshots. This is a
   335      [template engine](/docs/templates/engine.html)
   336      where the `SourceAMI` variable is replaced with the source AMI ID and
   337      `BuildRegion` variable is replaced with the value of `region`.
   338  
   339  -   `temporary_key_pair_name` (string) - The name of the temporary key pair
   340      to generate. By default, Packer generates a name that looks like
   341      `packer_<UUID>`, where &lt;UUID&gt; is a 36 character unique identifier.
   342  
   343  -   `token` (string) - The access token to use. This is different from the
   344      access key and secret key. If you're not sure what this is, then you
   345      probably don't need it. This will also be read from the `AWS_SESSION_TOKEN`
   346      environmental variable.
   347  
   348  -   `user_data` (string) - User data to apply when launching the instance. Note
   349      that you need to be careful about escaping characters due to the templates
   350      being JSON. It is often more convenient to use `user_data_file`, instead.
   351  
   352  -   `user_data_file` (string) - Path to a file that will be used for the user
   353      data when launching the instance.
   354  
   355  -   `vpc_id` (string) - If launching into a VPC subnet, Packer needs the VPC ID
   356      in order to create a temporary security group within the VPC. Requires `subnet_id`
   357      to be set. If this field is left blank, Packer will try to get the VPC ID from the
   358      `subnet_id`.
   359  
   360  -   `windows_password_timeout` (string) - The timeout for waiting for a Windows
   361      password for Windows instances. Defaults to 20 minutes. Example value: `10m`
   362  
   363  ## Basic Example
   364  
   365  Here is a basic example. You will need to provide access keys, and may need to
   366  change the AMI IDs according to what images exist at the time the template is run:
   367  
   368  ``` json
   369  {
   370    "type": "amazon-ebs",
   371    "access_key": "YOUR KEY HERE",
   372    "secret_key": "YOUR SECRET KEY HERE",
   373    "region": "us-east-1",
   374    "source_ami": "ami-fce3c696",
   375    "instance_type": "t2.micro",
   376    "ssh_username": "ubuntu",
   377    "ami_name": "packer-quick-start {{timestamp}}"
   378  }
   379  ```
   380  
   381  -&gt; **Note:** Packer can also read the access key and secret access key from
   382  environmental variables. See the configuration reference in the section above
   383  for more information on what environmental variables Packer will look for.
   384  
   385  Further information on locating AMI IDs and their relationship to instance types
   386  and regions can be found in the AWS EC2 Documentation
   387  [for Linux](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html)
   388  or [for Windows](http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/finding-an-ami.html).
   389  
   390  ## Accessing the Instance to Debug
   391  
   392  If you need to access the instance to debug for some reason, run the builder
   393  with the `-debug` flag. In debug mode, the Amazon builder will save the private
   394  key in the current directory and will output the DNS or IP information as well.
   395  You can use this information to access the instance as it is running.
   396  
   397  ## AMI Block Device Mappings Example
   398  
   399  Here is an example using the optional AMI block device mappings. Our
   400  configuration of `launch_block_device_mappings` will expand the root volume
   401  (`/dev/sda`) to 40gb during the build (up from the default of 8gb). With
   402  `ami_block_device_mappings` AWS will attach additional volumes `/dev/sdb` and
   403  `/dev/sdc` when we boot a new instance of our AMI.
   404  
   405  ``` json
   406  {
   407    "type": "amazon-ebs",
   408    "access_key": "YOUR KEY HERE",
   409    "secret_key": "YOUR SECRET KEY HERE",
   410    "region": "us-east-1",
   411    "source_ami": "ami-fce3c696",
   412    "instance_type": "t2.micro",
   413    "ssh_username": "ubuntu",
   414    "ami_name": "packer-quick-start {{timestamp}}",
   415    "launch_block_device_mappings": [
   416      {
   417        "device_name": "/dev/sda1",
   418        "volume_size": 40,
   419        "volume_type": "gp2",
   420        "delete_on_termination": true
   421      }
   422    ],
   423    "ami_block_device_mappings": [
   424      {
   425        "device_name": "/dev/sdb",
   426        "virtual_name": "ephemeral0"
   427      },
   428      {
   429        "device_name": "/dev/sdc",
   430        "virtual_name": "ephemeral1"
   431      }
   432    ]
   433  }
   434  ```
   435  
   436  ## Tag Example
   437  
   438  Here is an example using the optional AMI tags. This will add the tags
   439  `OS_Version` and `Release` to the finished AMI. As before, you will need to
   440  provide your access keys, and may need to change the source AMI ID based on what
   441  images exist when this template is run:
   442  
   443  ``` json
   444  {
   445    "type": "amazon-ebs",
   446    "access_key": "YOUR KEY HERE",
   447    "secret_key": "YOUR SECRET KEY HERE",
   448    "region": "us-east-1",
   449    "source_ami": "ami-fce3c696",
   450    "instance_type": "t2.micro",
   451    "ssh_username": "ubuntu",
   452    "ami_name": "packer-quick-start {{timestamp}}",
   453    "tags": {
   454      "OS_Version": "Ubuntu",
   455      "Release": "Latest"
   456    }
   457  }
   458  ```
   459  
   460  -&gt; **Note:** Packer uses pre-built AMIs as the source for building images.
   461  These source AMIs may include volumes that are not flagged to be destroyed on
   462  termination of the instance building the new image. Packer will attempt to clean
   463  up all residual volumes that are not designated by the user to remain after
   464  termination. If you need to preserve those source volumes, you can overwrite the
   465  termination setting by specifying `delete_on_termination=false` in the
   466  `launch_block_device_mappings` block for the device.