diff --git a/builder/amazon/common/step_pre_validate.go b/builder/amazon/common/step_pre_validate.go index 7a61335aa..3125cc698 100644 --- a/builder/amazon/common/step_pre_validate.go +++ b/builder/amazon/common/step_pre_validate.go @@ -23,6 +23,7 @@ type StepPreValidate struct { AMISkipBuildRegion bool VpcId string SubnetId string + HasSubnetFilter bool } func (s *StepPreValidate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -121,10 +122,11 @@ func (s *StepPreValidate) Run(ctx context.Context, state multistep.StateBag) mul } func (s *StepPreValidate) checkVpc(conn ec2iface.EC2API) error { - if s.VpcId == "" || (s.VpcId != "" && s.SubnetId != "") { + if s.VpcId == "" || (s.VpcId != "" && (s.SubnetId != "" || s.HasSubnetFilter)) { // Skip validation if: // * The user has not provided a VpcId. // * Both VpcId and SubnetId are provided; AWS API will error if something is wrong. + // * Both VpcId and SubnetFilter are provided return nil } @@ -135,7 +137,7 @@ func (s *StepPreValidate) checkVpc(conn ec2iface.EC2API) error { if res != nil && len(res.Vpcs) == 1 && res.Vpcs[0] != nil { if isDefault := aws.BoolValue(res.Vpcs[0].IsDefault); !isDefault { - return fmt.Errorf("Error: subnet_id must be provided for non-default VPCs (%s)", s.VpcId) + return fmt.Errorf("Error: subnet_id or subnet_filter must be provided for non-default VPCs (%s)", s.VpcId) } } return nil diff --git a/builder/amazon/common/step_pre_validate_test.go b/builder/amazon/common/step_pre_validate_test.go index cb66b76e7..7863aa870 100644 --- a/builder/amazon/common/step_pre_validate_test.go +++ b/builder/amazon/common/step_pre_validate_test.go @@ -45,6 +45,7 @@ func TestStepPreValidate_checkVpc(t *testing.T) { {"NonDefaultVpcWithSubnet", StepPreValidate{VpcId: "vpc-1234567890", SubnetId: "subnet-1234567890"}, false}, {"SubnetWithNoVpc", StepPreValidate{SubnetId: "subnet-1234567890"}, false}, {"NoVpcInformation", StepPreValidate{}, false}, + {"NonDefaultVpcWithSubnetFilter", StepPreValidate{VpcId: "vpc-1234567890", HasSubnetFilter: true}, false}, } mockConn, err := getMockConn(nil, "") diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 895e0d953..99414952e 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -210,6 +210,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack AMISkipBuildRegion: b.config.AMISkipBuildRegion, VpcId: b.config.VpcId, SubnetId: b.config.SubnetId, + HasSubnetFilter: len(b.config.SubnetFilter.Filters) > 0, }, &awscommon.StepSourceAMIInfo{ SourceAmi: b.config.SourceAmi, diff --git a/builder/amazon/ebssurrogate/builder.go b/builder/amazon/ebssurrogate/builder.go index 2de9fd943..f8a51cfee 100644 --- a/builder/amazon/ebssurrogate/builder.go +++ b/builder/amazon/ebssurrogate/builder.go @@ -233,6 +233,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack ForceDeregister: b.config.AMIForceDeregister, VpcId: b.config.VpcId, SubnetId: b.config.SubnetId, + HasSubnetFilter: len(b.config.SubnetFilter.Filters) > 0, }, &awscommon.StepSourceAMIInfo{ SourceAmi: b.config.SourceAmi, diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index ae098926c..98fe6ff03 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -296,6 +296,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack ForceDeregister: b.config.AMIForceDeregister, VpcId: b.config.VpcId, SubnetId: b.config.SubnetId, + HasSubnetFilter: len(b.config.SubnetFilter.Filters) > 0, }, &awscommon.StepSourceAMIInfo{ SourceAmi: b.config.SourceAmi,