Try to use SES Services with Soto SDK and Vapor 4 to send email with my own route

46 Views Asked by At

I want to use SES Services with Vapor 4 to send an Email with my own route.

I follow step by step the recommendation in Soto Doc to use Vapor 4.

But I got this error : AsyncHTTPClient/HTTPClient.swift:591: Precondition failed: Provided EventLoop must be part of clients EventLoopGroup.

Some one can give me some explanations or explain how can I implement Soto SES with Vapor.

Thanks :)

I follow the Soto SDK documentation to implement SES Services but it's not working

1

There are 1 best solutions below

1
JoannisO On

This issue lacks some code samples of how you set it up, but I'm assuming the following:

let awsClient = AWSClient(
    credentialProvider: .static(accessKeyId: myAccessKeyId, secretAccessKey: mySecretAccessKey),
    httpClientProvider: .createNew
)

You can access application.http.client.shared to use Vapor's instance of AsyncHTTPClient instead as such:

let awsClient = AWSClient(
    credentialProvider: .static(accessKeyId: myAccessKeyId, secretAccessKey: mySecretAccessKey),
    httpClientProvider: .shared(application.http.client.shared)
)

Vapor's req.eventLoop is part of an EventLoopGroup (multiple threads with EventLoops), and gets one of those threads assigned to it.

If you then call aws.ses.sendEmail(sendEmailRequest, on: req.eventLoop), that on: parameter must be an EventLoop that is part of a group used for the AsyncHTTPClient. If you use .createNew to create a new HTTPClient, it'll create a separate EventLoopGroup from Vapor's. So your request will not use a valid thread for AsyncHTTPClient. While you can still use the AsyncHTTPClient's EventLoop for the request, it's definitely recommended to re-use the AsyncHTTPClient Vapor has here.