@zio.dev/zio-aws 2026.11.18-53be30dc3ac8 → 2026.11.23-0451d341c87f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/artifacts.md CHANGED
@@ -23,6 +23,7 @@ title: Artifacts
23
23
  ## List of all the generated libraries:
24
24
 
25
25
  ```scala
26
+ "dev.zio" %% "zio-aws-abusesignal" % "<version>"
26
27
  "dev.zio" %% "zio-aws-accessanalyzer" % "<version>"
27
28
  "dev.zio" %% "zio-aws-account" % "<version>"
28
29
  "dev.zio" %% "zio-aws-accountaccess" % "<version>"
@@ -324,6 +325,7 @@ title: Artifacts
324
325
  "dev.zio" %% "zio-aws-partnercentralaccount" % "<version>"
325
326
  "dev.zio" %% "zio-aws-partnercentralbenefits" % "<version>"
326
327
  "dev.zio" %% "zio-aws-partnercentralchannel" % "<version>"
328
+ "dev.zio" %% "zio-aws-partnercentralmarketing" % "<version>"
327
329
  "dev.zio" %% "zio-aws-partnercentralrevenuemeasurement" % "<version>"
328
330
  "dev.zio" %% "zio-aws-partnercentralselling" % "<version>"
329
331
  "dev.zio" %% "zio-aws-paymentcryptography" % "<version>"
package/aspects.md CHANGED
@@ -45,7 +45,7 @@ val callLogging: AwsCallAspect[Any]
45
45
  def callDuration(prefix: String, boundaries: MetricKeyType.Histogram.Boundaries): AwsCallAspect[Any]
46
46
  ```
47
47
 
48
- `callLogging` logs one line for each call in the form `[service/operation] ran for <duration>`, using ZIO's logging. It logs successful calls only, because a failed call never reaches the point where the duration is logged. Use `logFailures`-style aspects, shown in the how-to guide, to log errors.
48
+ `callLogging` logs one line for each call in the form `[service/operation] ran for <duration>`, using ZIO's logging. It logs successful calls only, because a failed call never reaches the point where the duration is logged. To log failures, write an aspect of your own, as the [Add Aspects](./guide-add-aspects.md) guide does.
49
49
 
50
50
  `callDuration` records each call's duration in a histogram named `<prefix>_aws_call`. The histogram has the tags `aws_service` and `aws_operation`, and the `boundaries` are the upper limits of its buckets in seconds. Create the boundaries with `MetricKeyType.Histogram.Boundaries.fromChunk`.
51
51
 
@@ -63,6 +63,28 @@ The operator works on any `ZLayer` whose output is a service that extends `Aspec
63
63
  val observed = Ec2.live @@@ (logFailures >>> callLogging)
64
64
  ```
65
65
 
66
+ ## Attaching an Aspect to a Service Value
67
+
68
+ When you hold a service value instead of a layer, for example one built with `Ec2.scoped`, attach the aspect with `AspectSupport#withAspect`. Every generated service extends `AspectSupport`:
69
+
70
+ ```scala
71
+ trait AspectSupport[Self] {
72
+ def withAspect[R](newAspect: AwsCallAspect[R], r: ZEnvironment[R]): Self
73
+ }
74
+ ```
75
+
76
+ `AspectSupport#withAspect` returns a copy of the service whose operations run through `newAspect`, with the aspect's environment taken from `r`. It replaces the aspect the service had; to keep both, combine them with `>>>` first. `@@@` is built on `AspectSupport#withAspect`. For an aspect that needs nothing, pass `ZEnvironment.empty`:
77
+
78
+ ```scala
79
+ import zio._
80
+ import zio.aws.core.aspects._
81
+ import zio.aws.core.config.AwsConfig
82
+ import zio.aws.ec2.Ec2
83
+
84
+ val loggedEc2: ZIO[AwsConfig & Scope, Throwable, Ec2] =
85
+ Ec2.scoped(identity).map(_.withAspect(callLogging, ZEnvironment.empty))
86
+ ```
87
+
66
88
  ## Writing an Aspect
67
89
 
68
90
  An aspect is a class that extends `AwsCallAspect[R]` and implements `apply`. The library's own aspects use this signature:
