Skip to content

refactor(aws): collapse per-region client boilerplate into helpers (#297) - #359

Merged
scttfrdmn merged 1 commit into
mainfrom
refactor/297-regional-ec2
Jul 15, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
refactor/297-regional-ec2

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Collapses the ~40 repeated per-region client-construction sites in pkg/aws behind two helpers, and retires the always-nil-error getRegionalConfig.

Helpers (new pkg/aws/regional.go)

func (c *Client) regionalConfig(region string) aws.Config   // c.cfg.Copy(); if region != "" { cfg.Region = region }
func (c *Client) regionalEC2(region string) *ec2.Client      // ec2.NewFromConfig(c.regionalConfig(region))

What changed

  • ~40 inline cfg := c.cfg.Copy(); cfg.Region = region; ec2.NewFromConfig(cfg) sites across 16 files → c.regionalEC2(region) (EC2 sites) or c.regionalConfig(region) (fsx/s3/ssm/imagebuilder sites, and sites that reuse the config).
  • Deleted getRegionalConfig(ctx, region) (aws.Config, error) — its error was always nil, so its Pattern-D callers (ami_base/ami_checks/ami_mgmt/snapshot_build) also shed dead if err != nil branches.
  • Net −166 lines.

Safety

  • regionalConfig treats empty region as a no-op (default-chain region preserved), matching the prior if region != "" conditional sites (cleanup GetInstanceElasticIP, imagebuilder WaitForImage) — byte-identical behavior.
  • No file lost its last reference to a service package; no unused-import fallout.
  • go build ./... clean, gofmt/go vet clean, go test ./pkg/aws green (incl. TestBuildInlinePolicy).
  • Final grep confirms zero remaining call sites (only the helper body in regional.go).

Closes #297.

)

The pkg/aws hot path repeated cfg := c.cfg.Copy(); cfg.Region = region;
ec2.NewFromConfig(cfg) ~40 times across 16 files. Add two helpers on Client —
regionalEC2(region) *ec2.Client and regionalConfig(region) aws.Config — and route
every site through them. Sites that build a non-EC2 client (fsx/s3/ssm/
imagebuilder) or reuse the config use regionalConfig; the rest use regionalEC2.

Retire getRegionalConfig(ctx, region) (aws.Config, error): its returned error was
always nil, so its callers carried dead 'if err != nil' branches — those are
removed too. regionalConfig treats an empty region as a no-op (default-chain
region preserved), matching the prior conditional 'if region != ""' sites, so
behavior is byte-identical. Net -166 lines.

Closes #297.
@scttfrdmn
scttfrdmn force-pushed the refactor/297-regional-ec2 branch from 2a6354b to c594b75 Compare July 15, 2026 20:44
@scttfrdmn
scttfrdmn merged commit 17cd823 into main Jul 15, 2026
6 checks passed
@scttfrdmn
scttfrdmn deleted the refactor/297-regional-ec2 branch July 15, 2026 20:46
@codecov

codecov Bot commented Jul 15, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[audit] Dup: per-region EC2 client boilerplate repeated ~43+ times in pkg/aws — add c.regionalEC2(region)

1 participant