Skip to content

Commit 207d5b5

Browse files
Skip EBS AMI preparation for no-AMI builds
1 parent 8b0c09b commit 207d5b5

9 files changed

Lines changed: 170 additions & 12 deletions

builder/common/step_modify_ebs_instance.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@ type StepModifyEBSBackedInstance struct {
1919
Skip bool
2020
EnableAMIENASupport config.Trilean
2121
EnableAMISriovNetSupport bool
22+
AMISkipCreateImage bool
2223
}
2324

2425
func (s *StepModifyEBSBackedInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
25-
ec2conn := state.Get("ec2").(ec2iface.EC2API)
26-
instance := state.Get("instance").(*ec2.Instance)
27-
ui := state.Get("ui").(packersdk.Ui)
28-
2926
// Skip when it is a spot instance
3027
if s.Skip {
3128
return multistep.ActionContinue
3229
}
3330

31+
ui := state.Get("ui").(packersdk.Ui)
32+
if s.AMISkipCreateImage {
33+
ui.Say("skip_create_ami was set; skipping source instance attribute modification")
34+
return multistep.ActionContinue
35+
}
36+
37+
ec2conn := state.Get("ec2").(ec2iface.EC2API)
38+
instance := state.Get("instance").(*ec2.Instance)
39+
3440
// Set SriovNetSupport to "simple". See http://goo.gl/icuXh5
3541
// As of February 2017, this applies to C3, C4, D2, I2, R3, and M4 (excluding m4.16xlarge)
3642
if s.EnableAMISriovNetSupport {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright IBM Corp. 2013, 2026
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package common
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/hashicorp/packer-plugin-sdk/multistep"
11+
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
12+
"github.com/hashicorp/packer-plugin-sdk/template/config"
13+
)
14+
15+
func TestStepModifyEBSBackedInstance_SkipCreateAMISkipsModification(t *testing.T) {
16+
state := new(multistep.BasicStateBag)
17+
state.Put("ui", packersdk.TestUi(t))
18+
19+
step := &StepModifyEBSBackedInstance{
20+
AMISkipCreateImage: true,
21+
EnableAMISriovNetSupport: true,
22+
EnableAMIENASupport: config.TriTrue,
23+
}
24+
25+
action := step.Run(context.Background(), state)
26+
27+
if action != multistep.ActionContinue {
28+
t.Fatalf("expected ActionContinue, got %v", action)
29+
}
30+
31+
if rawErr, ok := state.GetOk("error"); ok {
32+
t.Fatalf("expected no error, got %v", rawErr)
33+
}
34+
}

builder/common/step_stop_ebs_instance.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,25 @@ type StepStopEBSBackedInstance struct {
1919
PollingConfig *AWSPollingConfig
2020
Skip bool
2121
DisableStopInstance bool
22+
AMISkipCreateImage bool
2223
}
2324

2425
func (s *StepStopEBSBackedInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
25-
ec2conn := state.Get("ec2").(*ec2.EC2)
26-
instance := state.Get("instance").(*ec2.Instance)
2726
ui := state.Get("ui").(packersdk.Ui)
2827

2928
// Skip when it is a spot instance
3029
if s.Skip {
3130
return multistep.ActionContinue
3231
}
3332

33+
if s.AMISkipCreateImage {
34+
ui.Say("skip_create_ami was set; skipping source instance stop")
35+
return multistep.ActionContinue
36+
}
37+
38+
ec2conn := state.Get("ec2").(*ec2.EC2)
39+
instance := state.Get("instance").(*ec2.Instance)
40+
3441
var err error
3542

3643
if !s.DisableStopInstance {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright IBM Corp. 2013, 2026
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package common
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/hashicorp/packer-plugin-sdk/multistep"
11+
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
12+
)
13+
14+
func TestStepStopEBSBackedInstance_SkipCreateAMISkipsStop(t *testing.T) {
15+
state := new(multistep.BasicStateBag)
16+
state.Put("ui", packersdk.TestUi(t))
17+
18+
step := &StepStopEBSBackedInstance{
19+
AMISkipCreateImage: true,
20+
}
21+
22+
action := step.Run(context.Background(), state)
23+
24+
if action != multistep.ActionContinue {
25+
t.Fatalf("expected ActionContinue, got %v", action)
26+
}
27+
28+
if rawErr, ok := state.GetOk("error"); ok {
29+
t.Fatalf("expected no error, got %v", rawErr)
30+
}
31+
}

builder/ebs/builder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,12 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
400400
PollingConfig: b.config.PollingConfig,
401401
Skip: b.config.IsSpotInstance(),
402402
DisableStopInstance: b.config.DisableStopInstance,
403+
AMISkipCreateImage: b.config.AMISkipCreateImage,
403404
},
404405
&awscommon.StepModifyEBSBackedInstance{
405406
EnableAMISriovNetSupport: b.config.AMISriovNetSupport,
406407
EnableAMIENASupport: b.config.AMIENASupport,
408+
AMISkipCreateImage: b.config.AMISkipCreateImage,
407409
},
408410
&awscommon.StepDeregisterAMI{
409411
AccessConfig: &b.config.AccessConfig,

common/step_modify_ebs_instance.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,24 @@ type StepModifyEBSBackedInstance struct {
2020
Skip bool
2121
EnableAMIENASupport config.Trilean
2222
EnableAMISriovNetSupport bool
23+
AMISkipCreateImage bool
2324
}
2425

2526
func (s *StepModifyEBSBackedInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
26-
ec2Client := state.Get("ec2v2").(clients.Ec2Client)
27-
instance := state.Get("instance").(ec2types.Instance)
28-
ui := state.Get("ui").(packersdk.Ui)
29-
3027
// Skip when it is a spot instance
3128
if s.Skip {
3229
return multistep.ActionContinue
3330
}
3431

32+
ui := state.Get("ui").(packersdk.Ui)
33+
if s.AMISkipCreateImage {
34+
ui.Say("skip_create_ami was set; skipping source instance attribute modification")
35+
return multistep.ActionContinue
36+
}
37+
38+
ec2Client := state.Get("ec2v2").(clients.Ec2Client)
39+
instance := state.Get("instance").(ec2types.Instance)
40+
3541
// Set SriovNetSupport to "simple". See http://goo.gl/icuXh5
3642
// As of February 2017, this applies to C3, C4, D2, I2, R3, and M4 (excluding m4.16xlarge)
3743
if s.EnableAMISriovNetSupport {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright IBM Corp. 2013, 2026
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package common
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/hashicorp/packer-plugin-sdk/multistep"
11+
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
12+
"github.com/hashicorp/packer-plugin-sdk/template/config"
13+
)
14+
15+
func TestStepModifyEBSBackedInstance_SkipCreateAMISkipsModification(t *testing.T) {
16+
state := new(multistep.BasicStateBag)
17+
state.Put("ui", packersdk.TestUi(t))
18+
19+
step := &StepModifyEBSBackedInstance{
20+
AMISkipCreateImage: true,
21+
EnableAMISriovNetSupport: true,
22+
EnableAMIENASupport: config.TriTrue,
23+
}
24+
25+
action := step.Run(context.Background(), state)
26+
27+
if action != multistep.ActionContinue {
28+
t.Fatalf("expected ActionContinue, got %v", action)
29+
}
30+
31+
if rawErr, ok := state.GetOk("error"); ok {
32+
t.Fatalf("expected no error, got %v", rawErr)
33+
}
34+
}

common/step_stop_ebs_instance.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,25 @@ type StepStopEBSBackedInstance struct {
2222
PollingConfig *AWSPollingConfig
2323
Skip bool
2424
DisableStopInstance bool
25+
AMISkipCreateImage bool
2526
}
2627

2728
func (s *StepStopEBSBackedInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
28-
ec2Client := state.Get("ec2v2").(clients.Ec2Client)
29-
instance := state.Get("instance").(ec2types.Instance)
3029
ui := state.Get("ui").(packersdk.Ui)
3130

3231
// Skip when it is a spot instance
3332
if s.Skip {
3433
return multistep.ActionContinue
3534
}
3635

36+
if s.AMISkipCreateImage {
37+
ui.Say("skip_create_ami was set; skipping source instance stop")
38+
return multistep.ActionContinue
39+
}
40+
41+
ec2Client := state.Get("ec2v2").(clients.Ec2Client)
42+
instance := state.Get("instance").(ec2types.Instance)
43+
3744
var err error
3845

3946
if !s.DisableStopInstance {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright IBM Corp. 2013, 2026
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package common
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/hashicorp/packer-plugin-sdk/multistep"
11+
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
12+
)
13+
14+
func TestStepStopEBSBackedInstance_SkipCreateAMISkipsStop(t *testing.T) {
15+
state := new(multistep.BasicStateBag)
16+
state.Put("ui", packersdk.TestUi(t))
17+
18+
step := &StepStopEBSBackedInstance{
19+
AMISkipCreateImage: true,
20+
}
21+
22+
action := step.Run(context.Background(), state)
23+
24+
if action != multistep.ActionContinue {
25+
t.Fatalf("expected ActionContinue, got %v", action)
26+
}
27+
28+
if rawErr, ok := state.GetOk("error"); ok {
29+
t.Fatalf("expected no error, got %v", rawErr)
30+
}
31+
}

0 commit comments

Comments
 (0)