Skip to content

Commit 625355e

Browse files
authored
Builder: ebs-surrogate aws sdk v2 upgrade (#606)
* common methods in state * no lint check + make generate * changing base of karthik/migrate/builder/ebsvolume to karthik/migrate/common_methods instead of main * refactor: migrate EC2 client to use new common clients package + migrating few common steps to aws sdk go v2 * migrate aws sdk v2 for commons steps * tests for spot instance step * tests for spot instance * make generate + test fixes for snapshot ebs volume * docs partials * adding waiter polling methods * adding security group exists waiter + wait for iam changes to propagate. * cleanup + modifying deprecated methods + replacing Message with Say method * make generate * cleanup * EBS Surrogate builder aws sdk v2 migrate * test fixes * test fixes + cleanup * test fixes + lint fix + cleanup * removing unused import * test fixes * test fixes for aws * test fixes for builder acceptance test * seperating the aws.go common file to be used in both ebs and ebs surrogate acceptance tests. * bug fixes * adding constants.
1 parent 6fbcb8a commit 625355e

29 files changed

Lines changed: 2230 additions & 198 deletions

builder/ebssurrogate/block_device.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
package ebssurrogate
77

88
import (
9-
"github.com/aws/aws-sdk-go/service/ec2"
10-
awscommon "github.com/hashicorp/packer-plugin-amazon/builder/common"
9+
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
10+
awscommon "github.com/hashicorp/packer-plugin-amazon/common"
1111
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
1212
)
1313

@@ -32,8 +32,8 @@ func (bds BlockDevices) Common() []awscommon.BlockDevice {
3232
return res
3333
}
3434

35-
func (bds BlockDevices) BuildEC2BlockDeviceMappings() []*ec2.BlockDeviceMapping {
36-
var blockDevices []*ec2.BlockDeviceMapping
35+
func (bds BlockDevices) BuildEC2BlockDeviceMappings() []ec2types.BlockDeviceMapping {
36+
var blockDevices []ec2types.BlockDeviceMapping
3737

3838
for _, blockDevice := range bds {
3939
blockDevices = append(blockDevices, blockDevice.BuildEC2BlockDeviceMapping())

builder/ebssurrogate/builder.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
"errors"
1414
"fmt"
1515

16-
"github.com/aws/aws-sdk-go/service/ec2"
17-
"github.com/aws/aws-sdk-go/service/iam"
16+
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
17+
"github.com/aws/aws-sdk-go-v2/service/iam"
1818
"github.com/hashicorp/hcl/v2/hcldec"
19-
awscommon "github.com/hashicorp/packer-plugin-amazon/builder/common"
19+
awscommon "github.com/hashicorp/packer-plugin-amazon/common"
2020
"github.com/hashicorp/packer-plugin-sdk/common"
2121
"github.com/hashicorp/packer-plugin-sdk/communicator"
2222
"github.com/hashicorp/packer-plugin-sdk/multistep"
@@ -201,8 +201,9 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
201201
errs = packersdk.MultiErrorAppend(errs, errors.New(`The only valid ami_architecture values are "arm64", "arm64_mac", "i386", "x86_64", or "x86_64_mac"`))
202202
}
203203

204-
if b.config.TpmSupport != "" && b.config.TpmSupport != ec2.TpmSupportValuesV20 {
205-
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf(`The only valid tpm_support value is %q`, ec2.TpmSupportValuesV20))
204+
if b.config.TpmSupport != "" && ec2types.TpmSupportValues(b.config.TpmSupport) != ec2types.TpmSupportValuesV20 {
205+
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf(`The only valid tpm_support value is %q`,
206+
ec2types.TpmSupportValuesV20))
206207
}
207208

208209
if b.config.BootMode != "" {
@@ -232,25 +233,28 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
232233
}
233234

234235
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
235-
session, err := b.config.Session()
236+
client, err := b.config.NewEC2Client(ctx)
236237
if err != nil {
237238
return nil, err
238239
}
239240

240-
ec2conn := ec2.New(session)
241-
iam := iam.New(session)
241+
awsConfig, err := b.config.Config(ctx)
242+
if err != nil {
243+
return nil, fmt.Errorf("error creating config: %w", err)
244+
}
245+
iamClient := iam.NewFromConfig(*awsConfig)
242246

243247
// Setup the state bag and initial state for the steps
244248
state := new(multistep.BasicStateBag)
245249
state.Put("config", &b.config)
246250
state.Put("access_config", &b.config.AccessConfig)
247251
state.Put("ami_config", &b.config.AMIConfig)
248-
state.Put("ec2", ec2conn)
249-
state.Put("iam", iam)
250-
state.Put("awsSession", session)
252+
state.Put("ec2v2", client)
253+
state.Put("iam", iamClient)
251254
state.Put("hook", hook)
252255
state.Put("ui", ui)
253-
state.Put("region", ec2conn.Config.Region)
256+
state.Put("region", awsConfig.Region)
257+
state.Put("aws_config", awsConfig)
254258
generatedData := &packerbuilderdata.GeneratedData{State: state}
255259

256260
var instanceStep multistep.Step
@@ -275,7 +279,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
275279
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
276280
InstanceType: b.config.InstanceType,
277281
FleetTags: b.config.FleetTags,
278-
Region: *ec2conn.Config.Region,
282+
Region: awsConfig.Region,
279283
SourceAMI: b.config.SourceAmi,
280284
SpotPrice: b.config.SpotPrice,
281285
SpotAllocationStrategy: b.config.SpotAllocationStrategy,
@@ -418,6 +422,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
418422
Ctx: b.config.ctx,
419423
},
420424
&awscommon.StepSecurityGroup{
425+
PollingConfig: b.config.PollingConfig,
421426
SecurityGroupFilter: b.config.SecurityGroupFilter,
422427
SecurityGroupIds: b.config.SecurityGroupIds,
423428
CommConfig: &b.config.RunConfig.Comm,
@@ -447,8 +452,8 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
447452
BuildName: b.config.PackerBuildName,
448453
},
449454
&awscommon.StepCreateSSMTunnel{
450-
AWSSession: session,
451-
Region: *ec2conn.Config.Region,
455+
AwsConfig: *awsConfig,
456+
Region: awsConfig.Region,
452457
PauseBeforeSSM: b.config.PauseBeforeSSM,
453458
LocalPortNumber: b.config.SessionManagerPort,
454459
RemotePortNumber: b.config.Comm.Port(),
@@ -458,7 +463,8 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
458463
&communicator.StepConnect{
459464
Config: &b.config.RunConfig.Comm,
460465
Host: awscommon.SSHHost(
461-
ec2conn,
466+
ctx,
467+
client,
462468
b.config.SSHInterface,
463469
b.config.Comm.Host(),
464470
),
@@ -501,7 +507,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
501507
RegionKeyIds: b.config.AMIRegionKMSKeyIDs,
502508
EncryptBootVolume: b.config.AMIEncryptBootVolume,
503509
Name: b.config.AMIName,
504-
OriginalRegion: *ec2conn.Config.Region,
510+
OriginalRegion: awsConfig.Region,
505511
AMISkipBuildRegion: b.config.AMISkipBuildRegion,
506512
AMISnapshotCopyDurationMinutes: b.config.AMISnapshotCopyDurationMinutes,
507513
},
@@ -547,7 +553,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
547553
artifact := &awscommon.Artifact{
548554
Amis: amis.(map[string]string),
549555
BuilderIdValue: BuilderId,
550-
Session: session,
556+
Config: awsConfig,
551557
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
552558
}
553559

builder/ebssurrogate/builder.hcl2spec.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/ebssurrogate/builder_acc_test.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,47 @@
44
package ebssurrogate
55

66
import (
7+
"context"
78
_ "embed"
89
"fmt"
910
"os"
1011
"os/exec"
1112
"testing"
1213
"time"
1314

14-
"github.com/aws/aws-sdk-go/aws"
15-
"github.com/aws/aws-sdk-go/service/ec2"
15+
"github.com/aws/aws-sdk-go-v2/aws"
16+
"github.com/aws/aws-sdk-go-v2/service/ec2"
17+
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
1618
"github.com/hashicorp/go-multierror"
17-
"github.com/hashicorp/packer-plugin-amazon/builder/common"
18-
amazon_acc "github.com/hashicorp/packer-plugin-amazon/builder/ebs/acceptance"
19+
"github.com/hashicorp/packer-plugin-amazon/common"
20+
amazon_acc "github.com/hashicorp/packer-plugin-amazon/common/acceptance"
21+
"github.com/hashicorp/packer-plugin-amazon/common/clients"
1922
"github.com/hashicorp/packer-plugin-sdk/acctest"
2023
)
2124

22-
func testEC2Conn(region string) (*ec2.EC2, error) {
25+
func testEC2Conn(region string) (clients.Ec2Client, error) {
26+
ctx := context.TODO()
2327
access := &common.AccessConfig{RawRegion: region}
24-
session, err := access.Session()
28+
awsConfig, err := access.Config(ctx)
2529
if err != nil {
2630
return nil, err
2731
}
28-
29-
return ec2.New(session), nil
32+
ec2Client := ec2.NewFromConfig(*awsConfig)
33+
return ec2Client, nil
3034
}
3135

3236
func checkAMITags(ami amazon_acc.AMIHelper, tagList map[string]string) error {
3337
images, err := ami.GetAmi()
3438
if err != nil || len(images) == 0 {
3539
return fmt.Errorf("failed to find ami %s at region %s", ami.Name, ami.Region)
3640
}
37-
41+
ctx := context.TODO()
3842
amiNameRegion := fmt.Sprintf("%s/%s", ami.Region, ami.Name)
3943

4044
// describe the image, get block devices with a snapshot
41-
ec2conn, _ := testEC2Conn(ami.Region)
42-
imageResp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{
43-
ImageIds: []*string{images[0].ImageId},
45+
ec2Client, _ := testEC2Conn(ami.Region)
46+
imageResp, err := ec2Client.DescribeImages(ctx, &ec2.DescribeImagesInput{
47+
ImageIds: []string{*images[0].ImageId},
4448
})
4549
if err != nil {
4650
return fmt.Errorf("failed to describe AMI %q: %s", amiNameRegion, err)
@@ -168,8 +172,8 @@ func TestAccBuilder_EbssurrogateBasic_forceIMDSv2(t *testing.T) {
168172

169173
img := images[0]
170174

171-
if img.ImdsSupport != nil && *img.ImdsSupport != "v2.0" {
172-
return fmt.Errorf("expected AMI to have IMDSv2 support, got %q", *img.ImdsSupport)
175+
if img.ImdsSupport != ec2types.ImdsSupportValuesV20 {
176+
return fmt.Errorf("expected AMI to have IMDSv2 support, got %s", img.ImdsSupport)
173177
}
174178

175179
return nil
@@ -326,6 +330,7 @@ func TestAccBuilder_EbssurrogateWithAMIDeprecate(t *testing.T) {
326330
Region: "us-east-1",
327331
Name: fmt.Sprintf("ebssurrogate-deprecate-at-acctest-%d", time.Now().Unix()),
328332
}
333+
ctx := context.TODO()
329334
testCase := &acctest.PluginTestCase{
330335
Name: "ebssurrogate - deprecate at set",
331336
Template: fmt.Sprintf(testBuilderAcc_WithDeprecateAt, ami.Name, time.Now().Add(time.Hour).UTC().Format("2006-01-02T15:04:05Z")),
@@ -343,10 +348,10 @@ func TestAccBuilder_EbssurrogateWithAMIDeprecate(t *testing.T) {
343348
return fmt.Errorf("failed to get connection to us-east-1: %s", err)
344349
}
345350

346-
out, err := conn.DescribeImages(&ec2.DescribeImagesInput{
347-
Filters: []*ec2.Filter{{
351+
out, err := conn.DescribeImages(ctx, &ec2.DescribeImagesInput{
352+
Filters: []ec2types.Filter{{
348353
Name: aws.String("name"),
349-
Values: []*string{&ami.Name},
354+
Values: []string{ami.Name},
350355
}},
351356
})
352357
if err != nil {

builder/ebssurrogate/builder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package ebssurrogate
66
import (
77
"testing"
88

9-
"github.com/hashicorp/packer-plugin-amazon/builder/common"
9+
"github.com/hashicorp/packer-plugin-amazon/common"
1010

1111
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
1212
)

0 commit comments

Comments
 (0)