@@ -71,9 +93,11 @@ An aspect is a class that extends `AwsCallAspect[R]` and implements `apply`. The
71
93
  override final def apply[R, E, A <: Described[_]](f: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]
72
94
  ```
73
95
 
74
- The `Described[_]` bound is part of the signature, so an aspect cannot be applied to a plain `ZIO` that is not a generated AWS call.
96
+ The `Described[_]` bound is part of the signature, so an aspect cannot be applied to a plain `ZIO` that is not a generated AWS call. To use the error, declare the bounds `E >: AwsError <: AwsError`, which tell the compiler that the error is exactly `AwsError`; the [Add Retries](./guide-add-retries.md) guide uses them to retry with a `Schedule`.
97
+
98
+ An aspect runs once for each AWS request. For an operation that ZIO AWS pages itself, that is once per page; for an operation that uses the Java SDK paginator, it is once for the whole stream, and calls made through a service's `api` field do not run through aspects at all.
75
99
 
76
100
  ## Going Further
77
101
 
78
102
  - The [Add Aspects for Logging and Metrics](./guide-add-aspects.md) guide walks through attaching the built-in aspects and writing one of your own.
79
- - The [Examples](./examples.md) page shows a complete program that builds its layers from `AwsConfig`.
103
+ - The [Add Retries](./guide-add-retries.md) guide builds a retry aspect.
package/changelog.md CHANGED
@@ -3,10 +3,18 @@ id: changelog
3
3
  title: Changelog
4
4
  ---
5
5
 
6
- # Version history
6
+ This is a manually maintained list of important changes. Releases are published automatically from CI, so this list may not show every change right away. Dependency updates are not listed.
7
7
 
8
- Note: this is a manually maintained list of important changes. Because of having auto-release from CI, this
9
- list may not reflect _all_ changes immediately. Dependencies are continuously updated and are not documented here.
8
+ ZIO AWS versions have four parts, such as `7.59.2.5`. The first part is the ZIO AWS major version, the second and third parts are the minor and patch version of the AWS Java SDK the release is built on (here AWS SDK `2.59.2`), and the last part counts the releases built on that SDK version. The [Releases](https://github.com/zio/zio-aws/releases) page on GitHub lists every release.
9
+
10
+ #### 7.41.17.2
11
+ - `endpointOverride` in configuration must now start with `http://` or `https://`. A value without a scheme fails when the configuration is loaded, instead of causing a `NullPointerException` in the AWS SDK ([#626](https://github.com/zio/zio-aws/issues/626)).
12
+
13
+ #### 7.31.77.7
14
+ - Scala 2.12 is no longer published. ZIO AWS is published for Scala 2.13 and Scala 3.
15
+
16
+ #### 7.30.15.2
17
+ - New HTTP client module, `zio-aws-crt-http`, built on the AWS Common Runtime (CRT) HTTP client.
10
18
 
11
19
  #### 7.x.x.x
12
20
  - ZIO 2.0.18 introduced a `ZLayerAspect` which uses the same operator (`@@`) that ZIO AWS was using on layers, but it is not powerful enough to express this library's aspects. The new operator conflicts with the existing extension method, so starting from this version it has been renamed from `@@` to `@@@` to avoid the conflict.
package/configuration.md CHANGED
@@ -1,30 +1,93 @@
1
1
  ---
2
2
  id: configuration
3
3
  title: Configuration
4
+ description: "Reference for AwsConfig and its three constructors, CommonAwsConfig, CommonClientConfig, ClientCustomization and the ZIO Config keys that describe them."
5
+ keywords:
6
+ - "ZIO AWS"
7
+ - "AwsConfig"
8
+ - "CommonAwsConfig"
9
+ - "ClientCustomization"
10
+ - "ZIO Config"
4
11
  ---
5
12
 
13
+ Each service layer depends on an `AwsConfig` layer. `AwsConfig` sets up the AWS Java SDK's async client of every service: it applies the common settings to the client builder and plugs in the [HTTP client](http.md). One `AwsConfig` layer is shared by every service in a program.
6
14
 
