doc.go 9.7 KB

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