builder/amazon: Add suppport for security_group_filter

Allow selecting SG's using a filter. Also some cleanup of vpc_filter and
subnet_filter.
This commit is contained in:
Rickard von Essen 2018-08-14 12:04:13 +02:00
parent 9840862757
commit 533967cb66
9 changed files with 177 additions and 184 deletions

View File

@ -47,6 +47,14 @@ func (d *VpcFilterOptions) Empty() bool {
return len(d.Filters) == 0 return len(d.Filters) == 0
} }
type SecurityGroupFilterOptions struct {
Filters map[*string]*string
}
func (d *SecurityGroupFilterOptions) Empty() bool {
return len(d.Filters) == 0
}
// RunConfig contains configuration for running an instance from a source // RunConfig contains configuration for running an instance from a source
// AMI and details on how to access that launched image. // AMI and details on how to access that launched image.
type RunConfig struct { type RunConfig struct {
@ -59,6 +67,7 @@ type RunConfig struct {
InstanceInitiatedShutdownBehavior string `mapstructure:"shutdown_behavior"` InstanceInitiatedShutdownBehavior string `mapstructure:"shutdown_behavior"`
InstanceType string `mapstructure:"instance_type"` InstanceType string `mapstructure:"instance_type"`
RunTags map[string]string `mapstructure:"run_tags"` RunTags map[string]string `mapstructure:"run_tags"`
SecurityGroupFilter SecurityGroupFilterOptions `mapstructure:"security_group_filter"`
SecurityGroupId string `mapstructure:"security_group_id"` SecurityGroupId string `mapstructure:"security_group_id"`
SecurityGroupIds []string `mapstructure:"security_group_ids"` SecurityGroupIds []string `mapstructure:"security_group_ids"`
SourceAmi string `mapstructure:"source_ami"` SourceAmi string `mapstructure:"source_ami"`

View File

@ -7,27 +7,27 @@ import (
"math/rand" "math/rand"
"sort" "sort"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
) )
// StepNetworkInfo queries AWS for information about // StepNetworkInfo queries AWS for information about
// VPC's, Subnets, and Security Groups that is used // VPC's and Subnets that is used throughout the AMI creation process.
// throughout the AMI creation process.
// //
// Produces: // Produces (adding them to the state bag):
// vpc_id string - the VPC ID // vpc_id string - the VPC ID
// subnet_id string - the Subnet ID // subnet_id string - the Subnet ID
// az string - the AZ name // availability_zone string - the AZ name
// sg_ids []string - the SG IDs
type StepNetworkInfo struct { type StepNetworkInfo struct {
VpcId string VpcId string
VpcFilter VpcFilterOptions VpcFilter VpcFilterOptions
SubnetId string SubnetId string
SubnetFilter SubnetFilterOptions SubnetFilter SubnetFilterOptions
AvailabilityZone string AvailabilityZone string
// TODO Security groups + filter SecurityGroupIds []string
SecurityGroupFilter SecurityGroupFilterOptions
} }
type subnetsSort []*ec2.Subnet type subnetsSort []*ec2.Subnet
@ -52,8 +52,8 @@ func (s *StepNetworkInfo) Run(_ context.Context, state multistep.StateBag) multi
// VPC // VPC
if s.VpcId == "" && !s.VpcFilter.Empty() { if s.VpcId == "" && !s.VpcFilter.Empty() {
params := &ec2.DescribeVpcsInput{} params := &ec2.DescribeVpcsInput{}
params.Filters = buildEc2Filters(s.VpcFilter.Filters) params.Filters = buildEc2Filters(s.VpcFilter.Filters)
log.Printf("Using VPC Filters %v", params) log.Printf("Using VPC Filters %v", params)
vpcResp, err := ec2conn.DescribeVpcs(params) vpcResp, err := ec2conn.DescribeVpcs(params)
@ -65,7 +65,7 @@ func (s *StepNetworkInfo) Run(_ context.Context, state multistep.StateBag) multi
} }
if len(vpcResp.Vpcs) != 1 { if len(vpcResp.Vpcs) != 1 {
err := fmt.Errorf("No or more than one VPC was found matching filters: %v", params) err := fmt.Errorf("Exactly one VPC should match the filter, but %d VPC's was found matching filters: %v", len(vpcResp.Vpcs), params)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
@ -79,11 +79,11 @@ func (s *StepNetworkInfo) Run(_ context.Context, state multistep.StateBag) multi
if s.SubnetId == "" && !s.SubnetFilter.Empty() { if s.SubnetId == "" && !s.SubnetFilter.Empty() {
params := &ec2.DescribeSubnetsInput{} params := &ec2.DescribeSubnetsInput{}
vpcId := "vpc-id" if s.VpcId != "" {
s.SubnetFilter.Filters[&vpcId] = &s.VpcId s.SubnetFilter.Filters[aws.String("vpc-id")] = &s.VpcId
}
if s.AvailabilityZone != "" { if s.AvailabilityZone != "" {
az := "availability-zone" s.SubnetFilter.Filters[aws.String("availability-zone")] = &s.AvailabilityZone
s.SubnetFilter.Filters[&az] = &s.AvailabilityZone
} }
params.Filters = buildEc2Filters(s.SubnetFilter.Filters) params.Filters = buildEc2Filters(s.SubnetFilter.Filters)
log.Printf("Using Subnet Filters %v", params) log.Printf("Using Subnet Filters %v", params)
@ -104,7 +104,7 @@ func (s *StepNetworkInfo) Run(_ context.Context, state multistep.StateBag) multi
} }
if len(subnetsResp.Subnets) > 1 && !s.SubnetFilter.Random && !s.SubnetFilter.MostFree { if len(subnetsResp.Subnets) > 1 && !s.SubnetFilter.Random && !s.SubnetFilter.MostFree {
err := fmt.Errorf("Your query returned more than one result. Please try a more specific search, or set random or most_free to true.") err := fmt.Errorf("Your filter matched %d Subnets. Please try a more specific search, or set random or most_free to true.", len(subnetsResp.Subnets))
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
@ -123,7 +123,28 @@ func (s *StepNetworkInfo) Run(_ context.Context, state multistep.StateBag) multi
ui.Message(fmt.Sprintf("Found Subnet ID: %s", s.SubnetId)) ui.Message(fmt.Sprintf("Found Subnet ID: %s", s.SubnetId))
} }
// Try to find AZ and VPC Id from Subnet if they are not yet found/given
if s.SubnetId != "" && (s.AvailabilityZone == "" || s.VpcId == "") {
log.Printf("[INFO] Finding AZ and VpcId for the given subnet '%s'", s.SubnetId)
resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []*string{&s.SubnetId}})
if err != nil {
err := fmt.Errorf("Describing the subnet: %s returned error: %s.", s.SubnetId, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if s.AvailabilityZone == "" {
s.AvailabilityZone = *resp.Subnets[0].AvailabilityZone
log.Printf("[INFO] AvailabilityZone found: '%s'", s.AvailabilityZone)
}
if s.VpcId == "" {
s.VpcId = *resp.Subnets[0].VpcId
log.Printf("[INFO] VpcId found: '%s'", s.VpcId)
}
}
state.Put("vpc_id", s.VpcId) state.Put("vpc_id", s.VpcId)
state.Put("availability_zone", s.AvailabilityZone)
state.Put("subnet_id", s.SubnetId) state.Put("subnet_id", s.SubnetId)
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -19,7 +19,6 @@ import (
type StepRunSourceInstance struct { type StepRunSourceInstance struct {
AssociatePublicIpAddress bool AssociatePublicIpAddress bool
AvailabilityZone string
BlockDevices BlockDevices BlockDevices BlockDevices
Ctx interpolate.Context Ctx interpolate.Context
Debug bool Debug bool
@ -31,7 +30,6 @@ type StepRunSourceInstance struct {
InstanceType string InstanceType string
IsRestricted bool IsRestricted bool
SourceAMI string SourceAMI string
SubnetId string
Tags TagMap Tags TagMap
UserData string UserData string
UserDataFile string UserDataFile string
@ -105,6 +103,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
return multistep.ActionHalt return multistep.ActionHalt
} }
az := state.Get("availability_zone").(string)
runOpts := &ec2.RunInstancesInput{ runOpts := &ec2.RunInstancesInput{
ImageId: &s.SourceAMI, ImageId: &s.SourceAMI,
InstanceType: &s.InstanceType, InstanceType: &s.InstanceType,
@ -113,7 +112,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
MinCount: aws.Int64(1), MinCount: aws.Int64(1),
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: &s.IamInstanceProfile}, IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: &s.IamInstanceProfile},
BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(), BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(),
Placement: &ec2.Placement{AvailabilityZone: &s.AvailabilityZone}, Placement: &ec2.Placement{AvailabilityZone: &az},
EbsOptimized: &s.EbsOptimized, EbsOptimized: &s.EbsOptimized,
} }
@ -154,25 +153,20 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
runOpts.KeyName = &keyName runOpts.KeyName = &keyName
} }
// TODO always get subnet_id from state. subnetId := state.Get("subnet_id").(string)
if s.SubnetId == "" {
if subnetId, ok := state.GetOk("subnet_id"); ok {
s.SubnetId = subnetId.(string)
}
}
if s.SubnetId != "" && s.AssociatePublicIpAddress { if subnetId != "" && s.AssociatePublicIpAddress {
runOpts.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{ runOpts.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{
{ {
DeviceIndex: aws.Int64(0), DeviceIndex: aws.Int64(0),
AssociatePublicIpAddress: &s.AssociatePublicIpAddress, AssociatePublicIpAddress: &s.AssociatePublicIpAddress,
SubnetId: &s.SubnetId, SubnetId: aws.String(subnetId),
Groups: securityGroupIds, Groups: securityGroupIds,
DeleteOnTermination: aws.Bool(true), DeleteOnTermination: aws.Bool(true),
}, },
} }
} else { } else {
runOpts.SubnetId = &s.SubnetId runOpts.SubnetId = aws.String(subnetId)
runOpts.SecurityGroupIds = securityGroupIds runOpts.SecurityGroupIds = securityGroupIds
} }

View File

@ -21,7 +21,6 @@ import (
type StepRunSpotInstance struct { type StepRunSpotInstance struct {
AssociatePublicIpAddress bool AssociatePublicIpAddress bool
AvailabilityZone string
BlockDevices BlockDevices BlockDevices BlockDevices
Debug bool Debug bool
EbsOptimized bool EbsOptimized bool
@ -33,7 +32,6 @@ type StepRunSpotInstance struct {
SpotPrice string SpotPrice string
SpotPriceProduct string SpotPriceProduct string
SpotTags TagMap SpotTags TagMap
SubnetId string
Tags TagMap Tags TagMap
VolumeTags TagMap VolumeTags TagMap
UserData string UserData string
@ -87,7 +85,12 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
} }
spotPrice := s.SpotPrice spotPrice := s.SpotPrice
availabilityZone := s.AvailabilityZone azConfig := ""
if azRaw, ok := state.GetOk("availability_zone"); ok {
azConfig = azRaw.(string)
}
az := azConfig
if spotPrice == "auto" { if spotPrice == "auto" {
ui.Message(fmt.Sprintf( ui.Message(fmt.Sprintf(
"Finding spot price for %s %s...", "Finding spot price for %s %s...",
@ -98,7 +101,7 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
resp, err := ec2conn.DescribeSpotPriceHistory(&ec2.DescribeSpotPriceHistoryInput{ resp, err := ec2conn.DescribeSpotPriceHistory(&ec2.DescribeSpotPriceHistoryInput{
InstanceTypes: []*string{&s.InstanceType}, InstanceTypes: []*string{&s.InstanceType},
ProductDescriptions: []*string{&s.SpotPriceProduct}, ProductDescriptions: []*string{&s.SpotPriceProduct},
AvailabilityZone: &s.AvailabilityZone, AvailabilityZone: &az,
StartTime: &startTime, StartTime: &startTime,
}) })
if err != nil { if err != nil {
@ -118,8 +121,8 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
} }
if price == 0 || current < price { if price == 0 || current < price {
price = current price = current
if s.AvailabilityZone == "" { if azConfig == "" {
availabilityZone = *history.AvailabilityZone az = *history.AvailabilityZone
} }
} }
} }
@ -163,24 +166,26 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
UserData: &userData, UserData: &userData,
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: &s.IamInstanceProfile}, IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: &s.IamInstanceProfile},
Placement: &ec2.SpotPlacement{ Placement: &ec2.SpotPlacement{
AvailabilityZone: &availabilityZone, AvailabilityZone: &az,
}, },
BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(), BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(),
EbsOptimized: &s.EbsOptimized, EbsOptimized: &s.EbsOptimized,
} }
if s.SubnetId != "" && s.AssociatePublicIpAddress { subnetId := state.Get("subnet_id").(string)
if subnetId != "" && s.AssociatePublicIpAddress {
runOpts.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{ runOpts.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{
{ {
DeviceIndex: aws.Int64(0), DeviceIndex: aws.Int64(0),
AssociatePublicIpAddress: &s.AssociatePublicIpAddress, AssociatePublicIpAddress: &s.AssociatePublicIpAddress,
SubnetId: &s.SubnetId, SubnetId: &subnetId,
Groups: securityGroupIds, Groups: securityGroupIds,
DeleteOnTermination: aws.Bool(true), DeleteOnTermination: aws.Bool(true),
}, },
} }
} else { } else {
runOpts.SubnetId = &s.SubnetId runOpts.SubnetId = &subnetId
runOpts.SecurityGroupIds = securityGroupIds runOpts.SecurityGroupIds = securityGroupIds
} }

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"strings"
"time" "time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
@ -17,8 +18,8 @@ import (
type StepSecurityGroup struct { type StepSecurityGroup struct {
CommConfig *communicator.Config CommConfig *communicator.Config
SecurityGroupFilter SecurityGroupFilterOptions
SecurityGroupIds []string SecurityGroupIds []string
VpcId string
TemporarySGSourceCidr string TemporarySGSourceCidr string
createdGroupId string createdGroupId string
@ -27,6 +28,7 @@ type StepSecurityGroup struct {
func (s *StepSecurityGroup) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepSecurityGroup) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ec2conn := state.Get("ec2").(*ec2.EC2) ec2conn := state.Get("ec2").(*ec2.EC2)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
vpcId := state.Get("vpc_id").(string)
if len(s.SecurityGroupIds) > 0 { if len(s.SecurityGroupIds) > 0 {
_, err := ec2conn.DescribeSecurityGroups( _, err := ec2conn.DescribeSecurityGroups(
@ -45,6 +47,39 @@ func (s *StepSecurityGroup) Run(_ context.Context, state multistep.StateBag) mul
return multistep.ActionContinue return multistep.ActionContinue
} }
if !s.SecurityGroupFilter.Empty() {
params := &ec2.DescribeSecurityGroupsInput{}
params.Filters = buildEc2Filters(s.SecurityGroupFilter.Filters)
vpcFilter := ec2.Filter{
Name: aws.String("vpc-id"),
Values: []*string{
aws.String(vpcId),
},
}
params.Filters = append(params.Filters, &vpcFilter)
log.Printf("Using SecurityGroup Filters %v", params)
sgResp, err := ec2conn.DescribeSecurityGroups(params)
if err != nil {
err := fmt.Errorf("Couldn't find security groups for filter: %s", err)
log.Printf("[DEBUG] %s", err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
securityGroupIds := []string{}
for _, sg := range sgResp.SecurityGroups {
securityGroupIds = append(securityGroupIds, *sg.GroupId)
}
ui.Message(fmt.Sprintf("Found Security Group(s): %s", strings.Join(securityGroupIds, ", ")))
state.Put("securityGroupIds", securityGroupIds)
return multistep.ActionContinue
}
port := s.CommConfig.Port() port := s.CommConfig.Port()
if port == 0 { if port == 0 {
if s.CommConfig.Type != "none" { if s.CommConfig.Type != "none" {
@ -60,16 +95,7 @@ func (s *StepSecurityGroup) Run(_ context.Context, state multistep.StateBag) mul
Description: aws.String("Temporary group for Packer"), Description: aws.String("Temporary group for Packer"),
} }
// TODO always get vpc_id from state. group.VpcId = &vpcId
if s.VpcId == "" {
if vpcId, ok := state.GetOk("vpc_id"); ok {
s.VpcId = vpcId.(string)
}
}
if s.VpcId != "" {
group.VpcId = &s.VpcId
}
groupResp, err := ec2conn.CreateSecurityGroup(group) groupResp, err := ec2conn.CreateSecurityGroup(group)
if err != nil { if err != nil {

View File

@ -93,28 +93,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
} }
ec2conn := ec2.New(session) ec2conn := ec2.New(session)
// TODO Translate this into VpcFilter/SubnetFilter and move the describe into apropriate step.
// If the subnet is specified but not the VpcId or AZ, try to determine them automatically
/*
* If subnet => vpc, az
*/
if b.config.SubnetId != "" && (b.config.AvailabilityZone == "" || b.config.VpcId == "") {
log.Printf("[INFO] Finding AZ and VpcId for the given subnet '%s'", b.config.SubnetId)
resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []*string{&b.config.SubnetId}})
if err != nil {
return nil, err
}
if b.config.AvailabilityZone == "" {
b.config.AvailabilityZone = *resp.Subnets[0].AvailabilityZone
log.Printf("[INFO] AvailabilityZone found: '%s'", b.config.AvailabilityZone)
}
if b.config.VpcId == "" {
b.config.VpcId = *resp.Subnets[0].VpcId
log.Printf("[INFO] VpcId found: '%s'", b.config.VpcId)
}
}
// Setup the state bag and initial state for the steps // Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag) state := new(multistep.BasicStateBag)
state.Put("config", b.config) state.Put("config", b.config)
@ -128,7 +106,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
if b.config.IsSpotInstance() { if b.config.IsSpotInstance() {
instanceStep = &awscommon.StepRunSpotInstance{ instanceStep = &awscommon.StepRunSpotInstance{
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
AvailabilityZone: b.config.AvailabilityZone,
BlockDevices: b.config.BlockDevices, BlockDevices: b.config.BlockDevices,
Ctx: b.config.ctx, Ctx: b.config.ctx,
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
@ -141,7 +118,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
SpotPrice: b.config.SpotPrice, SpotPrice: b.config.SpotPrice,
SpotPriceProduct: b.config.SpotPriceAutoProduct, SpotPriceProduct: b.config.SpotPriceAutoProduct,
SpotTags: b.config.SpotTags, SpotTags: b.config.SpotTags,
SubnetId: b.config.SubnetId,
Tags: b.config.RunTags, Tags: b.config.RunTags,
UserData: b.config.UserData, UserData: b.config.UserData,
UserDataFile: b.config.UserDataFile, UserDataFile: b.config.UserDataFile,
@ -150,7 +126,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
} else { } else {
instanceStep = &awscommon.StepRunSourceInstance{ instanceStep = &awscommon.StepRunSourceInstance{
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
AvailabilityZone: b.config.AvailabilityZone,
BlockDevices: b.config.BlockDevices, BlockDevices: b.config.BlockDevices,
Ctx: b.config.ctx, Ctx: b.config.ctx,
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
@ -162,7 +137,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
InstanceType: b.config.InstanceType, InstanceType: b.config.InstanceType,
IsRestricted: b.config.IsChinaCloud() || b.config.IsGovCloud(), IsRestricted: b.config.IsChinaCloud() || b.config.IsGovCloud(),
SourceAMI: b.config.SourceAmi, SourceAMI: b.config.SourceAmi,
SubnetId: b.config.SubnetId,
Tags: b.config.RunTags, Tags: b.config.RunTags,
UserData: b.config.UserData, UserData: b.config.UserData,
UserDataFile: b.config.UserDataFile, UserDataFile: b.config.UserDataFile,
@ -185,6 +159,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&awscommon.StepNetworkInfo{ &awscommon.StepNetworkInfo{
VpcId: b.config.VpcId, VpcId: b.config.VpcId,
VpcFilter: b.config.VpcFilter, VpcFilter: b.config.VpcFilter,
SecurityGroupIds: b.config.SecurityGroupIds,
SecurityGroupFilter: b.config.SecurityGroupFilter,
SubnetId: b.config.SubnetId, SubnetId: b.config.SubnetId,
SubnetFilter: b.config.SubnetFilter, SubnetFilter: b.config.SubnetFilter,
AvailabilityZone: b.config.AvailabilityZone, AvailabilityZone: b.config.AvailabilityZone,
@ -198,11 +174,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKey, PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKey,
}, },
&awscommon.StepSecurityGroup{ &awscommon.StepSecurityGroup{
// TODO remove SecurityGroupFilter: b.config.SecurityGroupFilter,
SecurityGroupIds: b.config.SecurityGroupIds, SecurityGroupIds: b.config.SecurityGroupIds,
CommConfig: &b.config.RunConfig.Comm, CommConfig: &b.config.RunConfig.Comm,
// TODO remove
VpcId: b.config.VpcId,
TemporarySGSourceCidr: b.config.TemporarySGSourceCidr, TemporarySGSourceCidr: b.config.TemporarySGSourceCidr,
}, },
&awscommon.StepCleanupVolumes{ &awscommon.StepCleanupVolumes{

View File

@ -107,23 +107,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
} }
ec2conn := ec2.New(session) ec2conn := ec2.New(session)
// If the subnet is specified but not the VpcId or AZ, try to determine them automatically
if b.config.SubnetId != "" && (b.config.AvailabilityZone == "" || b.config.VpcId == "") {
log.Printf("[INFO] Finding AZ and VpcId for the given subnet '%s'", b.config.SubnetId)
resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []*string{&b.config.SubnetId}})
if err != nil {
return nil, err
}
if b.config.AvailabilityZone == "" {
b.config.AvailabilityZone = *resp.Subnets[0].AvailabilityZone
log.Printf("[INFO] AvailabilityZone found: '%s'", b.config.AvailabilityZone)
}
if b.config.VpcId == "" {
b.config.VpcId = *resp.Subnets[0].VpcId
log.Printf("[INFO] VpcId found: '%s'", b.config.VpcId)
}
}
// Setup the state bag and initial state for the steps // Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag) state := new(multistep.BasicStateBag)
state.Put("config", &b.config) state.Put("config", &b.config)
@ -137,7 +120,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
if b.config.IsSpotInstance() { if b.config.IsSpotInstance() {
instanceStep = &awscommon.StepRunSpotInstance{ instanceStep = &awscommon.StepRunSpotInstance{
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
AvailabilityZone: b.config.AvailabilityZone,
BlockDevices: b.config.BlockDevices, BlockDevices: b.config.BlockDevices,
Ctx: b.config.ctx, Ctx: b.config.ctx,
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
@ -150,7 +132,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
SpotPrice: b.config.SpotPrice, SpotPrice: b.config.SpotPrice,
SpotPriceProduct: b.config.SpotPriceAutoProduct, SpotPriceProduct: b.config.SpotPriceAutoProduct,
SpotTags: b.config.SpotTags, SpotTags: b.config.SpotTags,
SubnetId: b.config.SubnetId,
Tags: b.config.RunTags, Tags: b.config.RunTags,
UserData: b.config.UserData, UserData: b.config.UserData,
UserDataFile: b.config.UserDataFile, UserDataFile: b.config.UserDataFile,
@ -159,7 +140,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
} else { } else {
instanceStep = &awscommon.StepRunSourceInstance{ instanceStep = &awscommon.StepRunSourceInstance{
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
AvailabilityZone: b.config.AvailabilityZone,
BlockDevices: b.config.BlockDevices, BlockDevices: b.config.BlockDevices,
Ctx: b.config.ctx, Ctx: b.config.ctx,
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
@ -171,7 +151,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
InstanceType: b.config.InstanceType, InstanceType: b.config.InstanceType,
IsRestricted: b.config.IsChinaCloud() || b.config.IsGovCloud(), IsRestricted: b.config.IsChinaCloud() || b.config.IsGovCloud(),
SourceAMI: b.config.SourceAmi, SourceAMI: b.config.SourceAmi,
SubnetId: b.config.SubnetId,
Tags: b.config.RunTags, Tags: b.config.RunTags,
UserData: b.config.UserData, UserData: b.config.UserData,
UserDataFile: b.config.UserDataFile, UserDataFile: b.config.UserDataFile,
@ -194,6 +173,15 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
EnableAMIENASupport: b.config.AMIENASupport, EnableAMIENASupport: b.config.AMIENASupport,
AmiFilters: b.config.SourceAmiFilter, AmiFilters: b.config.SourceAmiFilter,
}, },
&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,
},
&awscommon.StepKeyPair{ &awscommon.StepKeyPair{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
SSHAgentAuth: b.config.Comm.SSHAgentAuth, SSHAgentAuth: b.config.Comm.SSHAgentAuth,
@ -203,9 +191,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKey, PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKey,
}, },
&awscommon.StepSecurityGroup{ &awscommon.StepSecurityGroup{
SecurityGroupFilter: b.config.SecurityGroupFilter,
SecurityGroupIds: b.config.SecurityGroupIds, SecurityGroupIds: b.config.SecurityGroupIds,
CommConfig: &b.config.RunConfig.Comm, CommConfig: &b.config.RunConfig.Comm,
VpcId: b.config.VpcId,
TemporarySGSourceCidr: b.config.TemporarySGSourceCidr, TemporarySGSourceCidr: b.config.TemporarySGSourceCidr,
}, },
&awscommon.StepCleanupVolumes{ &awscommon.StepCleanupVolumes{

View File

@ -92,23 +92,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
} }
ec2conn := ec2.New(session) ec2conn := ec2.New(session)
// If the subnet is specified but not the VpcId or AZ, try to determine them automatically
if b.config.SubnetId != "" && (b.config.AvailabilityZone == "" || b.config.VpcId == "") {
log.Printf("[INFO] Finding AZ and VpcId for the given subnet '%s'", b.config.SubnetId)
resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []*string{&b.config.SubnetId}})
if err != nil {
return nil, err
}
if b.config.AvailabilityZone == "" {
b.config.AvailabilityZone = *resp.Subnets[0].AvailabilityZone
log.Printf("[INFO] AvailabilityZone found: '%s'", b.config.AvailabilityZone)
}
if b.config.VpcId == "" {
b.config.VpcId = *resp.Subnets[0].VpcId
log.Printf("[INFO] VpcId found: '%s'", b.config.VpcId)
}
}
// Setup the state bag and initial state for the steps // Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag) state := new(multistep.BasicStateBag)
state.Put("config", b.config) state.Put("config", b.config)
@ -121,7 +104,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
if b.config.IsSpotInstance() { if b.config.IsSpotInstance() {
instanceStep = &awscommon.StepRunSpotInstance{ instanceStep = &awscommon.StepRunSpotInstance{
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
AvailabilityZone: b.config.AvailabilityZone,
BlockDevices: b.config.launchBlockDevices, BlockDevices: b.config.launchBlockDevices,
Ctx: b.config.ctx, Ctx: b.config.ctx,
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
@ -134,7 +116,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
SpotPrice: b.config.SpotPrice, SpotPrice: b.config.SpotPrice,
SpotPriceProduct: b.config.SpotPriceAutoProduct, SpotPriceProduct: b.config.SpotPriceAutoProduct,
SpotTags: b.config.SpotTags, SpotTags: b.config.SpotTags,
SubnetId: b.config.SubnetId,
Tags: b.config.RunTags, Tags: b.config.RunTags,
UserData: b.config.UserData, UserData: b.config.UserData,
UserDataFile: b.config.UserDataFile, UserDataFile: b.config.UserDataFile,
@ -142,7 +123,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
} else { } else {
instanceStep = &awscommon.StepRunSourceInstance{ instanceStep = &awscommon.StepRunSourceInstance{
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
AvailabilityZone: b.config.AvailabilityZone,
BlockDevices: b.config.launchBlockDevices, BlockDevices: b.config.launchBlockDevices,
Ctx: b.config.ctx, Ctx: b.config.ctx,
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
@ -154,7 +134,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
InstanceType: b.config.InstanceType, InstanceType: b.config.InstanceType,
IsRestricted: b.config.IsChinaCloud() || b.config.IsGovCloud(), IsRestricted: b.config.IsChinaCloud() || b.config.IsGovCloud(),
SourceAMI: b.config.SourceAmi, SourceAMI: b.config.SourceAmi,
SubnetId: b.config.SubnetId,
Tags: b.config.RunTags, Tags: b.config.RunTags,
UserData: b.config.UserData, UserData: b.config.UserData,
UserDataFile: b.config.UserDataFile, UserDataFile: b.config.UserDataFile,
@ -169,6 +148,15 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
EnableAMIENASupport: b.config.AMIENASupport, EnableAMIENASupport: b.config.AMIENASupport,
AmiFilters: b.config.SourceAmiFilter, AmiFilters: b.config.SourceAmiFilter,
}, },
&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,
},
&awscommon.StepKeyPair{ &awscommon.StepKeyPair{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
SSHAgentAuth: b.config.Comm.SSHAgentAuth, SSHAgentAuth: b.config.Comm.SSHAgentAuth,
@ -178,9 +166,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKey, PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKey,
}, },
&awscommon.StepSecurityGroup{ &awscommon.StepSecurityGroup{
SecurityGroupFilter: b.config.SecurityGroupFilter,
SecurityGroupIds: b.config.SecurityGroupIds, SecurityGroupIds: b.config.SecurityGroupIds,
CommConfig: &b.config.RunConfig.Comm, CommConfig: &b.config.RunConfig.Comm,
VpcId: b.config.VpcId,
TemporarySGSourceCidr: b.config.TemporarySGSourceCidr, TemporarySGSourceCidr: b.config.TemporarySGSourceCidr,
}, },
instanceStep, instanceStep,

View File

@ -178,23 +178,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
} }
ec2conn := ec2.New(session) ec2conn := ec2.New(session)
// If the subnet is specified but not the VpcId or AZ, try to determine them automatically
if b.config.SubnetId != "" && (b.config.AvailabilityZone == "" || b.config.VpcId == "") {
log.Printf("[INFO] Finding AZ and VpcId for the given subnet '%s'", b.config.SubnetId)
resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []*string{&b.config.SubnetId}})
if err != nil {
return nil, err
}
if b.config.AvailabilityZone == "" {
b.config.AvailabilityZone = *resp.Subnets[0].AvailabilityZone
log.Printf("[INFO] AvailabilityZone found: '%s'", b.config.AvailabilityZone)
}
if b.config.VpcId == "" {
b.config.VpcId = *resp.Subnets[0].VpcId
log.Printf("[INFO] VpcId found: '%s'", b.config.VpcId)
}
}
// Setup the state bag and initial state for the steps // Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag) state := new(multistep.BasicStateBag)
state.Put("config", &b.config) state.Put("config", &b.config)
@ -208,7 +191,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
if b.config.IsSpotInstance() { if b.config.IsSpotInstance() {
instanceStep = &awscommon.StepRunSpotInstance{ instanceStep = &awscommon.StepRunSpotInstance{
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
AvailabilityZone: b.config.AvailabilityZone,
BlockDevices: b.config.BlockDevices, BlockDevices: b.config.BlockDevices,
Ctx: b.config.ctx, Ctx: b.config.ctx,
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
@ -218,7 +200,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
SourceAMI: b.config.SourceAmi, SourceAMI: b.config.SourceAmi,
SpotPrice: b.config.SpotPrice, SpotPrice: b.config.SpotPrice,
SpotPriceProduct: b.config.SpotPriceAutoProduct, SpotPriceProduct: b.config.SpotPriceAutoProduct,
SubnetId: b.config.SubnetId,
Tags: b.config.RunTags, Tags: b.config.RunTags,
SpotTags: b.config.SpotTags, SpotTags: b.config.SpotTags,
UserData: b.config.UserData, UserData: b.config.UserData,
@ -227,7 +208,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
} else { } else {
instanceStep = &awscommon.StepRunSourceInstance{ instanceStep = &awscommon.StepRunSourceInstance{
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
AvailabilityZone: b.config.AvailabilityZone,
BlockDevices: b.config.BlockDevices, BlockDevices: b.config.BlockDevices,
Ctx: b.config.ctx, Ctx: b.config.ctx,
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
@ -237,7 +217,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
InstanceType: b.config.InstanceType, InstanceType: b.config.InstanceType,
IsRestricted: b.config.IsChinaCloud() || b.config.IsGovCloud(), IsRestricted: b.config.IsChinaCloud() || b.config.IsGovCloud(),
SourceAMI: b.config.SourceAmi, SourceAMI: b.config.SourceAmi,
SubnetId: b.config.SubnetId,
Tags: b.config.RunTags, Tags: b.config.RunTags,
UserData: b.config.UserData, UserData: b.config.UserData,
UserDataFile: b.config.UserDataFile, UserDataFile: b.config.UserDataFile,
@ -256,6 +235,15 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
EnableAMIENASupport: b.config.AMIENASupport, EnableAMIENASupport: b.config.AMIENASupport,
AmiFilters: b.config.SourceAmiFilter, AmiFilters: b.config.SourceAmiFilter,
}, },
&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,
},
&awscommon.StepKeyPair{ &awscommon.StepKeyPair{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
SSHAgentAuth: b.config.Comm.SSHAgentAuth, SSHAgentAuth: b.config.Comm.SSHAgentAuth,
@ -266,8 +254,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}, },
&awscommon.StepSecurityGroup{ &awscommon.StepSecurityGroup{
CommConfig: &b.config.RunConfig.Comm, CommConfig: &b.config.RunConfig.Comm,
SecurityGroupFilter: b.config.SecurityGroupFilter,
SecurityGroupIds: b.config.SecurityGroupIds, SecurityGroupIds: b.config.SecurityGroupIds,
VpcId: b.config.VpcId,
TemporarySGSourceCidr: b.config.TemporarySGSourceCidr, TemporarySGSourceCidr: b.config.TemporarySGSourceCidr,
}, },
instanceStep, instanceStep,