2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
2019-12-17 05:25:56 -05:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
2019-05-31 08:27:41 -04:00
|
|
|
|
2013-07-15 02:56:28 -04:00
|
|
|
// The instance package contains a packer.Builder implementation that builds
|
|
|
|
// AMIs for Amazon EC2 backed by instance storage, as opposed to EBS storage.
|
|
|
|
package instance
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2013-07-21 00:00:12 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-06-04 17:58:11 -04:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2019-10-31 06:31:17 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-04-04 16:39:01 -04:00
|
|
|
awscommon "github.com/hashicorp/packer/builder/amazon/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/common"
|
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/commonsteps"
|
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/packerbuilderdata"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2013-07-15 02:56:28 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// The unique ID for this builder
|
|
|
|
const BuilderId = "mitchellh.amazon.instance"
|
|
|
|
|
2019-06-18 12:08:29 -04:00
|
|
|
// Config is the configuration that is chained through the steps and settable
|
|
|
|
// from the template.
|
2013-07-15 02:56:28 -04:00
|
|
|
type Config struct {
|
2013-07-20 22:40:45 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
awscommon.AccessConfig `mapstructure:",squash"`
|
2013-08-09 01:50:23 -04:00
|
|
|
awscommon.AMIConfig `mapstructure:",squash"`
|
2013-07-20 22:40:45 -04:00
|
|
|
awscommon.RunConfig `mapstructure:",squash"`
|
2019-06-18 12:08:29 -04:00
|
|
|
|
|
|
|
// Add one or more block device mappings to the AMI. These will be attached
|
|
|
|
// when booting a new instance from your AMI. To add a block device during
|
|
|
|
// the Packer build see `launch_block_device_mappings` below. Your options
|
|
|
|
// here may vary depending on the type of VM you use. See the
|
|
|
|
// [BlockDevices](#block-devices-configuration) documentation for fields.
|
|
|
|
AMIMappings awscommon.BlockDevices `mapstructure:"ami_block_device_mappings" required:"false"`
|
|
|
|
// Add one or more block devices before the Packer build starts. If you add
|
|
|
|
// instance store volumes or EBS volumes in addition to the root device
|
|
|
|
// volume, the created AMI will contain block device mapping information
|
|
|
|
// for those volumes. Amazon creates snapshots of the source instance's
|
|
|
|
// root volume and any other EBS volumes described here. When you launch an
|
|
|
|
// instance from this new AMI, the instance automatically launches with
|
|
|
|
// these additional volumes, and will restore them from snapshots taken
|
|
|
|
// from the source instance. See the
|
|
|
|
// [BlockDevices](#block-devices-configuration) documentation for fields.
|
|
|
|
LaunchMappings awscommon.BlockDevices `mapstructure:"launch_block_device_mappings" required:"false"`
|
|
|
|
// Your AWS account ID. This is required for bundling the AMI. This is not
|
|
|
|
// the same as the access key. You can find your account ID in the security
|
|
|
|
// credentials page of your AWS account.
|
2019-06-06 10:29:25 -04:00
|
|
|
AccountId string `mapstructure:"account_id" required:"true"`
|
2019-06-18 12:08:29 -04:00
|
|
|
// The directory on the running instance where the bundled AMI will be
|
|
|
|
// saved prior to uploading. By default this is /tmp. This directory must
|
|
|
|
// exist and be writable.
|
2019-06-06 10:29:25 -04:00
|
|
|
BundleDestination string `mapstructure:"bundle_destination" required:"false"`
|
2019-06-18 12:08:29 -04:00
|
|
|
// The prefix for files created from bundling the root volume. By default
|
2020-04-01 23:30:51 -04:00
|
|
|
// this is `image-{{timestamp}}`. The timestamp variable should be used to
|
2019-06-18 12:08:29 -04:00
|
|
|
// make sure this is unique, otherwise it can collide with other created
|
|
|
|
// AMIs by Packer in your account.
|
2019-06-06 10:29:25 -04:00
|
|
|
BundlePrefix string `mapstructure:"bundle_prefix" required:"false"`
|
2019-06-18 12:08:29 -04:00
|
|
|
// The command to use to upload the bundled volume. See the "custom bundle
|
|
|
|
// commands" section below for more information.
|
2019-05-28 11:50:58 -04:00
|
|
|
BundleUploadCommand string `mapstructure:"bundle_upload_command" required:"false"`
|
2019-06-18 12:08:29 -04:00
|
|
|
// The command to use to bundle the volume. See the "custom bundle
|
|
|
|
// commands" section below for more information.
|
2019-06-06 10:29:25 -04:00
|
|
|
BundleVolCommand string `mapstructure:"bundle_vol_command" required:"false"`
|
2019-06-18 12:08:29 -04:00
|
|
|
// The name of the S3 bucket to upload the AMI. This bucket will be created
|
|
|
|
// if it doesn't exist.
|
2019-06-06 10:29:25 -04:00
|
|
|
S3Bucket string `mapstructure:"s3_bucket" required:"true"`
|
2019-06-18 12:08:29 -04:00
|
|
|
// The local path to a valid X509 certificate for your AWS account. This is
|
|
|
|
// used for bundling the AMI. This X509 certificate must be registered with
|
|
|
|
// your account from the security credentials page in the AWS console.
|
2019-06-06 10:29:25 -04:00
|
|
|
X509CertPath string `mapstructure:"x509_cert_path" required:"true"`
|
2019-06-18 12:08:29 -04:00
|
|
|
// The local path to the private key for the X509 certificate specified by
|
|
|
|
// x509_cert_path. This is used for bundling the AMI.
|
2019-06-06 10:29:25 -04:00
|
|
|
X509KeyPath string `mapstructure:"x509_key_path" required:"true"`
|
2019-06-18 12:08:29 -04:00
|
|
|
// The path on the remote machine where the X509 certificate will be
|
|
|
|
// uploaded. This path must already exist and be writable. X509
|
|
|
|
// certificates are uploaded after provisioning is run, so it is perfectly
|
|
|
|
// okay to create this directory as part of the provisioning process.
|
|
|
|
// Defaults to /tmp.
|
2019-06-06 10:29:25 -04:00
|
|
|
X509UploadPath string `mapstructure:"x509_upload_path" required:"false"`
|
2013-08-08 18:27:12 -04:00
|
|
|
|
2015-06-22 12:22:42 -04:00
|
|
|
ctx interpolate.Context
|
2013-07-15 02:56:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config Config
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2015-06-29 12:49:11 -04:00
|
|
|
configs := make([]interface{}, len(raws)+1)
|
|
|
|
configs[0] = map[string]interface{}{
|
|
|
|
"bundle_prefix": "image-{{timestamp}}",
|
|
|
|
}
|
|
|
|
copy(configs[1:], raws)
|
|
|
|
|
2015-06-22 12:22:42 -04:00
|
|
|
b.config.ctx.Funcs = awscommon.TemplateFuncs
|
2015-05-27 14:47:45 -04:00
|
|
|
err := config.Decode(&b.config, &config.DecodeOpts{
|
2020-10-09 20:01:55 -04:00
|
|
|
PluginType: BuilderId,
|
2015-05-27 14:47:45 -04:00
|
|
|
Interpolate: true,
|
2015-06-22 12:22:42 -04:00
|
|
|
InterpolateContext: &b.config.ctx,
|
2015-05-27 14:47:45 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
2017-01-10 05:41:28 -05:00
|
|
|
"ami_description",
|
2015-05-27 14:47:45 -04:00
|
|
|
"bundle_upload_command",
|
|
|
|
"bundle_vol_command",
|
2017-01-10 05:41:28 -05:00
|
|
|
"run_tags",
|
|
|
|
"run_volume_tags",
|
|
|
|
"snapshot_tags",
|
|
|
|
"tags",
|
2017-10-12 17:33:01 -04:00
|
|
|
"spot_tags",
|
2015-05-27 14:47:45 -04:00
|
|
|
},
|
|
|
|
},
|
2015-06-29 12:49:11 -04:00
|
|
|
}, configs...)
|
2013-08-08 18:27:12 -04:00
|
|
|
if err != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, err
|
2013-08-08 18:27:12 -04:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:24:49 -05:00
|
|
|
if b.config.PackerConfig.PackerForce {
|
|
|
|
b.config.AMIForceDeregister = true
|
|
|
|
}
|
|
|
|
|
2013-07-25 00:22:16 -04:00
|
|
|
if b.config.BundleDestination == "" {
|
|
|
|
b.config.BundleDestination = "/tmp"
|
|
|
|
}
|
|
|
|
|
2013-07-25 00:29:21 -04:00
|
|
|
if b.config.BundleUploadCommand == "" {
|
2015-06-10 16:18:05 -04:00
|
|
|
if b.config.IamInstanceProfile != "" {
|
|
|
|
b.config.BundleUploadCommand = "sudo -i -n ec2-upload-bundle " +
|
|
|
|
"-b {{.BucketName}} " +
|
|
|
|
"-m {{.ManifestPath}} " +
|
|
|
|
"-d {{.BundleDirectory}} " +
|
|
|
|
"--batch " +
|
|
|
|
"--region {{.Region}} " +
|
|
|
|
"--retry"
|
|
|
|
} else {
|
|
|
|
b.config.BundleUploadCommand = "sudo -i -n ec2-upload-bundle " +
|
|
|
|
"-b {{.BucketName}} " +
|
|
|
|
"-m {{.ManifestPath}} " +
|
|
|
|
"-a {{.AccessKey}} " +
|
|
|
|
"-s {{.SecretKey}} " +
|
|
|
|
"-d {{.BundleDirectory}} " +
|
|
|
|
"--batch " +
|
|
|
|
"--region {{.Region}} " +
|
|
|
|
"--retry"
|
|
|
|
}
|
2013-07-24 16:41:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.BundleVolCommand == "" {
|
2015-04-04 10:52:17 -04:00
|
|
|
b.config.BundleVolCommand = "sudo -i -n ec2-bundle-vol " +
|
2013-07-24 16:41:49 -04:00
|
|
|
"-k {{.KeyPath}} " +
|
|
|
|
"-u {{.AccountId}} " +
|
|
|
|
"-c {{.CertPath}} " +
|
|
|
|
"-r {{.Architecture}} " +
|
2013-09-18 16:42:15 -04:00
|
|
|
"-e {{.PrivatePath}}/* " +
|
2013-07-25 00:22:16 -04:00
|
|
|
"-d {{.Destination}} " +
|
|
|
|
"-p {{.Prefix}} " +
|
2014-09-05 12:32:35 -04:00
|
|
|
"--batch " +
|
|
|
|
"--no-filter"
|
2013-07-24 16:41:49 -04:00
|
|
|
}
|
|
|
|
|
2013-07-25 11:51:38 -04:00
|
|
|
if b.config.X509UploadPath == "" {
|
|
|
|
b.config.X509UploadPath = "/tmp"
|
|
|
|
}
|
|
|
|
|
2013-07-25 00:29:21 -04:00
|
|
|
// Accumulate any errors
|
2015-05-27 14:47:45 -04:00
|
|
|
var errs *packer.MultiError
|
2019-06-28 17:00:56 -04:00
|
|
|
var warns []string
|
2015-06-22 12:22:42 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...)
|
2019-06-18 06:37:47 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.AMIMappings.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.LaunchMappings.Prepare(&b.config.ctx)...)
|
2017-10-30 17:17:19 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
b.config.AMIConfig.Prepare(&b.config.AccessConfig, &b.config.ctx)...)
|
2015-06-22 12:22:42 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
|
2013-07-25 00:29:21 -04:00
|
|
|
|
|
|
|
if b.config.AccountId == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, errors.New("account_id is required"))
|
|
|
|
} else {
|
|
|
|
b.config.AccountId = strings.Replace(b.config.AccountId, "-", "", -1)
|
|
|
|
}
|
|
|
|
|
2013-07-25 00:22:16 -04:00
|
|
|
if b.config.S3Bucket == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, errors.New("s3_bucket is required"))
|
|
|
|
}
|
|
|
|
|
2013-07-21 00:00:12 -04:00
|
|
|
if b.config.X509CertPath == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, errors.New("x509_cert_path is required"))
|
|
|
|
} else if _, err := os.Stat(b.config.X509CertPath); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("x509_cert_path points to bad file: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.X509KeyPath == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, errors.New("x509_key_path is required"))
|
|
|
|
} else if _, err := os.Stat(b.config.X509KeyPath); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("x509_key_path points to bad file: %s", err))
|
|
|
|
}
|
|
|
|
|
2019-08-22 16:17:35 -04:00
|
|
|
if b.config.IsSpotInstance() && ((b.config.AMIENASupport.True()) || b.config.AMISriovNetSupport) {
|
2017-12-08 17:56:19 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Spot instances do not support modification, which is required "+
|
|
|
|
"when either `ena_support` or `sriov_support` are set. Please ensure "+
|
|
|
|
"you use an AMI that already has either SR-IOV or ENA enabled."))
|
|
|
|
}
|
|
|
|
|
2019-06-28 17:00:56 -04:00
|
|
|
if b.config.RunConfig.SpotPriceAutoProduct != "" {
|
|
|
|
warns = append(warns, "spot_price_auto_product is deprecated and no "+
|
|
|
|
"longer necessary for Packer builds. In future versions of "+
|
|
|
|
"Packer, inclusion of spot_price_auto_product will error your "+
|
|
|
|
"builds. Please take a look at our current documentation to "+
|
|
|
|
"understand how Packer requests Spot instances.")
|
|
|
|
}
|
|
|
|
|
2013-07-20 22:40:45 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warns, errs
|
2013-07-20 22:40:45 -04:00
|
|
|
}
|
2018-08-10 17:25:14 -04:00
|
|
|
packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token)
|
2020-01-16 06:04:03 -05:00
|
|
|
|
2020-08-12 04:43:15 -04:00
|
|
|
generatedData := awscommon.GetGeneratedDataList()
|
2020-01-16 06:04:03 -05:00
|
|
|
return generatedData, warns, nil
|
2013-07-15 02:56:28 -04:00
|
|
|
}
|
|
|
|
|
2019-03-22 09:53:28 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2017-03-01 19:43:09 -05:00
|
|
|
session, err := b.config.Session()
|
2016-11-01 18:53:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-04 19:22:52 -05:00
|
|
|
ec2conn := ec2.New(session)
|
2019-10-19 05:56:18 -04:00
|
|
|
iam := iam.New(session)
|
2013-07-20 22:40:45 -04:00
|
|
|
|
|
|
|
// Setup the state bag and initial state for the steps
|
2013-08-31 16:03:13 -04:00
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", &b.config)
|
2019-02-05 17:07:04 -05:00
|
|
|
state.Put("access_config", &b.config.AccessConfig)
|
|
|
|
state.Put("ami_config", &b.config.AMIConfig)
|
2013-08-31 16:03:13 -04:00
|
|
|
state.Put("ec2", ec2conn)
|
2019-10-19 05:56:18 -04:00
|
|
|
state.Put("iam", iam)
|
2017-12-19 14:04:17 -05:00
|
|
|
state.Put("awsSession", session)
|
2013-08-31 16:03:13 -04:00
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2020-11-04 18:44:05 -05:00
|
|
|
generatedData := &packerbuilderdata.GeneratedData{State: state}
|
2013-07-20 22:40:45 -04:00
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
var instanceStep multistep.Step
|
|
|
|
|
2017-12-08 17:56:19 -05:00
|
|
|
if b.config.IsSpotInstance() {
|
|
|
|
instanceStep = &awscommon.StepRunSpotInstance{
|
2020-08-17 11:09:19 -04:00
|
|
|
PollingConfig: b.config.PollingConfig,
|
2017-10-04 05:29:38 -04:00
|
|
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
2019-06-18 06:37:47 -04:00
|
|
|
LaunchMappings: b.config.LaunchMappings,
|
2018-09-03 02:33:58 -04:00
|
|
|
BlockDurationMinutes: b.config.BlockDurationMinutes,
|
2017-10-04 05:29:38 -04:00
|
|
|
Ctx: b.config.ctx,
|
2018-09-11 21:20:14 -04:00
|
|
|
Comm: &b.config.RunConfig.Comm,
|
2017-10-04 05:29:38 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
2017-12-08 17:56:19 -05:00
|
|
|
EbsOptimized: b.config.EbsOptimized,
|
|
|
|
InstanceType: b.config.InstanceType,
|
2020-11-03 16:15:44 -05:00
|
|
|
Region: *ec2conn.Config.Region,
|
2017-12-08 17:56:19 -05:00
|
|
|
SourceAMI: b.config.SourceAmi,
|
2017-10-04 19:18:46 -04:00
|
|
|
SpotPrice: b.config.SpotPrice,
|
2019-05-22 13:16:42 -04:00
|
|
|
SpotInstanceTypes: b.config.SpotInstanceTypes,
|
2017-12-08 17:56:19 -05:00
|
|
|
Tags: b.config.RunTags,
|
2017-10-12 17:33:01 -04:00
|
|
|
SpotTags: b.config.SpotTags,
|
2017-10-04 05:29:38 -04:00
|
|
|
UserData: b.config.UserData,
|
|
|
|
UserDataFile: b.config.UserDataFile,
|
2017-12-08 17:56:19 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
instanceStep = &awscommon.StepRunSourceInstance{
|
2020-08-17 11:09:19 -04:00
|
|
|
PollingConfig: b.config.PollingConfig,
|
2017-10-04 05:29:38 -04:00
|
|
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
2019-06-18 06:37:47 -04:00
|
|
|
LaunchMappings: b.config.LaunchMappings,
|
2018-08-30 06:50:29 -04:00
|
|
|
Comm: &b.config.RunConfig.Comm,
|
2017-10-04 05:29:38 -04:00
|
|
|
Ctx: b.config.ctx,
|
2017-12-08 17:56:19 -05:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
EbsOptimized: b.config.EbsOptimized,
|
2018-05-13 12:16:10 -04:00
|
|
|
EnableT2Unlimited: b.config.EnableT2Unlimited,
|
2017-12-08 17:56:19 -05:00
|
|
|
InstanceType: b.config.InstanceType,
|
2018-02-02 23:16:23 -05:00
|
|
|
IsRestricted: b.config.IsChinaCloud() || b.config.IsGovCloud(),
|
2017-12-08 17:56:19 -05:00
|
|
|
SourceAMI: b.config.SourceAmi,
|
|
|
|
Tags: b.config.RunTags,
|
2020-10-11 01:54:22 -04:00
|
|
|
Tenancy: b.config.Tenancy,
|
2017-12-08 17:56:19 -05:00
|
|
|
UserData: b.config.UserData,
|
|
|
|
UserDataFile: b.config.UserDataFile,
|
2017-10-04 05:29:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-20 22:40:45 -04:00
|
|
|
// Build the steps
|
|
|
|
steps := []multistep.Step{
|
2015-06-12 14:05:15 -04:00
|
|
|
&awscommon.StepPreValidate{
|
|
|
|
DestAmiName: b.config.AMIName,
|
|
|
|
ForceDeregister: b.config.AMIForceDeregister,
|
2019-11-08 16:13:45 -05:00
|
|
|
VpcId: b.config.VpcId,
|
|
|
|
SubnetId: b.config.SubnetId,
|
2020-03-13 12:17:24 -04:00
|
|
|
HasSubnetFilter: !b.config.SubnetFilter.Empty(),
|
2015-06-12 14:05:15 -04:00
|
|
|
},
|
2014-06-04 17:58:11 -04:00
|
|
|
&awscommon.StepSourceAMIInfo{
|
2017-08-28 12:18:23 -04:00
|
|
|
SourceAmi: b.config.SourceAmi,
|
|
|
|
EnableAMISriovNetSupport: b.config.AMISriovNetSupport,
|
|
|
|
EnableAMIENASupport: b.config.AMIENASupport,
|
|
|
|
AmiFilters: b.config.SourceAmiFilter,
|
2018-09-04 21:13:18 -04:00
|
|
|
AMIVirtType: b.config.AMIVirtType,
|
2014-06-04 17:58:11 -04:00
|
|
|
},
|
2018-08-14 06:04:13 -04:00
|
|
|
&awscommon.StepNetworkInfo{
|
|
|
|
VpcId: b.config.VpcId,
|
|
|
|
VpcFilter: b.config.VpcFilter,
|
|
|
|
SecurityGroupIds: b.config.SecurityGroupIds,
|
|
|
|
SecurityGroupFilter: b.config.SecurityGroupFilter,
|
|
|
|
SubnetId: b.config.SubnetId,
|
|
|
|
SubnetFilter: b.config.SubnetFilter,
|
|
|
|
AvailabilityZone: b.config.AvailabilityZone,
|
|
|
|
},
|
2013-08-30 17:48:50 -04:00
|
|
|
&awscommon.StepKeyPair{
|
2018-08-28 11:47:02 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
Comm: &b.config.RunConfig.Comm,
|
|
|
|
DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
|
2013-08-30 17:48:50 -04:00
|
|
|
},
|
2013-07-20 22:50:55 -04:00
|
|
|
&awscommon.StepSecurityGroup{
|
2019-03-30 18:47:03 -04:00
|
|
|
CommConfig: &b.config.RunConfig.Comm,
|
|
|
|
SecurityGroupFilter: b.config.SecurityGroupFilter,
|
|
|
|
SecurityGroupIds: b.config.SecurityGroupIds,
|
|
|
|
TemporarySGSourceCidrs: b.config.TemporarySGSourceCidrs,
|
2020-04-16 14:52:12 -04:00
|
|
|
SkipSSHRuleCreation: b.config.SSMAgentEnabled(),
|
2013-07-20 22:58:27 -04:00
|
|
|
},
|
2019-10-19 05:56:18 -04:00
|
|
|
&awscommon.StepIamInstanceProfile{
|
|
|
|
IamInstanceProfile: b.config.IamInstanceProfile,
|
2019-12-10 03:17:39 -05:00
|
|
|
SkipProfileValidation: b.config.SkipProfileValidation,
|
2019-10-19 05:56:18 -04:00
|
|
|
TemporaryIamInstanceProfilePolicyDocument: b.config.TemporaryIamInstanceProfilePolicyDocument,
|
|
|
|
},
|
2017-10-04 05:29:38 -04:00
|
|
|
instanceStep,
|
2015-06-14 13:50:18 -04:00
|
|
|
&awscommon.StepGetPassword{
|
2018-04-16 14:51:04 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
Comm: &b.config.RunConfig.Comm,
|
2019-10-31 10:49:34 -04:00
|
|
|
Timeout: b.config.WindowsPasswordTimeout,
|
2018-04-16 14:51:04 -04:00
|
|
|
BuildName: b.config.PackerBuildName,
|
2015-06-14 13:50:18 -04:00
|
|
|
},
|
2020-04-16 14:52:12 -04:00
|
|
|
&awscommon.StepCreateSSMTunnel{
|
2020-04-29 15:14:25 -04:00
|
|
|
AWSSession: session,
|
|
|
|
Region: *ec2conn.Config.Region,
|
2020-09-25 04:19:19 -04:00
|
|
|
PauseBeforeSSM: b.config.PauseBeforeSSM,
|
2020-05-07 14:02:10 -04:00
|
|
|
LocalPortNumber: b.config.SessionManagerPort,
|
2020-04-29 15:14:25 -04:00
|
|
|
RemotePortNumber: b.config.Comm.Port(),
|
|
|
|
SSMAgentEnabled: b.config.SSMAgentEnabled(),
|
2020-04-16 14:52:12 -04:00
|
|
|
},
|
2015-06-13 18:16:12 -04:00
|
|
|
&communicator.StepConnect{
|
2020-04-16 14:52:12 -04:00
|
|
|
// StepConnect is provided settings for WinRM and SSH, but
|
|
|
|
// the communicator will ultimately determine which port to use.
|
2015-06-13 18:16:12 -04:00
|
|
|
Config: &b.config.RunConfig.Comm,
|
2015-06-13 19:23:33 -04:00
|
|
|
Host: awscommon.SSHHost(
|
2015-06-13 18:16:12 -04:00
|
|
|
ec2conn,
|
2019-09-26 15:30:04 -04:00
|
|
|
b.config.SSHInterface,
|
2020-01-30 18:22:22 -05:00
|
|
|
b.config.Comm.Host(),
|
2019-09-26 15:30:04 -04:00
|
|
|
),
|
2020-04-16 14:52:12 -04:00
|
|
|
SSHPort: awscommon.Port(
|
|
|
|
b.config.SSHInterface,
|
|
|
|
b.config.Comm.Port(),
|
|
|
|
),
|
2018-08-22 11:02:23 -04:00
|
|
|
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
|
2013-07-20 23:03:00 -04:00
|
|
|
},
|
2020-08-12 04:43:15 -04:00
|
|
|
&awscommon.StepSetGeneratedData{
|
|
|
|
GeneratedData: generatedData,
|
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepProvision{},
|
|
|
|
&commonsteps.StepCleanupTempKeys{
|
2018-09-14 14:03:23 -04:00
|
|
|
Comm: &b.config.RunConfig.Comm,
|
|
|
|
},
|
2013-07-21 00:00:12 -04:00
|
|
|
&StepUploadX509Cert{},
|
2014-07-24 19:30:30 -04:00
|
|
|
&StepBundleVolume{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
},
|
|
|
|
&StepUploadBundle{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
},
|
2015-06-12 14:05:15 -04:00
|
|
|
&awscommon.StepDeregisterAMI{
|
2017-08-14 12:20:08 -04:00
|
|
|
AccessConfig: &b.config.AccessConfig,
|
2016-11-30 16:28:34 -05:00
|
|
|
ForceDeregister: b.config.AMIForceDeregister,
|
|
|
|
ForceDeleteSnapshot: b.config.AMIForceDeleteSnapshot,
|
|
|
|
AMIName: b.config.AMIName,
|
2017-08-14 12:20:08 -04:00
|
|
|
Regions: b.config.AMIRegions,
|
2015-06-12 14:05:15 -04:00
|
|
|
},
|
2017-08-28 12:18:23 -04:00
|
|
|
&StepRegisterAMI{
|
|
|
|
EnableAMISriovNetSupport: b.config.AMISriovNetSupport,
|
|
|
|
EnableAMIENASupport: b.config.AMIENASupport,
|
2019-10-09 19:59:51 -04:00
|
|
|
AMISkipBuildRegion: b.config.AMISkipBuildRegion,
|
2020-08-17 11:09:19 -04:00
|
|
|
PollingConfig: b.config.PollingConfig,
|
2017-08-28 12:18:23 -04:00
|
|
|
},
|
2013-09-04 19:06:06 -04:00
|
|
|
&awscommon.StepAMIRegionCopy{
|
2017-06-01 12:28:17 -04:00
|
|
|
AccessConfig: &b.config.AccessConfig,
|
|
|
|
Regions: b.config.AMIRegions,
|
2019-04-15 19:44:56 -04:00
|
|
|
AMIKmsKeyId: b.config.AMIKmsKeyId,
|
2017-06-01 12:28:17 -04:00
|
|
|
RegionKeyIds: b.config.AMIRegionKMSKeyIDs,
|
|
|
|
EncryptBootVolume: b.config.AMIEncryptBootVolume,
|
|
|
|
Name: b.config.AMIName,
|
2019-05-03 17:47:09 -04:00
|
|
|
OriginalRegion: *ec2conn.Config.Region,
|
2013-09-04 19:06:06 -04:00
|
|
|
},
|
2013-08-22 18:35:47 -04:00
|
|
|
&awscommon.StepModifyAMIAttributes{
|
2016-12-02 03:49:21 -05:00
|
|
|
Description: b.config.AMIDescription,
|
|
|
|
Users: b.config.AMIUsers,
|
|
|
|
Groups: b.config.AMIGroups,
|
|
|
|
ProductCodes: b.config.AMIProductCodes,
|
|
|
|
SnapshotUsers: b.config.SnapshotUsers,
|
|
|
|
SnapshotGroups: b.config.SnapshotGroups,
|
2017-01-10 05:41:28 -05:00
|
|
|
Ctx: b.config.ctx,
|
2020-01-20 10:29:38 -05:00
|
|
|
GeneratedData: generatedData,
|
2013-08-22 18:35:47 -04:00
|
|
|
},
|
2013-08-22 18:11:54 -04:00
|
|
|
&awscommon.StepCreateTags{
|
2016-10-16 22:19:55 -04:00
|
|
|
Tags: b.config.AMITags,
|
|
|
|
SnapshotTags: b.config.SnapshotTags,
|
2017-01-10 05:41:28 -05:00
|
|
|
Ctx: b.config.ctx,
|
2013-08-22 18:03:30 -04:00
|
|
|
},
|
2013-07-20 22:40:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run!
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2013-07-20 22:40:45 -04:00
|
|
|
|
|
|
|
// If there was an error, return that
|
2013-08-31 16:03:13 -04:00
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
2013-07-20 22:40:45 -04:00
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no AMIs, then just return
|
2013-08-31 16:03:13 -04:00
|
|
|
if _, ok := state.GetOk("amis"); !ok {
|
2013-07-20 22:40:45 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:19:04 -04:00
|
|
|
// Build the artifact and return it
|
|
|
|
artifact := &awscommon.Artifact{
|
2013-08-31 16:03:13 -04:00
|
|
|
Amis: state.Get("amis").(map[string]string),
|
2013-07-25 01:19:04 -04:00
|
|
|
BuilderIdValue: BuilderId,
|
2017-12-19 14:04:17 -05:00
|
|
|
Session: session,
|
2020-01-16 06:04:03 -05:00
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2013-07-25 01:19:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
2013-07-15 02:56:28 -04:00
|
|
|
}
|