defaultsmode.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package config
  2. import (
  3. "context"
  4. "os"
  5. "github.com/aws/aws-sdk-go-v2/aws"
  6. "github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
  7. )
  8. const execEnvVar = "AWS_EXECUTION_ENV"
  9. // DefaultsModeOptions is the set of options that are used to configure
  10. type DefaultsModeOptions struct {
  11. // The SDK configuration defaults mode. Defaults to legacy if not specified.
  12. //
  13. // Supported modes are: auto, cross-region, in-region, legacy, mobile, standard
  14. Mode aws.DefaultsMode
  15. // The EC2 Instance Metadata Client that should be used when performing environment
  16. // discovery when aws.DefaultsModeAuto is set.
  17. //
  18. // If not specified the SDK will construct a client if the instance metadata service has not been disabled by
  19. // the AWS_EC2_METADATA_DISABLED environment variable.
  20. IMDSClient *imds.Client
  21. }
  22. func resolveDefaultsModeRuntimeEnvironment(ctx context.Context, envConfig *EnvConfig, client *imds.Client) (aws.RuntimeEnvironment, error) {
  23. getRegionOutput, err := client.GetRegion(ctx, &imds.GetRegionInput{})
  24. // honor context timeouts, but if we couldn't talk to IMDS don't fail runtime environment introspection.
  25. select {
  26. case <-ctx.Done():
  27. return aws.RuntimeEnvironment{}, err
  28. default:
  29. }
  30. var imdsRegion string
  31. if err == nil {
  32. imdsRegion = getRegionOutput.Region
  33. }
  34. return aws.RuntimeEnvironment{
  35. EnvironmentIdentifier: aws.ExecutionEnvironmentID(os.Getenv(execEnvVar)),
  36. Region: envConfig.Region,
  37. EC2InstanceMetadataRegion: imdsRegion,
  38. }, nil
  39. }