diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 208266518..75faed662 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -11,20 +11,21 @@ import ( // RunConfig contains configuration for running an instance from a source // AMI and details on how to access that launched image. type RunConfig struct { - SourceAmi string `mapstructure:"source_ami"` - IamInstanceProfile string `mapstructure:"iam_instance_profile"` - InstanceType string `mapstructure:"instance_type"` - UserData string `mapstructure:"user_data"` - UserDataFile string `mapstructure:"user_data_file"` - RawSSHTimeout string `mapstructure:"ssh_timeout"` - SSHUsername string `mapstructure:"ssh_username"` - SSHPort int `mapstructure:"ssh_port"` - SecurityGroupId string `mapstructure:"security_group_id"` - SecurityGroupIds []string `mapstructure:"security_group_ids"` - SubnetId string `mapstructure:"subnet_id"` - TemporaryKeyPairName string `mapstructure:"temporary_key_pair_name"` - VpcId string `mapstructure:"vpc_id"` - AvailabilityZone string `mapstructure:"availability_zone"` + SourceAmi string `mapstructure:"source_ami"` + IamInstanceProfile string `mapstructure:"iam_instance_profile"` + InstanceType string `mapstructure:"instance_type"` + UserData string `mapstructure:"user_data"` + UserDataFile string `mapstructure:"user_data_file"` + RawSSHTimeout string `mapstructure:"ssh_timeout"` + SSHUsername string `mapstructure:"ssh_username"` + SSHPort int `mapstructure:"ssh_port"` + SecurityGroupId string `mapstructure:"security_group_id"` + SecurityGroupIds []string `mapstructure:"security_group_ids"` + SubnetId string `mapstructure:"subnet_id"` + AssociatePublicIpAddress bool `mapstructure:"associate_public_ip_address"` + TemporaryKeyPairName string `mapstructure:"temporary_key_pair_name"` + VpcId string `mapstructure:"vpc_id"` + AvailabilityZone string `mapstructure:"availability_zone"` // Unexported fields that are calculated from others sshTimeout time.Duration diff --git a/builder/amazon/common/ssh.go b/builder/amazon/common/ssh.go index 5e7d8872d..6756c62e7 100644 --- a/builder/amazon/common/ssh.go +++ b/builder/amazon/common/ssh.go @@ -20,7 +20,11 @@ func SSHAddress(e *ec2.EC2, port int) func(multistep.StateBag) (string, error) { if i.DNSName != "" { host = i.DNSName } else if i.VpcId != "" { - host = i.PrivateIpAddress + if i.PublicIpAddress != "" { + host = i.PublicIpAddress + } else { + host = i.PrivateIpAddress + } } if host != "" { diff --git a/builder/amazon/common/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go index 9d92c226c..77d330a21 100644 --- a/builder/amazon/common/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -10,16 +10,17 @@ import ( ) type StepRunSourceInstance struct { - Debug bool - ExpectedRootDevice string - InstanceType string - UserData string - UserDataFile string - SourceAMI string - IamInstanceProfile string - SubnetId string - AvailabilityZone string - BlockDevices BlockDevices + Debug bool + ExpectedRootDevice string + InstanceType string + UserData string + UserDataFile string + SourceAMI string + IamInstanceProfile string + SubnetId string + AssociatePublicIpAddress bool + AvailabilityZone string + BlockDevices BlockDevices instance *ec2.Instance } @@ -47,17 +48,18 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi } runOpts := &ec2.RunInstances{ - KeyName: keyName, - ImageId: s.SourceAMI, - InstanceType: s.InstanceType, - UserData: []byte(userData), - MinCount: 0, - MaxCount: 0, - SecurityGroups: securityGroups, - IamInstanceProfile: s.IamInstanceProfile, - SubnetId: s.SubnetId, - BlockDevices: s.BlockDevices.BuildLaunchDevices(), - AvailZone: s.AvailabilityZone, + KeyName: keyName, + ImageId: s.SourceAMI, + InstanceType: s.InstanceType, + UserData: []byte(userData), + MinCount: 0, + MaxCount: 0, + SecurityGroups: securityGroups, + IamInstanceProfile: s.IamInstanceProfile, + SubnetId: s.SubnetId, + AssociatePublicIpAddress: s.AssociatePublicIpAddress, + BlockDevices: s.BlockDevices.BuildLaunchDevices(), + AvailZone: s.AvailabilityZone, } ui.Say("Launching a source AWS instance...") @@ -114,6 +116,10 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi ui.Message(fmt.Sprintf("Public DNS: %s", s.instance.DNSName)) } + if s.instance.PublicIpAddress != "" { + ui.Message(fmt.Sprintf("Public IP: %s", s.instance.PublicIpAddress)) + } + if s.instance.PrivateIpAddress != "" { ui.Message(fmt.Sprintf("Private IP: %s", s.instance.PrivateIpAddress)) } diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 5d68f9470..e58392f84 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -93,16 +93,17 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe VpcId: b.config.VpcId, }, &awscommon.StepRunSourceInstance{ - Debug: b.config.PackerDebug, - ExpectedRootDevice: "ebs", - InstanceType: b.config.InstanceType, - UserData: b.config.UserData, - UserDataFile: b.config.UserDataFile, - SourceAMI: b.config.SourceAmi, - IamInstanceProfile: b.config.IamInstanceProfile, - SubnetId: b.config.SubnetId, - AvailabilityZone: b.config.AvailabilityZone, - BlockDevices: b.config.BlockDevices, + Debug: b.config.PackerDebug, + ExpectedRootDevice: "ebs", + InstanceType: b.config.InstanceType, + UserData: b.config.UserData, + UserDataFile: b.config.UserDataFile, + SourceAMI: b.config.SourceAmi, + IamInstanceProfile: b.config.IamInstanceProfile, + SubnetId: b.config.SubnetId, + AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, + AvailabilityZone: b.config.AvailabilityZone, + BlockDevices: b.config.BlockDevices, }, &common.StepConnectSSH{ SSHAddress: awscommon.SSHAddress(ec2conn, b.config.SSHPort), diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 455a7cd01..bb655e629 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -196,16 +196,17 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe VpcId: b.config.VpcId, }, &awscommon.StepRunSourceInstance{ - Debug: b.config.PackerDebug, - ExpectedRootDevice: "instance-store", - InstanceType: b.config.InstanceType, - IamInstanceProfile: b.config.IamInstanceProfile, - UserData: b.config.UserData, - UserDataFile: b.config.UserDataFile, - SourceAMI: b.config.SourceAmi, - SubnetId: b.config.SubnetId, - AvailabilityZone: b.config.AvailabilityZone, - BlockDevices: b.config.BlockDevices, + Debug: b.config.PackerDebug, + ExpectedRootDevice: "instance-store", + InstanceType: b.config.InstanceType, + IamInstanceProfile: b.config.IamInstanceProfile, + UserData: b.config.UserData, + UserDataFile: b.config.UserDataFile, + SourceAMI: b.config.SourceAmi, + SubnetId: b.config.SubnetId, + AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, + AvailabilityZone: b.config.AvailabilityZone, + BlockDevices: b.config.BlockDevices, }, &common.StepConnectSSH{ SSHAddress: awscommon.SSHAddress(ec2conn, b.config.SSHPort), diff --git a/website/source/docs/builders/amazon-ebs.html.markdown b/website/source/docs/builders/amazon-ebs.html.markdown index a050314a0..96177bfb9 100644 --- a/website/source/docs/builders/amazon-ebs.html.markdown +++ b/website/source/docs/builders/amazon-ebs.html.markdown @@ -111,6 +111,10 @@ Optional: * `subnet_id` (string) - If using VPC, the ID of the subnet, such as "subnet-12345def", where Packer will launch the EC2 instance. +* `associate_public_ip_address` (bool) - If using a non-default VPC, public + IP addresses are not provided by default. If this is toggled, your new + instance will get a Public IP. + * `tags` (object of key/value strings) - Tags applied to the AMI. * `user_data` (string) - User data to apply when launching the instance. diff --git a/website/source/docs/builders/amazon-instance.html.markdown b/website/source/docs/builders/amazon-instance.html.markdown index b2caf1371..3135bf322 100644 --- a/website/source/docs/builders/amazon-instance.html.markdown +++ b/website/source/docs/builders/amazon-instance.html.markdown @@ -150,6 +150,10 @@ Optional: * `subnet_id` (string) - If using VPC, the ID of the subnet, such as "subnet-12345def", where Packer will launch the EC2 instance. +* `associate_public_ip_address` (bool) - If using a non-default VPC, public + IP addresses are not provided by default. If this is toggled, your new + instance will get a Public IP. + * `tags` (object of key/value strings) - Tags applied to the AMI. * `user_data` (string) - User data to apply when launching the instance.