packer-cn/website/source/docs/builders/amazon-ebs.html.md.erb

250 lines
8.0 KiB
Plaintext
Raw Normal View History

2013-06-10 17:31:31 -04:00
---
2017-06-14 21:04:16 -04:00
description: |
The amazon-ebs Packer builder is able to create Amazon AMIs backed by EBS
volumes for use in EC2. For more information on the difference between
EBS-backed instances and instance-store backed instances, see the storage for
the root device section in the EC2 documentation.
2015-07-22 22:31:00 -04:00
layout: docs
2017-06-14 21:04:16 -04:00
page_title: 'Amazon EBS - Builders'
sidebar_current: 'docs-builders-amazon-ebsbacked'
---
2013-06-10 17:31:31 -04:00
# AMI Builder (EBS backed)
2013-06-10 17:31:31 -04:00
Type: `amazon-ebs`
The `amazon-ebs` Packer builder is able to create Amazon AMIs backed by EBS
2016-01-14 15:31:19 -05:00
volumes for use in [EC2](https://aws.amazon.com/ec2/). For more information on
2018-10-26 20:02:51 -04:00
the difference between EBS-backed instances and instance-store backed
instances, see the ["storage for the root device" section in the EC2
2016-01-14 15:31:19 -05:00
documentation](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ComponentsAMIs.html#storage-for-the-root-device).
2013-06-10 17:31:31 -04:00
This builder builds an AMI by launching an EC2 instance from a source AMI,
provisioning that running machine, and then creating an AMI from that machine.
This is all done in your own AWS account. The builder will create temporary
2015-07-22 22:31:00 -04:00
keypairs, security group rules, etc. that provide it temporary access to the
2018-10-26 20:02:51 -04:00
instance while the image is being created. This simplifies configuration quite
a bit.
2013-06-10 17:31:31 -04:00
2018-10-26 20:02:51 -04:00
The builder does *not* manage AMIs. Once it creates an AMI and stores it in
your account, it is up to you to use, delete, etc. the AMI.
2013-06-10 17:31:31 -04:00
2018-10-26 20:02:51 -04:00
-> **Note:** Temporary resources are, by default, all created with the
prefix `packer`. This can be useful if you want to restrict the security groups
and key pairs Packer is able to operate on.
2019-06-18 11:37:33 -04:00
## EBS Specific Configuration Reference
2013-06-10 17:31:31 -04:00
2019-06-18 11:37:33 -04:00
There are many configuration options available for the builder. In addition to
the items listed here, you will want to look at the general configuration
references for [AMI](#ami-configuration),
[BlockDevices](#block-devices-configuration),
[Access](#acces-configuration),
[Run](#run-configuration) and
[Communicator](#communicator-configuration)
configuration references, which are
necessary for this build to succeed and can be found further down the page.
2013-06-10 17:31:31 -04:00
2019-06-18 11:37:33 -04:00
#### Optional:
2019-06-18 11:37:33 -04:00
<%= partial "partials/builder/amazon/ebs/Config-not-required" %>
### AMI Configuration
#### Required:
<%= partial "partials/builder/amazon/common/AMIConfig-required" %>
2019-06-18 11:37:33 -04:00
#### Optional:
<%= partial "partials/builder/amazon/common/AMIConfig-not-required" %>
### Acces Configuration
#### Required:
2013-06-10 17:31:31 -04:00
2019-06-06 10:46:12 -04:00
<%= partial "partials/builder/amazon/common/AccessConfig-required" %>
2013-06-10 17:31:31 -04:00
2019-06-18 11:37:33 -04:00
#### Optional:
2013-06-10 17:31:31 -04:00
2019-06-06 10:46:12 -04:00
<%= partial "partials/builder/amazon/common/AccessConfig-not-required" %>
2019-06-18 11:37:33 -04:00
### Run Configuration
#### Required:
<%= partial "partials/builder/amazon/common/RunConfig-required" %>
#### Optional:
2019-06-06 10:46:12 -04:00
<%= partial "partials/builder/amazon/common/RunConfig-not-required" %>
2019-06-18 11:37:33 -04:00
### Block Devices Configuration
Block devices can be nested in the
[ami_block_device_mappings](#ami_block_device_mappings) or the
2019-06-18 12:00:49 -04:00
[launch_block_device_mappings](#launch_block_device_mappings) array.
2019-06-18 11:37:33 -04:00
<%= partial "partials/builder/amazon/common/BlockDevice" %>
#### Optional:
<%= partial "partials/builder/amazon/common/BlockDevice-not-required" %>
### Communicator Configuration
#### Optional:
2019-06-06 10:46:12 -04:00
<%= partial "partials/helper/communicator/Config-not-required" %>
<%= partial "partials/helper/communicator/SSH-not-required" %>
2013-06-10 17:31:31 -04:00
## Basic Example
Here is a basic example. You will need to provide access keys, and may need to
2018-10-26 20:02:51 -04:00
change the AMI IDs according to what images exist at the time the template is
run:
2013-06-10 17:31:31 -04:00
2017-06-14 21:04:16 -04:00
``` json
2013-06-10 17:31:31 -04:00
{
2018-11-29 19:16:25 -05:00
"variables": {
"aws_access_key": "{{env `AWS_ACCESS_KEY_ID`}}",
"aws_secret_key": "{{env `AWS_SECRET_ACCESS_KEY`}}"
},
"builders": [
{
2018-11-29 19:16:25 -05:00
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "us-east-1",
"source_ami": "ami-fce3c696",
"instance_type": "t2.micro",
"ssh_username": "ubuntu",
"ami_name": "packer_AWS {{timestamp}}"
}
2018-11-29 19:16:25 -05:00
]
2013-06-10 17:31:31 -04:00
}
```
2013-06-10 17:31:31 -04:00
2017-06-14 21:04:16 -04:00
-&gt; **Note:** Packer can also read the access key and secret access key from
2015-07-22 22:31:00 -04:00
environmental variables. See the configuration reference in the section above
for more information on what environmental variables Packer will look for.
2013-07-01 18:26:08 -04:00
2018-10-26 20:02:51 -04:00
Further information on locating AMI IDs and their relationship to instance
types and regions can be found in the AWS EC2 Documentation [for
Linux](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html)
or [for
Windows](http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/finding-an-ami.html).
## Accessing the Instance to Debug
If you need to access the instance to debug for some reason, run the builder
2015-07-22 22:31:00 -04:00
with the `-debug` flag. In debug mode, the Amazon builder will save the private
key in the current directory and will output the DNS or IP information as well.
You can use this information to access the instance as it is running.
## AMI Block Device Mappings Example
Here is an example using the optional AMI block device mappings. Our
configuration of `launch_block_device_mappings` will expand the root volume
(`/dev/sda`) to 40gb during the build (up from the default of 8gb). With
`ami_block_device_mappings` AWS will attach additional volumes `/dev/sdb` and
`/dev/sdc` when we boot a new instance of our AMI.
2017-06-14 21:04:16 -04:00
``` json
{
"type": "amazon-ebs",
"access_key": "YOUR KEY HERE",
"secret_key": "YOUR SECRET KEY HERE",
"region": "us-east-1",
"source_ami": "ami-fce3c696",
"instance_type": "t2.micro",
"ssh_username": "ubuntu",
"ami_name": "packer-quick-start {{timestamp}}",
"launch_block_device_mappings": [
{
"device_name": "/dev/sda1",
"volume_size": 40,
"volume_type": "gp2",
"delete_on_termination": true
}
],
"ami_block_device_mappings": [
{
"device_name": "/dev/sdb",
"virtual_name": "ephemeral0"
},
{
"device_name": "/dev/sdc",
"virtual_name": "ephemeral1"
}
]
}
```
## Build template data
2018-10-26 20:02:51 -04:00
In configuration directives marked as a template engine above, the following
variables are available:
2018-10-26 20:02:51 -04:00
- `BuildRegion` - The region (for example `eu-central-1`) where Packer is
building the AMI.
- `SourceAMI` - The source AMI ID (for example `ami-a2412fcd`) used to build
the AMI.
- `SourceAMIName` - The source AMI Name (for example
`ubuntu/images/ebs-ssd/ubuntu-xenial-16.04-amd64-server-20180306`) used to
build the AMI.
- `SourceAMITags` - The source AMI Tags, as a `map[string]string` object.
## Tag Example
Here is an example using the optional AMI tags. This will add the tags
`OS_Version` and `Release` to the finished AMI. As before, you will need to
2018-10-26 20:02:51 -04:00
provide your access keys, and may need to change the source AMI ID based on
what images exist when this template is run:
2017-06-14 21:04:16 -04:00
``` json
{
"type": "amazon-ebs",
"access_key": "YOUR KEY HERE",
"secret_key": "YOUR SECRET KEY HERE",
"region": "us-east-1",
"source_ami": "ami-fce3c696",
"instance_type": "t2.micro",
"ssh_username": "ubuntu",
"ami_name": "packer-quick-start {{timestamp}}",
"tags": {
2017-07-22 02:14:27 -04:00
"OS_Version": "Ubuntu",
"Release": "Latest",
"Base_AMI_Name": "{{ .SourceAMIName }}",
"Extra": "{{ .SourceAMITags.TagName }}"
}
}
```
2015-06-15 12:53:03 -04:00
2017-06-14 21:04:16 -04:00
-&gt; **Note:** Packer uses pre-built AMIs as the source for building images.
These source AMIs may include volumes that are not flagged to be destroyed on
2018-10-26 20:02:51 -04:00
termination of the instance building the new image. Packer will attempt to
clean up all residual volumes that are not designated by the user to remain
after termination. If you need to preserve those source volumes, you can
overwrite the termination setting by specifying `delete_on_termination=false`
in the `launch_block_device_mappings` block for the device.
## Windows 2016 Sysprep Commands - For Amazon Windows AMIs Only
2018-10-26 20:02:51 -04:00
For Amazon Windows 2016 AMIs it is necessary to run Sysprep commands which can
be easily added to the provisioner section.
2018-10-26 20:02:51 -04:00
``` json
{
"type": "powershell",
"inline": [
"C:/ProgramData/Amazon/EC2-Windows/Launch/Scripts/InitializeInstance.ps1 -Schedule",
"C:/ProgramData/Amazon/EC2-Windows/Launch/Scripts/SysprepInstance.ps1 -NoShutdown"
]
}
```
<%= partial "partials/builders/aws-ssh-differentiation-table" %>