2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
|
|
|
|
2013-08-09 01:50:23 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-30 17:08:48 -05:00
|
|
|
"log"
|
2018-12-10 14:13:28 -05:00
|
|
|
"regexp"
|
2017-04-04 21:02:23 -04:00
|
|
|
|
2019-08-19 12:48:32 -04:00
|
|
|
"github.com/hashicorp/packer/helper/config"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-08-09 01:50:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// AMIConfig is for common configuration related to creating AMIs.
|
|
|
|
type AMIConfig struct {
|
2019-09-20 09:04:01 -04:00
|
|
|
// The name of the resulting AMI that will appear when managing AMIs in the
|
|
|
|
// AWS console or via APIs. This must be unique. To help make this unique,
|
|
|
|
// use a function like timestamp (see [template
|
2019-06-03 11:55:09 -04:00
|
|
|
// engine](../templates/engine.html) for more info).
|
|
|
|
AMIName string `mapstructure:"ami_name" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The description to set for the resulting
|
2019-06-03 11:55:09 -04:00
|
|
|
// AMI(s). By default this description is empty. This is a template
|
|
|
|
// engine, see Build template
|
|
|
|
// data for more information.
|
|
|
|
AMIDescription string `mapstructure:"ami_description" required:"false"`
|
2019-10-24 13:19:03 -04:00
|
|
|
// The type of virtualization for the AMI
|
|
|
|
// you are building. This option is required to register HVM images. Can be
|
|
|
|
// paravirtual (default) or hvm.
|
2019-06-03 11:55:09 -04:00
|
|
|
AMIVirtType string `mapstructure:"ami_virtualization_type" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// A list of account IDs that have access to
|
2019-06-03 11:55:09 -04:00
|
|
|
// launch the resulting AMI(s). By default no additional users other than the
|
|
|
|
// user creating the AMI has permissions to launch it.
|
|
|
|
AMIUsers []string `mapstructure:"ami_users" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// A list of groups that have access to
|
2019-06-03 11:55:09 -04:00
|
|
|
// launch the resulting AMI(s). By default no groups have permission to launch
|
|
|
|
// the AMI. all will make the AMI publicly accessible.
|
|
|
|
AMIGroups []string `mapstructure:"ami_groups" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// A list of product codes to
|
2019-06-03 11:55:09 -04:00
|
|
|
// associate with the AMI. By default no product codes are associated with the
|
|
|
|
// AMI.
|
|
|
|
AMIProductCodes []string `mapstructure:"ami_product_codes" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// A list of regions to copy the AMI to.
|
2019-06-03 11:55:09 -04:00
|
|
|
// Tags and attributes are copied along with the AMI. AMI copying takes time
|
|
|
|
// depending on the size of the AMI, but will generally take many minutes.
|
|
|
|
AMIRegions []string `mapstructure:"ami_regions" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Set to true if you want to skip
|
2019-06-03 11:55:09 -04:00
|
|
|
// validation of the ami_regions configuration option. Default false.
|
|
|
|
AMISkipRegionValidation bool `mapstructure:"skip_region_validation" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Tags applied to the AMI. This is a
|
2019-06-03 11:55:09 -04:00
|
|
|
// [template engine](/docs/templates/engine.html), see [Build template
|
|
|
|
// data](#build-template-data) for more information.
|
|
|
|
AMITags TagMap `mapstructure:"tags" required:"false"`
|
2019-09-20 09:04:01 -04:00
|
|
|
// Enable enhanced networking (ENA but not SriovNetSupport) on
|
|
|
|
// HVM-compatible AMIs. If set, add `ec2:ModifyInstanceAttribute` to your
|
|
|
|
// AWS IAM policy.
|
|
|
|
//
|
|
|
|
// Note: you must make sure enhanced networking is enabled on your
|
|
|
|
// instance. See [Amazon's documentation on enabling enhanced
|
2019-06-03 11:55:09 -04:00
|
|
|
// networking](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enhanced-networking.html#enabling_enhanced_networking).
|
2019-09-20 04:54:38 -04:00
|
|
|
AMIENASupport config.Trilean `mapstructure:"ena_support" required:"false"`
|
2019-06-06 10:45:37 -04:00
|
|
|
// Enable enhanced networking (SriovNetSupport but not ENA) on
|
|
|
|
// HVM-compatible AMIs. If true, add `ec2:ModifyInstanceAttribute` to your
|
|
|
|
// AWS IAM policy. Note: you must make sure enhanced networking is enabled
|
|
|
|
// on your instance. See [Amazon's documentation on enabling enhanced
|
|
|
|
// networking](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enhanced-networking.html#enabling_enhanced_networking).
|
|
|
|
// Default `false`.
|
2019-06-03 11:55:09 -04:00
|
|
|
AMISriovNetSupport bool `mapstructure:"sriov_support" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Force Packer to first deregister an existing
|
2019-06-03 11:55:09 -04:00
|
|
|
// AMI if one with the same name already exists. Default false.
|
|
|
|
AMIForceDeregister bool `mapstructure:"force_deregister" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Force Packer to delete snapshots
|
2019-06-03 11:55:09 -04:00
|
|
|
// associated with AMIs, which have been deregistered by force_deregister.
|
|
|
|
// Default false.
|
|
|
|
AMIForceDeleteSnapshot bool `mapstructure:"force_delete_snapshot" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Whether or not to encrypt the resulting AMI when
|
2019-06-03 11:55:09 -04:00
|
|
|
// copying a provisioned instance to an AMI. By default, Packer will keep the
|
|
|
|
// encryption setting to what it was in the source image. Setting false will
|
|
|
|
// result in an unencrypted image, and true will result in an encrypted one.
|
2019-09-27 12:51:11 -04:00
|
|
|
// If you have used the `launch_block_device_mappings` to set an encryption
|
|
|
|
// key and that key is the same as the one you want the image encrypted with
|
|
|
|
// at the end, then you don't need to set this field; leaving it empty will
|
|
|
|
// prevent an unnecessary extra copy step and save you some time.
|
2019-09-20 04:54:38 -04:00
|
|
|
AMIEncryptBootVolume config.Trilean `mapstructure:"encrypt_boot" required:"false"`
|
2019-06-03 11:55:09 -04:00
|
|
|
// ID, alias or ARN of the KMS key to use for boot volume encryption. This
|
|
|
|
// only applies to the main `region`, other regions where the AMI will be
|
|
|
|
// copied will be encrypted by the default EBS KMS key. For valid formats
|
|
|
|
// see *KmsKeyId* in the [AWS API docs -
|
|
|
|
// CopyImage](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CopyImage.html).
|
|
|
|
// This field is validated by Packer, when using an alias, you will have to
|
|
|
|
// prefix `kms_key_id` with `alias/`.
|
|
|
|
AMIKmsKeyId string `mapstructure:"kms_key_id" required:"false"`
|
2019-06-06 10:45:37 -04:00
|
|
|
// regions to copy the ami to, along with the custom kms key id (alias or
|
|
|
|
// arn) to use for encryption for that region. Keys must match the regions
|
|
|
|
// provided in `ami_regions`. If you just want to encrypt using a default
|
|
|
|
// ID, you can stick with `kms_key_id` and `ami_regions`. If you want a
|
2019-06-03 11:55:09 -04:00
|
|
|
// region to be encrypted with that region's default key ID, you can use an
|
2019-06-06 10:45:37 -04:00
|
|
|
// empty string `""` instead of a key id in this map. (e.g. `"us-east-1":
|
|
|
|
// ""`) However, you cannot use default key IDs if you are using this in
|
|
|
|
// conjunction with `snapshot_users` -- in that situation you must use
|
|
|
|
// custom keys. For valid formats see *KmsKeyId* in the [AWS API docs -
|
|
|
|
// CopyImage](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CopyImage.html).
|
2019-08-21 06:28:34 -04:00
|
|
|
//
|
|
|
|
// This option supercedes the `kms_key_id` option -- if you set both, and
|
|
|
|
// they are different, Packer will respect the value in
|
|
|
|
// `region_kms_key_ids` for your build region and silently disregard the
|
|
|
|
// value provided in `kms_key_id`.
|
2019-06-03 11:55:09 -04:00
|
|
|
AMIRegionKMSKeyIDs map[string]string `mapstructure:"region_kms_key_ids" required:"false"`
|
2019-08-21 06:28:34 -04:00
|
|
|
// If true, Packer will not check whether an AMI with the `ami_name` exists
|
|
|
|
// in the region it is building in. It will use an intermediary AMI name,
|
|
|
|
// which it will not convert to an AMI in the build region. It will copy
|
|
|
|
// the intermediary AMI into any regions provided in `ami_regions`, then
|
|
|
|
// delete the intermediary AMI. Default `false`.
|
|
|
|
AMISkipBuildRegion bool `mapstructure:"skip_save_build_region"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Tags to apply to snapshot.
|
2019-06-03 11:55:09 -04:00
|
|
|
// They will override AMI tags if already applied to snapshot. This is a
|
2019-06-06 10:45:37 -04:00
|
|
|
// [template engine](../templates/engine.html), see [Build template
|
|
|
|
// data](#build-template-data) for more information.
|
2019-06-03 11:55:09 -04:00
|
|
|
SnapshotTags TagMap `mapstructure:"snapshot_tags" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// A list of account IDs that have
|
2019-06-03 11:55:09 -04:00
|
|
|
// access to create volumes from the snapshot(s). By default no additional
|
|
|
|
// users other than the user creating the AMI has permissions to create
|
|
|
|
// volumes from the backing snapshot(s).
|
|
|
|
SnapshotUsers []string `mapstructure:"snapshot_users" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// A list of groups that have access to
|
2019-06-03 11:55:09 -04:00
|
|
|
// create volumes from the snapshot(s). By default no groups have permission
|
|
|
|
// to create volumes from the snapshot(s). all will make the snapshot
|
|
|
|
// publicly accessible.
|
|
|
|
SnapshotGroups []string `mapstructure:"snapshot_groups" required:"false"`
|
2013-08-09 01:50:23 -04:00
|
|
|
}
|
|
|
|
|
2017-05-31 16:41:32 -04:00
|
|
|
func stringInSlice(s []string, searchstr string) bool {
|
|
|
|
for _, item := range s {
|
2017-05-25 17:42:03 -04:00
|
|
|
if item == searchstr {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-30 17:17:19 -04:00
|
|
|
func (c *AMIConfig) Prepare(accessConfig *AccessConfig, ctx *interpolate.Context) []error {
|
2015-05-27 14:35:56 -04:00
|
|
|
var errs []error
|
2017-10-30 17:17:19 -04:00
|
|
|
|
2013-08-09 01:50:23 -04:00
|
|
|
if c.AMIName == "" {
|
|
|
|
errs = append(errs, fmt.Errorf("ami_name must be specified"))
|
|
|
|
}
|
|
|
|
|
2017-11-30 16:51:33 -05:00
|
|
|
// Make sure that if we have region_kms_key_ids defined,
|
|
|
|
// the regions in region_kms_key_ids are also in ami_regions
|
|
|
|
if len(c.AMIRegionKMSKeyIDs) > 0 {
|
|
|
|
for kmsKeyRegion := range c.AMIRegionKMSKeyIDs {
|
|
|
|
if !stringInSlice(c.AMIRegions, kmsKeyRegion) {
|
|
|
|
errs = append(errs, fmt.Errorf("Region %s is in region_kms_key_ids but not in ami_regions", kmsKeyRegion))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 05:26:53 -04:00
|
|
|
errs = append(errs, c.prepareRegions(accessConfig)...)
|
2018-09-18 11:29:20 -04:00
|
|
|
|
2019-05-30 18:25:36 -04:00
|
|
|
// Prevent sharing of default KMS key encrypted volumes with other aws users
|
2019-05-30 18:16:36 -04:00
|
|
|
if len(c.AMIUsers) > 0 {
|
2019-08-22 17:04:26 -04:00
|
|
|
if len(c.AMIKmsKeyId) == 0 && c.AMIEncryptBootVolume.True() {
|
2019-05-30 18:16:36 -04:00
|
|
|
errs = append(errs, fmt.Errorf("Cannot share AMI encrypted with default KMS key"))
|
|
|
|
}
|
|
|
|
if len(c.AMIRegionKMSKeyIDs) > 0 {
|
|
|
|
for _, kmsKey := range c.AMIRegionKMSKeyIDs {
|
|
|
|
if len(kmsKey) == 0 {
|
|
|
|
errs = append(errs, fmt.Errorf("Cannot share AMI encrypted with default KMS key for other regions"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 11:29:20 -04:00
|
|
|
}
|
|
|
|
|
2019-10-25 19:09:20 -04:00
|
|
|
kmsKeys := make([]string, 0)
|
2018-12-10 14:13:28 -05:00
|
|
|
if len(c.AMIKmsKeyId) > 0 {
|
|
|
|
kmsKeys = append(kmsKeys, c.AMIKmsKeyId)
|
|
|
|
}
|
|
|
|
if len(c.AMIRegionKMSKeyIDs) > 0 {
|
|
|
|
for _, kmsKey := range c.AMIRegionKMSKeyIDs {
|
2019-10-25 19:09:20 -04:00
|
|
|
if len(kmsKey) > 0 {
|
|
|
|
kmsKeys = append(kmsKeys, kmsKey)
|
2018-12-10 14:13:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-25 19:09:20 -04:00
|
|
|
|
|
|
|
if len(kmsKeys) > 0 && !c.AMIEncryptBootVolume.True() {
|
|
|
|
errs = append(errs, fmt.Errorf("If you have set either "+
|
|
|
|
"region_kms_key_ids or kms_key_id, encrypt_boot must also be true."))
|
|
|
|
|
|
|
|
}
|
2018-12-10 14:13:28 -05:00
|
|
|
for _, kmsKey := range kmsKeys {
|
|
|
|
if !validateKmsKey(kmsKey) {
|
|
|
|
errs = append(errs, fmt.Errorf("%s is not a valid KMS Key Id.", kmsKey))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 11:29:20 -04:00
|
|
|
if len(c.SnapshotUsers) > 0 {
|
2019-10-25 19:09:20 -04:00
|
|
|
if len(c.AMIKmsKeyId) == 0 && len(c.AMIRegionKMSKeyIDs) == 0 && c.AMIEncryptBootVolume.True() {
|
|
|
|
errs = append(errs, fmt.Errorf("Cannot share snapshot encrypted "+
|
2019-10-30 12:34:42 -04:00
|
|
|
"with default KMS key, see https://www.packer.io/docs/builders/amazon-ebs.html#region_kms_key_ids for more information"))
|
2018-09-18 11:29:20 -04:00
|
|
|
}
|
|
|
|
if len(c.AMIRegionKMSKeyIDs) > 0 {
|
|
|
|
for _, kmsKey := range c.AMIRegionKMSKeyIDs {
|
|
|
|
if len(kmsKey) == 0 {
|
|
|
|
errs = append(errs, fmt.Errorf("Cannot share snapshot encrypted with default KMS key"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.AMIName) < 3 || len(c.AMIName) > 128 {
|
|
|
|
errs = append(errs, fmt.Errorf("ami_name must be between 3 and 128 characters long"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.AMIName != templateCleanAMIName(c.AMIName) {
|
|
|
|
errs = append(errs, fmt.Errorf("AMIName should only contain "+
|
|
|
|
"alphanumeric characters, parentheses (()), square brackets ([]), spaces "+
|
|
|
|
"( ), periods (.), slashes (/), dashes (-), single quotes ('), at-signs "+
|
2019-04-03 04:27:05 -04:00
|
|
|
"(@), or underscores(_). You can use the `clean_resource_name` template "+
|
2018-09-18 11:29:20 -04:00
|
|
|
"filter to automatically clean your ami name."))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-24 05:26:53 -04:00
|
|
|
func (c *AMIConfig) prepareRegions(accessConfig *AccessConfig) (errs []error) {
|
2013-08-21 21:44:14 -04:00
|
|
|
if len(c.AMIRegions) > 0 {
|
2013-08-22 17:58:21 -04:00
|
|
|
regionSet := make(map[string]struct{})
|
|
|
|
regions := make([]string, 0, len(c.AMIRegions))
|
|
|
|
|
2013-08-21 21:44:14 -04:00
|
|
|
for _, region := range c.AMIRegions {
|
2013-08-22 17:58:21 -04:00
|
|
|
// If we already saw the region, then don't look again
|
|
|
|
if _, ok := regionSet[region]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark that we saw the region
|
|
|
|
regionSet[region] = struct{}{}
|
|
|
|
|
2017-05-31 16:41:32 -04:00
|
|
|
// Make sure that if we have region_kms_key_ids defined,
|
|
|
|
// the regions in ami_regions are also in region_kms_key_ids
|
|
|
|
if len(c.AMIRegionKMSKeyIDs) > 0 {
|
|
|
|
if _, ok := c.AMIRegionKMSKeyIDs[region]; !ok {
|
2017-05-25 17:42:03 -04:00
|
|
|
errs = append(errs, fmt.Errorf("Region %s is in ami_regions but not in region_kms_key_ids", region))
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 16:51:33 -05:00
|
|
|
if (accessConfig != nil) && (region == accessConfig.RawRegion) {
|
|
|
|
// make sure we don't try to copy to the region we originally
|
|
|
|
// create the AMI in.
|
2017-11-30 17:08:48 -05:00
|
|
|
log.Printf("Cannot copy AMI to AWS session region '%s', deleting it from `ami_regions`.", region)
|
2017-11-30 16:51:33 -05:00
|
|
|
continue
|
|
|
|
}
|
2013-08-22 17:58:21 -04:00
|
|
|
regions = append(regions, region)
|
2013-08-21 21:44:14 -04:00
|
|
|
}
|
2013-08-22 17:58:21 -04:00
|
|
|
|
|
|
|
c.AMIRegions = regions
|
2013-08-21 21:44:14 -04:00
|
|
|
}
|
2018-09-18 11:29:20 -04:00
|
|
|
return errs
|
2013-08-09 01:50:23 -04:00
|
|
|
}
|
2018-12-10 14:13:28 -05:00
|
|
|
|
|
|
|
// See https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CopyImage.html
|
|
|
|
func validateKmsKey(kmsKey string) (valid bool) {
|
|
|
|
kmsKeyIdPattern := `[a-f0-9-]+$`
|
|
|
|
aliasPattern := `alias/[a-zA-Z0-9:/_-]+$`
|
2019-02-26 03:36:57 -05:00
|
|
|
kmsArnStartPattern := `^arn:aws(-us-gov)?:kms:([a-z]{2}-(gov-)?[a-z]+-\d{1})?:(\d{12}):`
|
2018-12-10 14:13:28 -05:00
|
|
|
if regexp.MustCompile(fmt.Sprintf("^%s", kmsKeyIdPattern)).MatchString(kmsKey) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if regexp.MustCompile(fmt.Sprintf("^%s", aliasPattern)).MatchString(kmsKey) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if regexp.MustCompile(fmt.Sprintf("%skey/%s", kmsArnStartPattern, kmsKeyIdPattern)).MatchString(kmsKey) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if regexp.MustCompile(fmt.Sprintf("%s%s", kmsArnStartPattern, aliasPattern)).MatchString(kmsKey) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|