@@ -174,7 +174,6 @@ const (
174174
175175// Options provides the means to control how a Session is created and what
176176// configuration values will be loaded.
177- //
178177type Options struct {
179178 // Provides config values for the SDK to use when creating service clients
180179 // and making API requests to services. Any value set in with this field
@@ -322,24 +321,24 @@ type Options struct {
322321// credentials file. Enabling the Shared Config will also allow the Session
323322// to be built with retrieving credentials with AssumeRole set in the config.
324323//
325- // // Equivalent to session.New
326- // sess := session.Must(session.NewSessionWithOptions(session.Options{}))
324+ // // Equivalent to session.New
325+ // sess := session.Must(session.NewSessionWithOptions(session.Options{}))
327326//
328- // // Specify profile to load for the session's config
329- // sess := session.Must(session.NewSessionWithOptions(session.Options{
330- // Profile: "profile_name",
331- // }))
327+ // // Specify profile to load for the session's config
328+ // sess := session.Must(session.NewSessionWithOptions(session.Options{
329+ // Profile: "profile_name",
330+ // }))
332331//
333- // // Specify profile for config and region for requests
334- // sess := session.Must(session.NewSessionWithOptions(session.Options{
335- // Config: aws.Config{Region: aws.String("us-east-1")},
336- // Profile: "profile_name",
337- // }))
332+ // // Specify profile for config and region for requests
333+ // sess := session.Must(session.NewSessionWithOptions(session.Options{
334+ // Config: aws.Config{Region: aws.String("us-east-1")},
335+ // Profile: "profile_name",
336+ // }))
338337//
339- // // Force enable Shared Config support
340- // sess := session.Must(session.NewSessionWithOptions(session.Options{
341- // SharedConfigState: session.SharedConfigEnable,
342- // }))
338+ // // Force enable Shared Config support
339+ // sess := session.Must(session.NewSessionWithOptions(session.Options{
340+ // SharedConfigState: session.SharedConfigEnable,
341+ // }))
343342func NewSessionWithOptions (opts Options ) (* Session , error ) {
344343 var envCfg envConfig
345344 var err error
@@ -375,7 +374,7 @@ func NewSessionWithOptions(opts Options) (*Session, error) {
375374// This helper is intended to be used in variable initialization to load the
376375// Session and configuration at startup. Such as:
377376//
378- // var sess = session.Must(session.NewSession())
377+ // var sess = session.Must(session.NewSession())
379378func Must (sess * Session , err error ) * Session {
380379 if err != nil {
381380 panic (err )
@@ -780,16 +779,6 @@ func mergeConfigSrcs(cfg, userCfg *aws.Config,
780779 cfg .EndpointResolver = wrapEC2IMDSEndpoint (cfg .EndpointResolver , ec2IMDSEndpoint , endpointMode )
781780 }
782781
783- // Configure credentials if not already set by the user when creating the
784- // Session.
785- if cfg .Credentials == credentials .AnonymousCredentials && userCfg .Credentials == nil {
786- creds , err := resolveCredentials (cfg , envCfg , sharedCfg , handlers , sessOpts )
787- if err != nil {
788- return err
789- }
790- cfg .Credentials = creds
791- }
792-
793782 cfg .S3UseARNRegion = userCfg .S3UseARNRegion
794783 if cfg .S3UseARNRegion == nil {
795784 cfg .S3UseARNRegion = & envCfg .S3UseARNRegion
@@ -812,6 +801,17 @@ func mergeConfigSrcs(cfg, userCfg *aws.Config,
812801 }
813802 }
814803
804+ // Configure credentials if not already set by the user when creating the Session.
805+ // Credentials are resolved last such that all _resolved_ config values are propagated to credential providers.
806+ // ticket: P83606045
807+ if cfg .Credentials == credentials .AnonymousCredentials && userCfg .Credentials == nil {
808+ creds , err := resolveCredentials (cfg , envCfg , sharedCfg , handlers , sessOpts )
809+ if err != nil {
810+ return err
811+ }
812+ cfg .Credentials = creds
813+ }
814+
815815 return nil
816816}
817817
@@ -845,8 +845,8 @@ func initHandlers(s *Session) {
845845// and handlers. If any additional configs are provided they will be merged
846846// on top of the Session's copied config.
847847//
848- // // Create a copy of the current Session, configured for the us-west-2 region.
849- // sess.Copy(&aws.Config{Region: aws.String("us-west-2")})
848+ // // Create a copy of the current Session, configured for the us-west-2 region.
849+ // sess.Copy(&aws.Config{Region: aws.String("us-west-2")})
850850func (s * Session ) Copy (cfgs ... * aws.Config ) * Session {
851851 newSession := & Session {
852852 Config : s .Config .Copy (cfgs ... ),
0 commit comments