Soto icon

Soto

AWSClient

The AWSClient is the core of Soto. This is the object that manages your communication with AWS. It manages credential acquisition, takes your request, encodes it, signs it, sends it to AWS and then decodes the response for you. In most situations your application should only require one AWSClient. Create this at startup and use it throughout.

The init for creating an AWSClient looks like the following:

public init(
    credentialProvider credentialProviderFactory: CredentialProviderFactory = .default,
    retryPolicy retryPolicyFactory: RetryPolicyFactory = .default,
    middlewares: some AWSMiddlewareProtocol,
    httpClient: AWSHTTPClient,
    logger clientLogger: Logger = AWSClient.loggingDisabled
)

Details for each option are below.

Credential Provider

The credentialProvider defines how the client acquires its AWS credentials. Its default is to try four different methods:

  • environment variables
  • ECS container credentials
  • EC2 instance metadata
  • the shared credential file ~/.aws/credential

An alternative is to provide credentials in code. You can do this as follows

let client = AWSClient(
    credentialProvider: .static(
        accessKeyId: "MY_AWS_ACCESS_KEY_ID",
        secretAccessKey: "MY_AWS_SECRET_ACCESS_KEY"
    ),
    ...
)

You can find out more about CredentialProviders here.

Retry policy

The retryPolicy defines how the client reacts to a failed request. There are three retry policies supplied. .noRetry doesn't retry the request if it fails. The other two will retry if the response is a 5xx (server error) or a connection error. They differ in how long they wait before performing the retry. .exponential doubles the wait time after each retry and .jitter is the same as exponential except it adds a random element to the wait time. .jitter is the recommended method from AWS so it is the default.

Middleware

Middleware allows you to insert your own code just as a request has been constructed or a response has been received. You can use this to edit the request/response or just to view it. Soto Core supplies one middleware — AWSLoggingMiddleware — which outputs your request to the console once constructed and the response received from AWS.

HTTP Client

AWSHTTPClient is a protocol defining the requirements for HTTP client's that are used with Soto. HTTPClient from async-http-client is setup to conform to this protocol and the default for the httpClient is the global instance HTTPClient.shared. In the future we are looking to conform URLSession to this protocol as well.

Logger

The final function parameter is the Logger. This Logger is used for logging background processes the client might perform like AWS credential acquisition.

AWSClient Shutdown

The AWSClient requires you shut it down manually before it is deinitialized. The manual shutdown is required to ensure any internal processes are finished before the AWSClient is freed and Soto's http client is shutdown properly. You do this with AWSClient.shutdown().