-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for region discovery issue with aws sdkv2 when running in ec2
- Loading branch information
Showing
5 changed files
with
48 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package agent | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds" | ||
) | ||
|
||
func GetAWSConfigV2(ctx context.Context, optFns ...func(*config.LoadOptions) error) (cfg aws.Config, err error) { | ||
cfg, err = config.LoadDefaultConfig(ctx, optFns...) | ||
if err != nil { | ||
return cfg, fmt.Errorf("error loading default config: %w", err) | ||
} | ||
|
||
// local configuration resolved a region so we can return | ||
if cfg.Region != "" { | ||
return cfg, nil | ||
} | ||
|
||
// we need to fall back to the ec2 imds service to get the region | ||
client := imds.NewFromConfig(cfg) | ||
|
||
var regionResult *imds.GetRegionOutput | ||
regionResult, err = client.GetRegion(ctx, &imds.GetRegionInput{}) | ||
if err != nil { | ||
return cfg, fmt.Errorf("error getting region using imds: %w", err) | ||
} | ||
|
||
optFns = append(optFns, config.WithRegion(regionResult.Region)) | ||
|
||
cfg, err = config.LoadDefaultConfig(ctx, optFns...) | ||
if err != nil { | ||
return cfg, fmt.Errorf("error loading default config using imds region: %w", err) | ||
} | ||
|
||
return cfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters