Skip to content

Commit e3de372

Browse files
committed
Added getting region from ARN prefix if missing from AWS SDK configuration
1 parent d1f3e7a commit e3de372

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

arnutil/arnutil.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ func ValidateARN(arnString string) bool {
2828
return true
2929
}
3030

31+
// Helper function to get region from ARNs
32+
func GetRegionFromARN(arnString string) string {
33+
arn, err := arn.Parse(arnString)
34+
if err != nil {
35+
log.Warnf("The ARN supplied as argument does not parse successfully: %s", arnString)
36+
return ""
37+
}
38+
return arn.Region
39+
}
40+
3141
// InstanceProfileArn uses the EC2 metadata API to find the role for
3242
// the instance.
3343
func InstanceProfileArn(svc *ec2metadata.EC2Metadata) (arn.ARN, error) {

main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ func main() {
118118
}),
119119
)
120120

121+
// if region is not configured according to AWS SDK docs, but ARN prefix is provided
122+
// the region will be parsed from the ARN prefix
123+
if config.Region == nil && *arnPrefix != "" {
124+
arnRegion := arnutil.GetRegionFromARN(*arnPrefix)
125+
config.Region = &arnRegion
126+
}
127+
121128
session, err := session.NewSessionWithOptions(session.Options{
122129
Config: *config,
123130
})

0 commit comments

Comments
 (0)