How do I set a timeout for AWS V3 Dynamo Clients

Viewed 2344

I have started using the new AWS Version 3 sdk for some of my services. Unfortunately it is not always clear how to use some features in the modular version 3 code that are available in the Version 2 sdk.

To set timeouts for the non-modular sdk, you can do the following:

AWS.config.update({
     httpOptions: {
        connectTimeout: 10000,
        timeout: 10000
    }
 });

However, when I want to use the Version 3 sdk and use the Dynamo client, I am explicitly trying not to use the global AWS object. As far as I can tell the configuration input to DynamoDBClient does not accept httpOptions, which is where a timeout would normally get set.

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
const REGION = process.env.AWS_REGION;
const v3DynamoClient: DynamoDBClient = new DynamoDBClient({ region: REGION });

How do I set a timeout for the DynamoDBClient in the AWS V3 sdk?

2 Answers

This link about upgrading seems to have an example, in the section about httpOptions. There is an obvious typo in the example there. Looking at the new AWS v3 code directly, it is apparent that the agent NEED NOT BE SPECIFIED (there are defaults if the agent is not passed) - so this example is sufficient.

// Use default Https agent, but override the socket timeout
const requestHandler = new NodeHttpHandler({
  connectionTimeout: 30000,
  socketTimeout: 30000,
});

const options = {
  region: AWS_REGION,
  maxAttempts: 2,
  requestHandler, // Use handler with alternate settings for timeouts
};
export const dynamodbClient = new DynamoDBClient(options);

Here's an example of setting TLS v1.2 options that should help:

const https = require("https");
const {NodeHttpHandler} = require("@aws-sdk/node-http-handler");
const {DynamoDBClient} = require("@aws-sdk/client-dynamodb");
const client = new DynamoDBClient({
  region: "us-west-2",
  requestHandler: new NodeHttpHandler({
    httpsAgent: new https.Agent({secureProtocol: 'TLSv1_2_method'})
  })
});

You should be able to set connectionTimeout or socketTimeout in the options to NodeHttpHandler.

Also, worth reading the SDK v3 Developer Guide.

Related