7
- ## Common configuration
15
+ ## AwsConfig
8
16
 
9
- Each _service module_ depends on the `AwsConfig` layer. This layer is responsible for setting up the
10
- AWS Java SDK's async client, by setting the [underlying HTTP engine](http.md) and all the common
11
- settings. You can use the following layers to provide `AwsConfig`:
17
+ `AwsConfig` is a trait in `zio.aws.core.config` with two methods, which every generated service calls while it builds its client:
12
18
 
13
- ### Default
14
- `AwsConfig.default` requires a `HttpClient` as dependency, but does not customize any other setting of the client
19
+ ```scala
20
+ trait AwsConfig {
21
+ def configure[Client, Builder <: AwsClientBuilder[Builder, Client]](builder: Builder): Task[Builder]
22
+
23
+ def configureHttpClient[Client, Builder <: AwsAsyncClientBuilder[Builder, Client]](
24
+ builder: Builder,
25
+ serviceCaps: ServiceHttpCapabilities
26
+ ): Task[Builder]
27
+ }
28
+ ```
29
+
30
+ You rarely implement it yourself; the companion object provides three layers, described below. All three need an `HttpClient`.
31
+
32
+ ### AwsConfig.default
33
+
34
+ `AwsConfig.default: ZLayer[HttpClient, Nothing, AwsConfig]` sets only the HTTP client. The region, credentials, endpoint, timeouts and retries are left to the AWS SDK, which reads them from its standard sources, such as the `AWS_REGION` environment variable and the `~/.aws` profile files.
35
+
36
+ ### AwsConfig.configured
37
+
38
+ `AwsConfig.configured(): ZLayer[HttpClient & CommonAwsConfig, Nothing, AwsConfig]` is the recommended layer. It also needs a `CommonAwsConfig`, which you can build in code with `ZLayer.succeed(CommonAwsConfig(...))` or load from any [ZIO Config](https://zio.dev/zio-config/) source with `ZLayer.fromZIO(ZIO.config(commonAwsConfig))`, using the descriptor from `zio.aws.core.config.descriptors`. The [Configure Credentials and Region](guide-configure-credentials-and-region.md) guide shows both.
39
+
40
+ The layer applies the fields of `CommonAwsConfig` to every client builder:
41
+
42
+ - `region`, when set, becomes the client's region.
43
+ - `credentialsProvider` is always applied.
44
+ - `endpointOverride`, when set, replaces the AWS endpoint, for example to reach [LocalStack](guide-use-a-local-endpoint.md).
45
+ - `commonClientConfig`, when set, becomes the client's override configuration with the extra headers, timeouts and profile name, and **turns off the AWS SDK's own retries**. Add retries with an [aspect](guide-add-retries.md) instead. When it is `None`, the SDK keeps its default retry behaviour.
46
+
47
+ ### AwsConfig.customized
48
+
49
+ `AwsConfig.customized(customization: ClientCustomization): ZLayer[HttpClient, Nothing, AwsConfig]` passes every client builder to a `ClientCustomization`, and does not read a `CommonAwsConfig`. `ClientCustomization` has one generic method, so that the same code works for the builders of all services:
50
+
51
+ ```scala
52
+ trait ClientCustomization {
53
+ def customize[Client, Builder <: AwsClientBuilder[Builder, Client]](builder: Builder): Builder
54
+ }
55
+ ```
56
+
57
+ `ClientCustomization.None` returns the builder unchanged; `AwsConfig.default` is `AwsConfig.customized(ClientCustomization.None)`. The [Customize Clients](guide-call-the-sdk-client-directly.md) guide implements one. To change the builder of one service only, use the service's `customized` constructor, described in [Wrappers](wrappers.md).
15
58
 
16
- ### Fully customized
17
- `AwsConfig.customized(customization)` gives the freedom to customize the creation of the AWS async client directly by modifying it's `Builder`
59
+ ## CommonAwsConfig
18
60
 
19
- ### Configured
20
- `AwsConfig.configured()` is the *recommended* way to construct an `AwsConfig`. Beside requiring a `HttpClient` it also requires a `CommonAwsConfig` service.
21
- The `CommonAwsConfig` value can be either provided from code, for example by `ZLayer.succeed(CommonAwsConfig(...))`, or it can
22
- be read from any of the supported config sources by [zio-config](https://zio.dev/zio-config/), for example with `ZLayer.fromZIO(ZIO.config(commonAwsConfig))`, where `commonAwsConfig` is the descriptor in `zio.aws.core.config.descriptors`. The [Configure Credentials and Region](./guide-configure-credentials-and-region.md) guide shows both ways.
61
+ `CommonAwsConfig` holds the settings that `AwsConfig.configured()` applies:
23
62
 
24
- When `commonClientConfig` is set, `configured()` also turns off the AWS SDK's own retries. Retries are not exposed as a setting in `CommonAwsConfig`. The recommended way to handle retries is to use [aspects on the service layers](aspects.md).
25
-
26
- See the following table about the possible configuration values. Please note that the underlying HTTP engine also has its own
27
- specific configuration which is described [on the page about the HTTP engines](http.md). The table below lists the common keys, generated from the `commonAwsConfig` descriptor:
63
+ ```scala
64
+ case class CommonAwsConfig(
65
+ region: Option[Region],
66
+ credentialsProvider: AwsCredentialsProvider,
67
+ endpointOverride: Option[URI],
68
+ commonClientConfig: Option[CommonClientConfig]
69
+ )
70
+
71
+ case class CommonClientConfig(
72
+ extraHeaders: Map[String, List[String]],
73
+ apiCallTimeout: Option[Duration],
74
+ apiCallAttemptTimeout: Option[Duration],
75
+ defaultProfileName: Option[String]
76
+ )
77
+ ```
78
+
79
+ `Region`, `AwsCredentialsProvider` and `URI` are the AWS SDK and Java types, and `Duration` is ZIO's. `apiCallTimeout` limits a whole call, including retries, and `apiCallAttemptTimeout` limits one HTTP attempt.
80
+
81
+ ## Configuration Keys
82
+
83
+ The `commonAwsConfig` descriptor in `zio.aws.core.config.descriptors` reads a `CommonAwsConfig` from these keys:
84
+
85
+ - `region` is an AWS region name, such as `eu-west-1`. It is optional.
86
+ - `credentials.type` selects the credentials provider: `default` (the SDK's default chain, also used when the `credentials` section is missing), `anonymous`, `instance-profile` or `static`. The `static` type also reads `credentials.accessKeyId` and `credentials.secretAccessKey`.
87
+ - `endpointOverride` is an optional URI. It must start with `http://` or `https://`; a value such as `localhost:9000` fails when the configuration is loaded.
88
+ - `client` is the optional `CommonClientConfig` section, with `extraHeaders` (a list of `name` and `value` entries), `apiCallTimeout`, `apiCallAttemptTimeout` and `defaultProfileName`.
89
+
90
+ The following table is generated from the descriptor. The HTTP clients have their own keys, listed on the [HTTP Clients](http.md) page:
28
91
 
29
92
 
30
93
  ## Configuration Details
@@ -32,18 +95,18 @@ specific configuration which is described [on the page about the HTTP engines](h
32
95
 
33
96
  |FieldName|Format |Description|Sources|
34
97
  |--- |--- |--- |--- |
35
- | |[all-of](#field-descriptions)| | |
98
+ | |all-of| | |
36
99
 
37
- ### Field Descriptions
100
+ #### Field Descriptions
38
101
 
39
102
  |FieldName |Format |Description |Sources|
40
103
  |--- |--- |--- |--- |
41
104
  |region |primitive |a text property, AWS region to connect to | |
42
- | |[any-one-of](#field-descriptions-1)| | |
105
+ | |any-one-of| | |
43
106
  |endpointOverride |primitive |a text property, Overrides the AWS service endpoint (must include http:// or https:// scheme)| |
44
- |[client](#client)|[all-of](#client) |Common settings for AWS service clients | |
107
+ |client|all-of |Common settings for AWS service clients | |
45
108
 
46
- ### Field Descriptions
109
+ #### Field Descriptions
47
110
 
48
111
  |FieldName|Format |Description |Sources|
49
112
  |--- |--- |--- |--- |
@@ -54,7 +117,7 @@ specific configuration which is described [on the page about the HTTP engines](h
54
117
 
55
118
  |FieldName |Format |Description |Sources|
56
119
  |--- |--- |--- |--- |
57
- |[extraHeaders](#extraheaders)|[list](#extraheaders)|Extra headers to be sent with each request | |
120
+ |extraHeaders|list|Extra headers to be sent with each request | |
58
121
  |apiCallTimeout |primitive |a duration property, Amount of time to allow the client to complete the execution of an API call| |
59
122
  |apiCallAttemptTimeout |primitive |a duration property, Amount of time to wait for the HTTP request to complete before giving up | |
60
123
  |defaultProfileName |primitive |a text property, Default profile name | |
@@ -67,15 +130,22 @@ specific configuration which is described [on the page about the HTTP engines](h
67
130
  |value |list |a text property, Header value| |
68
131
 
69
132
 
70
- ## Service layer
71
- Each AWS service's generated client has it own layer that depends on `AwsConfig`. It is possible to reuse the same `AwsConfig` layer
72
- for multiple AWS service clients, sharing a common configuration. Usually the service client does not require any additional configuration,
73
- in this case the `Ec2.live` layer can be used, for example:
133
+ The descriptor's building blocks, `region`, `httpUri`, `credentialsProvider`, `awsCredentials` and `commonClientConfig`, are public too, so you can reuse them in your own configuration types.
134
+
135
+ ## Sharing One AwsConfig
136
+
137
+ Every generated service layer depends on `AwsConfig`, so one `AwsConfig` layer serves several services, which then share the HTTP client and the common settings:
74
138
 
75
139
  ```scala
76
- program.provide(
77
- awsConfig,
78
- Ec2.live,
79
- ElasticBeanstalk.live
80
- )
140
+ import zio._
141
+ import zio.aws.core.config.AwsConfig
142
+ import zio.aws.ec2.Ec2
143
+ import zio.aws.elasticbeanstalk.ElasticBeanstalk
144
+ import zio.aws.netty.NettyHttpClient
145
+
146
+ val awsConfig: ZLayer[Any, Throwable, AwsConfig] =
147
+ NettyHttpClient.default >>> AwsConfig.default
148
+
149
+ val services: ZLayer[Any, Throwable, Ec2 & ElasticBeanstalk] =
150
+ awsConfig >>> (Ec2.live ++ ElasticBeanstalk.live)
81
151
  ```
package/examples.md CHANGED
@@ -3,9 +3,7 @@ id: examples
3
3
  title: Examples
4
4
  ---
5
5
 
6
- # Examples
7
-
8
- The following example uses the ElasticBeanstalk and EC2 APIs to print some info.
6
+ The following program combines two services, Elastic Beanstalk and EC2. It finds an Elastic Beanstalk application named `my-service`, streams its environments, reads the EC2 instance IDs behind each environment, and prints the type and launch time of each instance. It builds the layers by hand with `>>>` and `++`, and reports the failures of each environment without stopping the others:
9
7
 
10
8
 
11
9
  ```scala
@@ -0,0 +1,87 @@
1
+ ---
2
+ id: getting-started
3
+ title: "Getting Started"
4
+ description: "Add ZIO AWS to a project, call one AWS operation and run the program, end to end."
5
+ keywords:
6
+ - "ZIO AWS"
7
+ - "Getting started"
8
+ - "AwsConfig"
9
+ - "Ec2"
10
+ ---
11
+
12
+ This page takes you from an empty sbt project to a running program that calls AWS. It assumes you know basic Scala and ZIO effects and layers, and that AWS credentials are set up on your machine, for example with `aws configure`.
13
+
14
+ ## Add the Dependencies
15
+
16
+ A ZIO AWS program needs the core module, one HTTP client module and one module per AWS service. We use EC2 and the Netty client:
17
+
18
+ ```scala
19
+ libraryDependencies ++= Seq(
20
+ "dev.zio" %% "zio-aws-core" % "7.59.5.2",
21
+ "dev.zio" %% "zio-aws-netty" % "7.59.5.2",
22
+ "dev.zio" %% "zio-aws-ec2" % "7.59.5.2"
23
+ )
24
+ ```
25
+
26
+ The [Installation](index.md#installation) section explains each module, and [Artifacts](artifacts.md) lists every AWS service module.
27
+
28
+ ## Call One Operation
29
+
30
+ Each AWS service is a ZIO service, and each operation is a function on its companion object. We call EC2's `DescribeRegions`, which lists the regions EC2 offers:
31
+
32
+ ```scala
33
+ import zio._
34
+ import zio.aws.core.AwsError
35
+ import zio.aws.core.config.AwsConfig
36
+ import zio.aws.ec2.Ec2
37
+ import zio.aws.ec2.model.DescribeRegionsRequest
38
+ import zio.aws.netty.NettyHttpClient
39
+
40
+ val listRegions: ZIO[Ec2, AwsError, Unit] =
41
+ for {
42
+ response <- Ec2.describeRegions(DescribeRegionsRequest())
43
+ regions <- response.getRegions
44
+ _ <- ZIO.foreachDiscard(regions) { region =>
45
+ region.getRegionName.flatMap(name => Console.printLine(name).orDie)
46
+ }
47
+ } yield ()
48
+ ```
49
+
50
+ The request is a case class whose fields all have default values, so `DescribeRegionsRequest()` is a valid request. The response is a read-only wrapper around the Java SDK response. Its `getRegions` getter is a ZIO effect, because the field is optional in the AWS model: when the field is absent, the effect fails with an `AwsError` instead of returning `null`.
51
+
52
+ Building `listRegions` makes no network call. The call happens when the effect runs.
53
+
54
+ ## Provide the Layers and Run
55
+
56
+ `listRegions` needs an `Ec2` service. Three layers build it: the HTTP client, the common AWS configuration and the EC2 client itself. We provide all three and run the program as a `ZIOAppDefault`:
57
+
58
+ ```scala
59
+ object ListRegionsApp extends ZIOAppDefault {
60
+ def run =
61
+ listRegions.provide(
62
+ NettyHttpClient.default,
63
+ AwsConfig.default,
64
+ Ec2.live
65
+ )
66
+ }
67
+ ```
68
+
69
+ The `ZIO#provide` call builds the layers in dependency order: `NettyHttpClient.default` provides the `HttpClient`, `AwsConfig.default` uses it to provide `AwsConfig`, and `Ec2.live` uses that to build the EC2 client. The client is closed when the program ends.
70
+
71
+ When you run the program, it prints one line per EC2 region, such as `eu-west-1`.
72
+
73
+ ## Where the Region and Credentials Come From
74
+
75
+ `AwsConfig.default` sets neither the region nor the credentials, so the AWS SDK looks them up itself. It reads the `AWS_REGION` environment variable, the `aws.region` system property or the profile in `~/.aws/config`, and the same sources for credentials. If the program fails with an error about a missing region, set `AWS_REGION`:
76
+
77
+ ```bash
78
+ export AWS_REGION=eu-west-1
79
+ ```
80
+
81
+ To set the values in code or load them from a configuration file, see [Configure Credentials and Region](guide-configure-credentials-and-region.md).
82
+
83
+ ## Next Steps
84
+
85
+ - Build a complete program step by step in the [tutorial](tutorial-first-program.md).
86
+ - Learn what a generated service contains in the [Wrappers](wrappers.md) reference.
87
+ - Handle failures with [Handle AWS Errors](guide-handle-errors.md).
@@ -26,9 +26,11 @@ ZIO AWS lets you attach one aspect to a service layer. Every operation of that s
26
26
  Add the core module, the service module and an HTTP client:
27
27
 
28
28
  ```scala
29
- libraryDependencies += "dev.zio" %% "zio-aws-core" % "<version>"
30
- libraryDependencies += "dev.zio" %% "zio-aws-ec2" % "<version>"
31
- libraryDependencies += "dev.zio" %% "zio-aws-netty" % "<version>"
29
+ libraryDependencies ++= Seq(
30
+ "dev.zio" %% "zio-aws-core" % "7.59.5.2",
31
+ "dev.zio" %% "zio-aws-ec2" % "7.59.5.2",
32
+ "dev.zio" %% "zio-aws-netty" % "7.59.5.2"
33
+ )
32
34
  ```
33
35
 
34
36
  The examples use these imports:
@@ -103,7 +105,7 @@ Each service layer takes its own aspect, so the EC2 layer and a second service l
103
105
 
104
106
  ## Retries
105
107
 
106
- The library does not ship a retry aspect. Retries are a good use for an aspect, because they apply to every call of a service in the same way. Write one with the ZIO `retry` operator, using a `Schedule` that matches the error type you want to retry. The [AWS Errors](./aws-error.md) reference lists the error cases you can match.
108
+ Retries are also a good use for an aspect, because they apply to every call of a service in the same way. The [Add Retries](guide-add-retries.md) guide builds a retry aspect step by step.
107
109
 
108
110
  ## Going Further
109
111
 
@@ -0,0 +1,132 @@
1
+ ---
2
+ id: guide-add-retries
3
+ title: "Add Retries"
4
+ description: "Retry throttled and transient AWS failures for every call of a service, with a ZIO Schedule inside an aspect."
5
+ keywords:
6
+ - "ZIO AWS"
7
+ - "Retries"
8
+ - "AwsCallAspect"
9
+ - "Schedule"
10
+ - "Throttling"
11
+ ---
12
+
13
+ In this guide, we will retry failed AWS calls with a ZIO `Schedule`, for every operation of a service at once. By the end, you will have an aspect that retries throttling and network errors with exponential backoff, and leaves other errors alone.
14
+
15
+ We'll take an incremental approach: first we retry every failure, then we retry only the failures that are worth retrying, and finally we attach the aspect to a service layer.
16
+
17
+ ## The Problem
18
+
19
+ AWS services reject requests when a caller sends too many of them, and networks drop connections. Both failures go away on their own, so the right reaction is to wait and send the request again.
20
+
21
+ The AWS SDK has its own retry logic, but ZIO AWS turns it off when you configure clients with `AwsConfig.configured()` and a `client` section, so that retries can be controlled in ZIO instead. Adding `ZIO#retry` to each call is repetitive, and a forgotten call site has no retries at all.
22
+
23
+ An aspect solves both problems. ZIO AWS runs every AWS request of a service through the aspect attached to the service layer, so one aspect gives every operation the same retry policy.
24
+
25
+ ## Prerequisites
26
+
27
+ Add the core module, a service module and an HTTP client:
28
+
29
+ ```scala
30
+ libraryDependencies ++= Seq(
31
+ "dev.zio" %% "zio-aws-core" % "7.59.5.2",
32
+ "dev.zio" %% "zio-aws-ec2" % "7.59.5.2",
33
+ "dev.zio" %% "zio-aws-netty" % "7.59.5.2"
34
+ )
35
+ ```
36
+
37
+ The examples use these imports:
38
+
39
+ ```scala
40
+ import zio._
41
+ import zio.aws.core.{AwsError, GenericAwsError}
42
+ import zio.aws.core.aspects._
43
+ import zio.aws.core.config.AwsConfig
44
+ import zio.aws.ec2.Ec2
45
+ import software.amazon.awssdk.awscore.exception.AwsServiceException
46
+ import software.amazon.awssdk.core.exception.SdkClientException
47
+ import java.io.IOException
48
+ ```
49
+
50
+ ## Retrying Every Failure
51
+
52
+ An aspect is an `AwsCallAspect[R]`. Its `apply` method receives one AWS request as a ZIO effect and returns a new effect. To retry, we call `ZIO#retry` on that effect with the schedule we are given:
53
+
54
+ ```scala
55
+ def retrying(policy: Schedule[Any, AwsError, Any]): AwsCallAspect[Any] =
56
+ new AwsCallAspect[Any] {
57
+ override final def apply[R, E >: AwsError <: AwsError, A <: Described[_]](
58
+ f: ZIO[R, E, A]
59
+ )(implicit trace: Trace): ZIO[R, E, A] =
60
+ f.retry(policy)
61
+ }
62
+ ```
63
+
64
+ The error type bounds `E >: AwsError <: AwsError` tell the compiler that the error is exactly `AwsError`, so a `Schedule` that reads `AwsError` values can drive the retries. The `Described[_]` bound limits the aspect to generated AWS calls.
65
+
66
+ A simple policy retries three times with exponential backoff:
67
+
68
+ ```scala
69
+ val threeTimes: Schedule[Any, AwsError, Any] =
70
+ Schedule.exponential(100.millis) && Schedule.recurs(3)
71
+ ```
72
+
73
+ `Schedule.exponential(100.millis)` waits 100 ms, then 200 ms, then 400 ms, and `Schedule.recurs(3)` stops after three retries. The `&&` operator continues only while both schedules continue.
74
+
75
+ ## Retrying Only Transient Failures
76
+
77
+ Retrying every failure is wasteful: a request that AWS rejects for missing permissions fails the same way every time. A `GenericAwsError` carries the exception the AWS SDK threw, and its type and fields tell whether the failure is worth retrying:
78
+
79
+ ```scala
80
+ def isTransient(error: AwsError): Boolean =
81
+ error match {
82
+ case GenericAwsError(e: AwsServiceException) => e.isThrottlingException || e.statusCode() >= 500
83
+ case GenericAwsError(e: SdkClientException) => e.getCause.isInstanceOf[IOException]
84
+ case GenericAwsError(_: IOException) => true
85
+ case _ => false
86
+ }
87
+ ```
88
+
89
+ The code above:
90
+
91
+ - `AwsServiceException#isThrottlingException` is `true` when the service rejected the request because of its rate limit, for example with the `Throttling` or `RequestLimitExceeded` error code.
92
+ - `AwsServiceException#statusCode` is the HTTP status of the response; a 5xx status means the service itself failed.
93
+ - An `SdkClientException` caused by an `IOException`, or a bare `IOException` from the HTTP client, means the request did not reach the service or the connection broke.
94
+ - `FieldIsNone`, the other case of `AwsError`, is a missing field in a successful response, so it is never retried.
95
+
96
+ We add the check to the policy with `Schedule.recurWhile`, and add jitter so that many fibers do not retry at the same moment:
97
+
98
+ ```scala
99
+ val transientOnly: Schedule[Any, AwsError, Any] =
100
+ (Schedule.exponential(100.millis).jittered && Schedule.recurs(5)) &&
101
+ Schedule.recurWhile[AwsError](isTransient)
102
+ ```
103
+
104
+ The policy now retries up to five times, but stops at the first failure that is not transient, and the effect fails with that error.
105
+
106
+ ## Attaching the Aspect to a Service
107
+
108
+ The `@@@` operator, from `zio.aws.core.aspects._`, attaches the aspect to a service layer:
109
+
110
+ ```scala
111
+ val ec2WithRetries: ZLayer[AwsConfig, Throwable, Ec2] =
112
+ Ec2.live @@@ retrying(transientOnly)
113
+ ```
114
+
115
+ Every operation of the `Ec2` service built by this layer now retries transient failures. For most paginated operations, including those with a `NextToken` field such as `Ec2.describeInstances`, each page request is retried on its own, so a failure on the tenth page does not fetch the first nine again. Operations that use the AWS Java SDK's paginator run the aspect once, when the stream starts.
116
+
117
+ To log the calls as well, combine the retry aspect with another one using `>>>`:
118
+
119
+ ```scala
120
+ val ec2Observed: ZLayer[AwsConfig, Throwable, Ec2] =
121
+ Ec2.live @@@ (retrying(transientOnly) >>> callLogging)
122
+ ```
123
+
124
+ :::note
125
+ `callLogging` logs successful calls only. With the retry aspect first, it logs one line for the call that finally succeeded, including the time spent on retries.
126
+ :::
127
+
128
+ ## Going Further
129
+
130
+ - The [Add Aspects for Logging and Metrics](guide-add-aspects.md) guide shows the built-in aspects and how to write others.
131
+ - The [Aspects](aspects.md) reference describes `AwsCallAspect` and the `@@@` operator.
132
+ - The [Handle AWS Errors](guide-handle-errors.md) guide shows how to read the AWS error code of a failure.