doc.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. Package session provides configuration for the SDK's service clients.
  3. Sessions can be shared across all service clients that share the same base
  4. configuration. The Session is built from the SDK's default configuration and
  5. request handlers.
  6. Sessions should be cached when possible, because creating a new Session will
  7. load all configuration values from the environment, and config files each time
  8. the Session is created. Sharing the Session value across all of your service
  9. clients will ensure the configuration is loaded the fewest number of times possible.
  10. Concurrency
  11. Sessions are safe to use concurrently as long as the Session is not being
  12. modified. The SDK will not modify the Session once the Session has been created.
  13. Creating service clients concurrently from a shared Session is safe.
  14. Sessions from Shared Config
  15. Sessions can be created using the method above that will only load the
  16. additional config if the AWS_SDK_LOAD_CONFIG environment variable is set.
  17. Alternatively you can explicitly create a Session with shared config enabled.
  18. To do this you can use NewSessionWithOptions to configure how the Session will
  19. be created. Using the NewSessionWithOptions with SharedConfigState set to
  20. SharedConfigEnable will create the session as if the AWS_SDK_LOAD_CONFIG
  21. environment variable was set.
  22. Creating Sessions
  23. When creating Sessions optional aws.Config values can be passed in that will
  24. override the default, or loaded config values the Session is being created
  25. with. This allows you to provide additional, or case based, configuration
  26. as needed.
  27. By default NewSession will only load credentials from the shared credentials
  28. file (~/.aws/credentials). If the AWS_SDK_LOAD_CONFIG environment variable is
  29. set to a truthy value the Session will be created from the configuration
  30. values from the shared config (~/.aws/config) and shared credentials
  31. (~/.aws/credentials) files. See the section Sessions from Shared Config for
  32. more information.
  33. Create a Session with the default config and request handlers. With credentials
  34. region, and profile loaded from the environment and shared config automatically.
  35. Requires the AWS_PROFILE to be set, or "default" is used.
  36. // Create Session
  37. sess := session.Must(session.NewSession())
  38. // Create a Session with a custom region
  39. sess := session.Must(session.NewSession(&aws.Config{
  40. Region: aws.String("us-east-1"),
  41. }))
  42. // Create a S3 client instance from a session
  43. sess := session.Must(session.NewSession())
  44. svc := s3.New(sess)
  45. Create Session With Option Overrides
  46. In addition to NewSession, Sessions can be created using NewSessionWithOptions.
  47. This func allows you to control and override how the Session will be created
  48. through code instead of being driven by environment variables only.
  49. Use NewSessionWithOptions when you want to provide the config profile, or
  50. override the shared config state (AWS_SDK_LOAD_CONFIG).
  51. // Equivalent to session.NewSession()
  52. sess := session.Must(session.NewSessionWithOptions(session.Options{
  53. // Options
  54. }))
  55. // Specify profile to load for the session's config
  56. sess := session.Must(session.NewSessionWithOptions(session.Options{
  57. Profile: "profile_name",
  58. }))
  59. // Specify profile for config and region for requests
  60. sess := session.Must(session.NewSessionWithOptions(session.Options{
  61. Config: aws.Config{Region: aws.String("us-east-1")},
  62. Profile: "profile_name",
  63. }))
  64. // Force enable Shared Config support
  65. sess := session.Must(session.NewSessionWithOptions(session.Options{
  66. SharedConfigState: session.SharedConfigEnable,
  67. }))
  68. Adding Handlers
  69. You can add handlers to a session for processing HTTP requests. All service
  70. clients that use the session inherit the handlers. For example, the following
  71. handler logs every request and its payload made by a service client:
  72. // Create a session, and add additional handlers for all service
  73. // clients created with the Session to inherit. Adds logging handler.
  74. sess := session.Must(session.NewSession())
  75. sess.Handlers.Send.PushFront(func(r *request.Request) {
  76. // Log every request made and its payload
  77. logger.Printf("Request: %s/%s, Payload: %s",
  78. r.ClientInfo.ServiceName, r.Operation, r.Params)
  79. })
  80. Deprecated "New" function
  81. The New session function has been deprecated because it does not provide good
  82. way to return errors that occur when loading the configuration files and values.
  83. Because of this, NewSession was created so errors can be retrieved when
  84. creating a session fails.
  85. Shared Config Fields
  86. By default the SDK will only load the shared credentials file's (~/.aws/credentials)
  87. credentials values, and all other config is provided by the environment variables,
  88. SDK defaults, and user provided aws.Config values.
  89. If the AWS_SDK_LOAD_CONFIG environment variable is set, or SharedConfigEnable
  90. option is used to create the Session the full shared config values will be
  91. loaded. This includes credentials, region, and support for assume role. In
  92. addition the Session will load its configuration from both the shared config
  93. file (~/.aws/config) and shared credentials file (~/.aws/credentials). Both
  94. files have the same format.
  95. If both config files are present the configuration from both files will be
  96. read. The Session will be created from configuration values from the shared
  97. credentials file (~/.aws/credentials) over those in the shared config file (~/.aws/config).
  98. Credentials are the values the SDK should use for authenticating requests with
  99. AWS Services. They are from a configuration file will need to include both
  100. aws_access_key_id and aws_secret_access_key must be provided together in the
  101. same file to be considered valid. The values will be ignored if not a complete
  102. group. aws_session_token is an optional field that can be provided if both of
  103. the other two fields are also provided.
  104. aws_access_key_id = AKID
  105. aws_secret_access_key = SECRET
  106. aws_session_token = TOKEN
  107. Assume Role values allow you to configure the SDK to assume an IAM role using
  108. a set of credentials provided in a config file via the source_profile field.
  109. Both "role_arn" and "source_profile" are required. The SDK supports assuming
  110. a role with MFA token if the session option AssumeRoleTokenProvider
  111. is set.
  112. role_arn = arn:aws:iam::<account_number>:role/<role_name>
  113. source_profile = profile_with_creds
  114. external_id = 1234
  115. mfa_serial = <serial or mfa arn>
  116. role_session_name = session_name
  117. Region is the region the SDK should use for looking up AWS service endpoints
  118. and signing requests.
  119. region = us-east-1
  120. Assume Role with MFA token
  121. To create a session with support for assuming an IAM role with MFA set the
  122. session option AssumeRoleTokenProvider to a function that will prompt for the
  123. MFA token code when the SDK assumes the role and refreshes the role's credentials.
  124. This allows you to configure the SDK via the shared config to assumea role
  125. with MFA tokens.
  126. In order for the SDK to assume a role with MFA the SharedConfigState
  127. session option must be set to SharedConfigEnable, or AWS_SDK_LOAD_CONFIG
  128. environment variable set.
  129. The shared configuration instructs the SDK to assume an IAM role with MFA
  130. when the mfa_serial configuration field is set in the shared config
  131. (~/.aws/config) or shared credentials (~/.aws/credentials) file.
  132. If mfa_serial is set in the configuration, the SDK will assume the role, and
  133. the AssumeRoleTokenProvider session option is not set an an error will
  134. be returned when creating the session.
  135. sess := session.Must(session.NewSessionWithOptions(session.Options{
  136. AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
  137. }))
  138. // Create service client value configured for credentials
  139. // from assumed role.
  140. svc := s3.New(sess)
  141. To setup assume role outside of a session see the stscreds.AssumeRoleProvider
  142. documentation.
  143. Environment Variables
  144. When a Session is created several environment variables can be set to adjust
  145. how the SDK functions, and what configuration data it loads when creating
  146. Sessions. All environment values are optional, but some values like credentials
  147. require multiple of the values to set or the partial values will be ignored.
  148. All environment variable values are strings unless otherwise noted.
  149. Environment configuration values. If set both Access Key ID and Secret Access
  150. Key must be provided. Session Token and optionally also be provided, but is
  151. not required.
  152. # Access Key ID
  153. AWS_ACCESS_KEY_ID=AKID
  154. AWS_ACCESS_KEY=AKID # only read if AWS_ACCESS_KEY_ID is not set.
  155. # Secret Access Key
  156. AWS_SECRET_ACCESS_KEY=SECRET
  157. AWS_SECRET_KEY=SECRET=SECRET # only read if AWS_SECRET_ACCESS_KEY is not set.
  158. # Session Token
  159. AWS_SESSION_TOKEN=TOKEN
  160. Region value will instruct the SDK where to make service API requests to. If is
  161. not provided in the environment the region must be provided before a service
  162. client request is made.
  163. AWS_REGION=us-east-1
  164. # AWS_DEFAULT_REGION is only read if AWS_SDK_LOAD_CONFIG is also set,
  165. # and AWS_REGION is not also set.
  166. AWS_DEFAULT_REGION=us-east-1
  167. Profile name the SDK should load use when loading shared config from the
  168. configuration files. If not provided "default" will be used as the profile name.
  169. AWS_PROFILE=my_profile
  170. # AWS_DEFAULT_PROFILE is only read if AWS_SDK_LOAD_CONFIG is also set,
  171. # and AWS_PROFILE is not also set.
  172. AWS_DEFAULT_PROFILE=my_profile
  173. SDK load config instructs the SDK to load the shared config in addition to
  174. shared credentials. This also expands the configuration loaded so the shared
  175. credentials will have parity with the shared config file. This also enables
  176. Region and Profile support for the AWS_DEFAULT_REGION and AWS_DEFAULT_PROFILE
  177. env values as well.
  178. AWS_SDK_LOAD_CONFIG=1
  179. Shared credentials file path can be set to instruct the SDK to use an alternative
  180. file for the shared credentials. If not set the file will be loaded from
  181. $HOME/.aws/credentials on Linux/Unix based systems, and
  182. %USERPROFILE%\.aws\credentials on Windows.
  183. AWS_SHARED_CREDENTIALS_FILE=$HOME/my_shared_credentials
  184. Shared config file path can be set to instruct the SDK to use an alternative
  185. file for the shared config. If not set the file will be loaded from
  186. $HOME/.aws/config on Linux/Unix based systems, and
  187. %USERPROFILE%\.aws\config on Windows.
  188. AWS_CONFIG_FILE=$HOME/my_shared_config
  189. Path to a custom Credentials Authority (CA) bundle PEM file that the SDK
  190. will use instead of the default system's root CA bundle. Use this only
  191. if you want to replace the CA bundle the SDK uses for TLS requests.
  192. AWS_CA_BUNDLE=$HOME/my_custom_ca_bundle
  193. Enabling this option will attempt to merge the Transport into the SDK's HTTP
  194. client. If the client's Transport is not a http.Transport an error will be
  195. returned. If the Transport's TLS config is set this option will cause the SDK
  196. to overwrite the Transport's TLS config's RootCAs value. If the CA bundle file
  197. contains multiple certificates all of them will be loaded.
  198. The Session option CustomCABundle is also available when creating sessions
  199. to also enable this feature. CustomCABundle session option field has priority
  200. over the AWS_CA_BUNDLE environment variable, and will be used if both are set.
  201. Setting a custom HTTPClient in the aws.Config options will override this setting.
  202. To use this option and custom HTTP client, the HTTP client needs to be provided
  203. when creating the session. Not the service client.
  204. */
  205. package session