core.adoc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. = Spring Cloud AWS Core
  2. Each Spring Cloud AWS module uses `AwsCredentialsProvider` and `AwsRegionProvider` to get the AWS region and access credentials.
  3. Spring Cloud AWS provides a Spring Boot starter to auto-configure the core components.
  4. Maven coordinates, using Spring Cloud AWS BOM:
  5. [source,xml]
  6. ----
  7. <dependency>
  8. <groupId>io.awspring.cloud</groupId>
  9. <artifactId>spring-cloud-aws-starter</artifactId>
  10. </dependency>
  11. ----
  12. == Credentials
  13. `software.amazon.awssdk.auth.credentials.AwsCredentialsProvider` is a functional interface that returns the credentials to authenticate and authorize calls to AWS services.
  14. [source,java]
  15. ----
  16. public interface AwsCredentialsProvider {
  17. AwsCredentials resolveCredentials();
  18. }
  19. ----
  20. There are 3 ways in which the `AwsCredentialsProvider` in Spring Cloud AWS can be configured:
  21. 1. `DefaultCredentialsProvider`
  22. 2. `StsWebIdentityTokenFileCredentialsProvider` - recommended for EKS
  23. 3. Custom `AwsCredentialsProvider`
  24. If you are having problems with configuring credentials, consider enabling debug logging for more info:
  25. [source,properties]
  26. ----
  27. logging.level.io.awspring.cloud=debug
  28. ----
  29. === DefaultCredentialsProvider
  30. By default, Spring Cloud AWS starter auto-configures a `DefaultCredentialsProvider`, which looks for AWS credentials in this order:
  31. 1. Java System Properties - `aws.accessKeyId` and `aws.secretAccessKey`
  32. 2. Environment Variables - `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`
  33. 3. Web Identity Token credentials from system properties or environment variables
  34. 4. Credential profiles file at the default location (`~/.aws/credentials`) shared by all AWS SDKs and the AWS CLI
  35. 5. Credentials delivered through the Amazon EC2 container service if `AWS_CONTAINER_CREDENTIALS_RELATIVE_URI`" environment variable is set and security manager has permission to access the variable,
  36. 6. Instance profile credentials delivered through the Amazon EC2 metadata service
  37. If it does not serve your project needs, this behavior can be changed by setting additional properties:
  38. [cols="3*", options="header"]
  39. |===
  40. |property
  41. |example
  42. |description
  43. |spring.cloud.aws.credentials.access-key
  44. |AKIAIOSFODNN7EXAMPLE
  45. |The access key to be used with a static provider
  46. |spring.cloud.aws.credentials.secret-key
  47. |wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
  48. |The secret key to be used with a static provider
  49. |spring.cloud.aws.credentials.instance-profile
  50. |true
  51. |Configures an https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/auth/credentials/InstanceProfileCredentialsProvider.html[InstanceProfileCredentialsProvider] with no further configuration
  52. |spring.cloud.aws.credentials.profile.name
  53. |default
  54. |The name of a configuration profile in the specified configuration file
  55. |spring.cloud.aws.credentials.profile.path
  56. |`~/.aws/credentials`
  57. |The file path where the profile configuration file is located. Defaults to `~/.aws/credentials` if a value is not provided
  58. |===
  59. === StsWebIdentityTokenFileCredentialsProvider
  60. The `StsWebIdentityTokenFileCredentialsProvider` allows your application to assume an AWS IAM Role using a web identity token file, which is especially useful in Kubernetes and AWS EKS environments.
  61. ==== Prerequisites
  62. 1. Create a role that you want to assume.
  63. 2. Create a web identity token file for your application.
  64. In EKS, please follow this guide to set up service accounts https://docs.aws.amazon.com/eks/latest/userguide/pod-configuration.html
  65. The `StsWebIdentityTokenFileCredentialsProvider` support is optional, so you need to include the following Maven dependency:
  66. [source,xml,indent=0]
  67. ----
  68. <dependency>
  69. <groupId>software.amazon.awssdk</groupId>
  70. <artifactId>sts</artifactId>
  71. </dependency>
  72. ----
  73. ==== Configuring
  74. In EKS no additional configuration is required as the service account already configures the correct environment variables; however, they can be overridden.
  75. STS credentials configuration supports following properties:
  76. [cols="2,3,1,1"]
  77. |===
  78. | Name | Description | Required | Default value
  79. | `spring.cloud.aws.credentials.sts.role-arn` | ARN of IAM role associated with STS. | No | `null` (falls back to SDK default)
  80. | `spring.cloud.aws.credentials.sts.web-identity-token-file` | Absolute path to the web identity token file that will be used by credentials provider. | No | `null` (falls back to SDK default)
  81. | `spring.cloud.aws.credentials.sts.is-async-credentials-update` | Enables provider to asynchronously fetch credentials in the background. | No | `false`
  82. | `spring.cloud.aws.credentials.sts.role-session-name` | Role session name that will be used by credentials provider. | No | `null` (falls back to SDK default)
  83. |===
  84. === Custom AwsCredentialsProvider
  85. It is also possible to configure custom `AwsCredentialsProvider` bean which will prevent Spring Cloud AWS from auto-configuring credentials provider:
  86. [source,java,indent=0]
  87. ----
  88. import org.springframework.context.annotation.Bean;
  89. import org.springframework.context.annotation.Configuration;
  90. import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
  91. @Configuration
  92. class CustomCredentialsProviderConfiguration {
  93. @Bean
  94. public AwsCredentialsProvider customAwsCredentialsProvider() {
  95. return new CustomAWSCredentialsProvider();
  96. }
  97. }
  98. ----
  99. == Region
  100. `software.amazon.awssdk.regions.providers.AwsRegionProvider` is a functional interface that returns the region AWS clients issue requests to.
  101. [source,java]
  102. ----
  103. public interface AwsRegionProvider {
  104. Region getRegion();
  105. }
  106. ----
  107. By default, Spring Cloud AWS starter auto-configures a `DefaultAwsRegionProviderChain`, which looks resolves AWS region in this order:
  108. 1. Check the `aws.region` system property for the region.
  109. 2. Check the `AWS_REGION` environment variable for the region.
  110. 3. Check the `{user.home}/.aws/credentials` and `{user.home}/.aws/config` files for the region.
  111. 4. If running in EC2, check the EC2 metadata service for the region.
  112. If it does not serve your project needs, this behavior can be changed by setting additional properties:
  113. [cols="3*", options="header"]
  114. |===
  115. |property
  116. |example
  117. |description
  118. |spring.cloud.aws.region.static
  119. |eu-west-1
  120. |A static value for region used by auto-configured AWS clients
  121. |spring.cloud.aws.region.instance-profile
  122. |true
  123. |Configures an https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/regions/providers/InstanceProfileRegionProvider.html[InstanceProfileRegionProvider] with no further configuration
  124. |spring.cloud.aws.region.profile.name
  125. |default
  126. |The name of a configuration profile in the specified configuration file
  127. |spring.cloud.aws.region.profile.path
  128. |`~/.aws/credentials`
  129. |The file path where the profile configuration file is located. Defaults to `~/.aws/credentials` if value is not provided
  130. |===
  131. It is also possible to configure custom `AwsRegionProvider` bean which will prevent Spring Cloud AWS from auto-configuring region provider:
  132. [source,java,indent=0]
  133. ----
  134. import org.springframework.context.annotation.Bean;
  135. import org.springframework.context.annotation.Configuration;
  136. import software.amazon.awssdk.regions.providers.AwsRegionProvider;
  137. @Configuration
  138. class CustomRegionProviderConfiguration {
  139. @Bean
  140. public AwsRegionProvider customRegionProvider() {
  141. return new CustomRegionProvider();
  142. }
  143. }
  144. ----
  145. == Endpoint
  146. To simplify using services with AWS compatible APIs, or running applications against https://localstack.cloud/[LocalStack], it is possible to configure an endpoint set on all auto-configured AWS clients:
  147. [cols="3*", options="header"]
  148. |===
  149. |property
  150. |example
  151. |description
  152. |`spring.cloud.aws.endpoint`
  153. |`http://localhost:4566`
  154. |endpoint url applied to auto-configured AWS clients
  155. |===
  156. == Customizing AWS Clients
  157. To configure an AWS client with custom HTTP client or `ClientOverrideConfiguration`, define a bean of type `AwsClientConfigurer` with a type parameter indicating configured client builder.
  158. [source,java,indent=0]
  159. ----
  160. import io.awspring.cloud.autoconfigure.core.AwsClientCustomizer;
  161. import org.springframework.context.annotation.Bean;
  162. import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
  163. import software.amazon.awssdk.http.SdkHttpClient;
  164. import software.amazon.awssdk.http.apache.ApacheHttpClient;
  165. import software.amazon.awssdk.services.sns.SnsClientBuilder;
  166. import java.time.Duration;
  167. @Configuration
  168. class S3AwsClientConfigurerConfiguration {
  169. @Bean
  170. AwsClientCustomizer<S3ClientBuilder> s3ClientBuilderAwsClientConfigurer() {
  171. return new S3AwsClientClientConfigurer();
  172. }
  173. static class S3AwsClientClientConfigurer implements AwsClientCustomizer<S3ClientBuilder> {
  174. @Override
  175. public ClientOverrideConfiguration overrideConfiguration() {
  176. return ClientOverrideConfiguration.builder().apiCallTimeout(Duration.ofMillis(500)).build();
  177. }
  178. @Override
  179. public SdkHttpClient httpClient() {
  180. return ApacheHttpClient.builder().connectionTimeout(Duration.ofMillis(1000)).build();
  181. }
  182. }
  183. }
  184. ----