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

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