github.com/aavshr/aws-sdk-go@v1.41.3/aws/session/doc.go (about)

     1  /*
     2  Package session provides configuration for the SDK's service clients. Sessions
     3  can be shared across service clients that share the same base configuration.
     4  
     5  Sessions are safe to use concurrently as long as the Session is not being
     6  modified. Sessions should be cached when possible, because creating a new
     7  Session will load all configuration values from the environment, and config
     8  files each time the Session is created. Sharing the Session value across all of
     9  your service clients will ensure the configuration is loaded the fewest number
    10  of times possible.
    11  
    12  Sessions options from Shared Config
    13  
    14  By default NewSession will only load credentials from the shared credentials
    15  file (~/.aws/credentials). If the AWS_SDK_LOAD_CONFIG environment variable is
    16  set to a truthy value the Session will be created from the configuration
    17  values from the shared config (~/.aws/config) and shared credentials
    18  (~/.aws/credentials) files. Using the NewSessionWithOptions with
    19  SharedConfigState set to SharedConfigEnable will create the session as if the
    20  AWS_SDK_LOAD_CONFIG environment variable was set.
    21  
    22  Credential and config loading order
    23  
    24  The Session will attempt to load configuration and credentials from the
    25  environment, configuration files, and other credential sources. The order
    26  configuration is loaded in is:
    27  
    28    * Environment Variables
    29    * Shared Credentials file
    30    * Shared Configuration file (if SharedConfig is enabled)
    31    * EC2 Instance Metadata (credentials only)
    32  
    33  The Environment variables for credentials will have precedence over shared
    34  config even if SharedConfig is enabled. To override this behavior, and use
    35  shared config credentials instead specify the session.Options.Profile, (e.g.
    36  when using credential_source=Environment to assume a role).
    37  
    38    sess, err := session.NewSessionWithOptions(session.Options{
    39  	  Profile: "myProfile",
    40    })
    41  
    42  Creating Sessions
    43  
    44  Creating a Session without additional options will load credentials region, and
    45  profile loaded from the environment and shared config automatically. See,
    46  "Environment Variables" section for information on environment variables used
    47  by Session.
    48  
    49  	// Create Session
    50  	sess, err := session.NewSession()
    51  
    52  
    53  When creating Sessions optional aws.Config values can be passed in that will
    54  override the default, or loaded, config values the Session is being created
    55  with. This allows you to provide additional, or case based, configuration
    56  as needed.
    57  
    58  	// Create a Session with a custom region
    59  	sess, err := session.NewSession(&aws.Config{
    60  		Region: aws.String("us-west-2"),
    61  	})
    62  
    63  Use NewSessionWithOptions to provide additional configuration driving how the
    64  Session's configuration will be loaded. Such as, specifying shared config
    65  profile, or override the shared config state,  (AWS_SDK_LOAD_CONFIG).
    66  
    67  	// Equivalent to session.NewSession()
    68  	sess, err := session.NewSessionWithOptions(session.Options{
    69  		// Options
    70  	})
    71  
    72  	sess, err := session.NewSessionWithOptions(session.Options{
    73  		// Specify profile to load for the session's config
    74  		Profile: "profile_name",
    75  
    76  		// Provide SDK Config options, such as Region.
    77  		Config: aws.Config{
    78  			Region: aws.String("us-west-2"),
    79  		},
    80  
    81  		// Force enable Shared Config support
    82  		SharedConfigState: session.SharedConfigEnable,
    83  	})
    84  
    85  Adding Handlers
    86  
    87  You can add handlers to a session to decorate API operation, (e.g. adding HTTP
    88  headers). All clients that use the Session receive a copy of the Session's
    89  handlers. For example, the following request handler added to the Session logs
    90  every requests made.
    91  
    92  	// Create a session, and add additional handlers for all service
    93  	// clients created with the Session to inherit. Adds logging handler.
    94  	sess := session.Must(session.NewSession())
    95  
    96  	sess.Handlers.Send.PushFront(func(r *request.Request) {
    97  		// Log every request made and its payload
    98  		logger.Printf("Request: %s/%s, Params: %s",
    99  			r.ClientInfo.ServiceName, r.Operation, r.Params)
   100  	})
   101  
   102  Shared Config Fields
   103  
   104  By default the SDK will only load the shared credentials file's
   105  (~/.aws/credentials) credentials values, and all other config is provided by
   106  the environment variables, SDK defaults, and user provided aws.Config values.
   107  
   108  If the AWS_SDK_LOAD_CONFIG environment variable is set, or SharedConfigEnable
   109  option is used to create the Session the full shared config values will be
   110  loaded. This includes credentials, region, and support for assume role. In
   111  addition the Session will load its configuration from both the shared config
   112  file (~/.aws/config) and shared credentials file (~/.aws/credentials). Both
   113  files have the same format.
   114  
   115  If both config files are present the configuration from both files will be
   116  read. The Session will be created from configuration values from the shared
   117  credentials file (~/.aws/credentials) over those in the shared config file
   118  (~/.aws/config).
   119  
   120  Credentials are the values the SDK uses to authenticating requests with AWS
   121  Services. When specified in a file, both aws_access_key_id and
   122  aws_secret_access_key must be provided together in the same file to be
   123  considered valid. They will be ignored if both are not present.
   124  aws_session_token is an optional field that can be provided in addition to the
   125  other two fields.
   126  
   127  	aws_access_key_id = AKID
   128  	aws_secret_access_key = SECRET
   129  	aws_session_token = TOKEN
   130  
   131  	; region only supported if SharedConfigEnabled.
   132  	region = us-east-1
   133  
   134  Assume Role configuration
   135  
   136  The role_arn field allows you to configure the SDK to assume an IAM role using
   137  a set of credentials from another source. Such as when paired with static
   138  credentials, "profile_source", "credential_process", or "credential_source"
   139  fields. If "role_arn" is provided, a source of credentials must also be
   140  specified, such as "source_profile", "credential_source", or
   141  "credential_process".
   142  
   143  	role_arn = arn:aws:iam::<account_number>:role/<role_name>
   144  	source_profile = profile_with_creds
   145  	external_id = 1234
   146  	mfa_serial = <serial or mfa arn>
   147  	role_session_name = session_name
   148  
   149  
   150  The SDK supports assuming a role with MFA token. If "mfa_serial" is set, you
   151  must also set the Session Option.AssumeRoleTokenProvider. The Session will fail
   152  to load if the AssumeRoleTokenProvider is not specified.
   153  
   154      sess := session.Must(session.NewSessionWithOptions(session.Options{
   155          AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
   156      }))
   157  
   158  To setup Assume Role outside of a session see the stscreds.AssumeRoleProvider
   159  documentation.
   160  
   161  Environment Variables
   162  
   163  When a Session is created several environment variables can be set to adjust
   164  how the SDK functions, and what configuration data it loads when creating
   165  Sessions. All environment values are optional, but some values like credentials
   166  require multiple of the values to set or the partial values will be ignored.
   167  All environment variable values are strings unless otherwise noted.
   168  
   169  Environment configuration values. If set both Access Key ID and Secret Access
   170  Key must be provided. Session Token and optionally also be provided, but is
   171  not required.
   172  
   173  	# Access Key ID
   174  	AWS_ACCESS_KEY_ID=AKID
   175  	AWS_ACCESS_KEY=AKID # only read if AWS_ACCESS_KEY_ID is not set.
   176  
   177  	# Secret Access Key
   178  	AWS_SECRET_ACCESS_KEY=SECRET
   179  	AWS_SECRET_KEY=SECRET=SECRET # only read if AWS_SECRET_ACCESS_KEY is not set.
   180  
   181  	# Session Token
   182  	AWS_SESSION_TOKEN=TOKEN
   183  
   184  Region value will instruct the SDK where to make service API requests to. If is
   185  not provided in the environment the region must be provided before a service
   186  client request is made.
   187  
   188  	AWS_REGION=us-east-1
   189  
   190  	# AWS_DEFAULT_REGION is only read if AWS_SDK_LOAD_CONFIG is also set,
   191  	# and AWS_REGION is not also set.
   192  	AWS_DEFAULT_REGION=us-east-1
   193  
   194  Profile name the SDK should load use when loading shared config from the
   195  configuration files. If not provided "default" will be used as the profile name.
   196  
   197  	AWS_PROFILE=my_profile
   198  
   199  	# AWS_DEFAULT_PROFILE is only read if AWS_SDK_LOAD_CONFIG is also set,
   200  	# and AWS_PROFILE is not also set.
   201  	AWS_DEFAULT_PROFILE=my_profile
   202  
   203  SDK load config instructs the SDK to load the shared config in addition to
   204  shared credentials. This also expands the configuration loaded so the shared
   205  credentials will have parity with the shared config file. This also enables
   206  Region and Profile support for the AWS_DEFAULT_REGION and AWS_DEFAULT_PROFILE
   207  env values as well.
   208  
   209  	AWS_SDK_LOAD_CONFIG=1
   210  
   211  Custom Shared Config and Credential Files
   212  
   213  Shared credentials file path can be set to instruct the SDK to use an alternative
   214  file for the shared credentials. If not set the file will be loaded from
   215  $HOME/.aws/credentials on Linux/Unix based systems, and
   216  %USERPROFILE%\.aws\credentials on Windows.
   217  
   218  	AWS_SHARED_CREDENTIALS_FILE=$HOME/my_shared_credentials
   219  
   220  Shared config file path can be set to instruct the SDK to use an alternative
   221  file for the shared config. If not set the file will be loaded from
   222  $HOME/.aws/config on Linux/Unix based systems, and
   223  %USERPROFILE%\.aws\config on Windows.
   224  
   225  	AWS_CONFIG_FILE=$HOME/my_shared_config
   226  
   227  Custom CA Bundle
   228  
   229  Path to a custom Credentials Authority (CA) bundle PEM file that the SDK
   230  will use instead of the default system's root CA bundle. Use this only
   231  if you want to replace the CA bundle the SDK uses for TLS requests.
   232  
   233  	AWS_CA_BUNDLE=$HOME/my_custom_ca_bundle
   234  
   235  Enabling this option will attempt to merge the Transport into the SDK's HTTP
   236  client. If the client's Transport is not a http.Transport an error will be
   237  returned. If the Transport's TLS config is set this option will cause the SDK
   238  to overwrite the Transport's TLS config's  RootCAs value. If the CA bundle file
   239  contains multiple certificates all of them will be loaded.
   240  
   241  The Session option CustomCABundle is also available when creating sessions
   242  to also enable this feature. CustomCABundle session option field has priority
   243  over the AWS_CA_BUNDLE environment variable, and will be used if both are set.
   244  
   245  Setting a custom HTTPClient in the aws.Config options will override this setting.
   246  To use this option and custom HTTP client, the HTTP client needs to be provided
   247  when creating the session. Not the service client.
   248  
   249  Custom Client TLS Certificate
   250  
   251  The SDK supports the environment and session option being configured with
   252  Client TLS certificates that are sent as a part of the client's TLS handshake
   253  for client authentication. If used, both Cert and Key values are required. If
   254  one is missing, or either fail to load the contents of the file an error will
   255  be returned.
   256  
   257  HTTP Client's Transport concrete implementation must be a http.Transport
   258  or creating the session will fail.
   259  
   260  	AWS_SDK_GO_CLIENT_TLS_KEY=$HOME/my_client_key
   261  	AWS_SDK_GO_CLIENT_TLS_CERT=$HOME/my_client_cert
   262  
   263  This can also be configured via the session.Options ClientTLSCert and ClientTLSKey.
   264  
   265  	sess, err := session.NewSessionWithOptions(session.Options{
   266  		ClientTLSCert: myCertFile,
   267  		ClientTLSKey: myKeyFile,
   268  	})
   269  
   270  Custom EC2 IMDS Endpoint
   271  
   272  The endpoint of the EC2 IMDS client can be configured via the environment
   273  variable, AWS_EC2_METADATA_SERVICE_ENDPOINT when creating the client with a
   274  Session. See Options.EC2IMDSEndpoint for more details.
   275  
   276    AWS_EC2_METADATA_SERVICE_ENDPOINT=http://169.254.169.254
   277  
   278  If using an URL with an IPv6 address literal, the IPv6 address
   279  component must be enclosed in square brackets.
   280  
   281    AWS_EC2_METADATA_SERVICE_ENDPOINT=http://[::1]
   282  
   283  The custom EC2 IMDS endpoint can also be specified via the Session options.
   284  
   285    sess, err := session.NewSessionWithOptions(session.Options{
   286        EC2MetadataEndpoint: "http://[::1]",
   287    })
   288  */
   289  package session