diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index b9277a5ab..50274ef87 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -72,6 +72,17 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe session := session.New(config) ec2conn := ec2.New(session) + // If the subnet is specified but not the AZ, try to determine the AZ automatically + if b.config.SubnetId != "" && b.config.AvailabilityZone == "" { + log.Printf("[INFO] Finding AZ 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 + } + b.config.AvailabilityZone = *resp.Subnets[0].AvailabilityZone + log.Printf("[INFO] AZ found: '%s'", b.config.AvailabilityZone) + } + // Setup the state bag and initial state for the steps state := new(multistep.BasicStateBag) state.Put("config", b.config) diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 6b1726efb..96b8fa9cc 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -163,6 +163,17 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe session := session.New(config) ec2conn := ec2.New(session) + // If the subnet is specified but not the AZ, try to determine the AZ automatically + if b.config.SubnetId != "" && b.config.AvailabilityZone == "" { + log.Printf("[INFO] Finding AZ 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 + } + b.config.AvailabilityZone = *resp.Subnets[0].AvailabilityZone + log.Printf("[INFO] AZ found: '%s'", b.config.AvailabilityZone) + } + // Setup the state bag and initial state for the steps state := new(multistep.BasicStateBag) state.Put("config", &b.config)