Soto icon

Soto

Waiting

With the release of Soto 5.7 we introduce a new feature: waiters. A waiter is a client side abstraction that polls an AWS resource until a desired state is reached. This is a common task with services that create resources asynchronously. Writing the code to continually poll a resource for a state can be error prone. Writing loops in asynchronous code can be particularly difficult. Waiters are provided to take this responsibility away from the client. Below is an example of creating a DynamoDB table, waiting for it to become active and then putting an item in it.

let input = DynamoDB.CreateTableInput(
    attributeDefinitions: [.init(attributeName: "pk", attributeType: .s)],
    keySchema: [.init(attributeName: "pk", keyType: .hash)],
    provisionedThroughput: .init(readCapacityUnits: 1, writeCapacityUnits: 1),
    tableName: "test-table"
)
let futureResult = dynamoDB.createTable(input)
    .flatMap { _ -> EventLoopFuture<Void> in
        dynamoDB.waitUntilTableExists(.init(tableName: "test-table"))
    }
    .flatMap { _ -> EventLoopFuture<DynamoDB.PutItemOutput> in
        dynamoDB.putItem(.init(item: ["pk": .s("test")], tableName: "test-table"))
    }

Most of the code in this sample is setting up the DynamoDB table. The code to wait for for the table to be active has been reduced to the single line dynamoDB.waitUntilTableExists(...).

AWS provide models for waiters for many situations where you might be waiting on a resource state. Examples include waiting for an EC2 instance to be running EC2.waitUntilInstanceRunning, a RDS database to be available RDS.waitUntilDBInstanceAvailable, an IAM Role to exist IAM.waitUntilRoleExists or for a S3 Object to exist S3.waitUntilObjectExists.