code.dwrz.net

Go monorepo.
Log | Files | Refs

defaultsmode.go (1489B)


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