5

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?

ORcoder
  • 252
  • 2
  • 11

2 Answers2

5

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);
Marvin
  • 2,537
  • 24
  • 35
  • 1
    This doesn't work for me, but I'm using the DynamoDBClient class instead of the DynamoDB class. I'm calling the DynamoDBClient.send method with a PutItemCommand argument. I'm running the function in a Lambda with a timeout of 3 minutes but the Lambda times out before the DynamoDB connection attempt does. `new NodeHttpHandler({ connectionTimeout: 30000, socketTimeout: 30000 })`. – ngAlchemist Jan 12 '22 at 17:30
  • @ngAlchemist - fixed the typo you pointed out, it should be `DynamoDbClient`. – Marvin Jul 25 '22 at 13:12
3

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.

jarmod
  • 71,565
  • 16
  • 115
  • 122
  • 1
    One thing to add is that `timeout` is now `socketTimeout` and `connectTimeout` is `connectionTimeout`. You can read more [here](https://github.com/aws/aws-sdk-js-v3/issues/2028) – Philiiiiiipp Apr 09 '21 at 14:02