AWS DynamoDb scan makes two HTTP requests to server provoking an error, why

0

I've a typescript react application who's using dynamo db services for storing data. I've javascript middleware that manages the db access operations. I developed locally the scan and put and delete operations with no errors. Once I setup the production environment, I have an error. If I execute the code locally with production environment I have the same error.

I noticed that scan operation fails. Actually it fails in a weird way, because I saw that request to aws server is done twice.

To make my debug life simple I moved for debugging all db access within my react onClick method, in order to isolate the operation from lifecycle of the application.

I noticed that:

  • the onClick fetch operation is indeed called once (as expected)
  • the DynamoDb scan operation is indeed called once (as expected)
  • the calllback of the scann is executed twice (not as expected)
    • the first time data is indeed fetched and I can log it (perfect)
    • the second call is executed with an error

aws config code

const awsConfig = {
  accessKeyId: process.env.REACT_APP_AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.REACT_APP_AWS_SECRET_ACCESS_KEY,
  region: process.env.REACT_APP_AWS_REGION,
  endpoint: process.env.REACT_APP_AWS_ENDPOINT
}

new AWS.Endpoint(awsConfig.endpoint || '')

AWS.config.update({
  region: awsConfig.region,
  //endpoint: awsConfig.endpoint || '',
  credentials: new AWS.Credentials(awsConfig.accessKeyId || '', awsConfig.secretAccessKey || ''),
  sslEnabled: false,
  maxRetries: 5
})

const dynamoDbClient = new AWS.DynamoDB.DocumentClient()

// note to this part of the code, Typescript gives me an error if I leave the endpoint in the AWS.config.update so I set it differently new AWS.Endpoint(awsConfig.endpoint || '')

Argument of type '{ region: string | undefined; endpoint: string; credentials: Credentials; sslEnabled: false; maxRetries: number; }' is not assignable to parameter of type 'ConfigurationOptions & ConfigurationServicePlaceholders & APIVersions'.
  Object literal may only specify known properties, and 'endpoint' does not exist in type 'ConfigurationOptions & ConfigurationServicePlaceholders & APIVersions'.ts(2345)

or

    const awsConfig = {
      accessKeyId: process.env.REACT_APP_AWS_ACCESS_KEY_ID,
      secretAccessKey: process.env.REACT_APP_AWS_SECRET_ACCESS_KEY,
      region: process.env.REACT_APP_AWS_REGION,
      endpoint: process.env.REACT_APP_AWS_ENDPOINT
    }


    const dynamoDb = new AWS.DynamoDB({
      region: awsConfig.region,
      endpoint: awsConfig.endpoint,
      credentials: new AWS.Credentials(awsConfig.accessKeyId || '', awsConfig.secretAccessKey || ''),
      sslEnabled: false,
      maxRetries: 5
    })
    const dynamoDbClient = new AWS.DynamoDB.DocumentClient({
      service: dynamoDb
    })

and this is

aws database access code

const searchParams = { TableName: 'table' }

let dataItems: DateFields[] = []

await dynamoDbClient
  .scan(searchParams, (error: any, data: any) => {
    if (error) {
      console.error(`Error fetchCategories: `, JSON.stringify(error, null, 2))
      return
    }
    dataItems = data.Items
  })
  .promise()

//with the following error

Error fetchCategories:  {
  "message": "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.",
  "code": "InvalidSignatureException",
  "time": "2020-04-29T08:39:14.449Z",
  "requestId": "xxxxxxxxxxxxxxxxxxx",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 15.082420221534553
}

{"__type":"com.amazon.coral.service#InvalidSignatureException","message":"The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details."}

Any idea why this is happening? Thanks a lot

已提問 4 年前檢視次數 385 次
1 個回答
0

The issue lies in the aws access code and to fix it I updated the DynamoDB client like this:

  try {
    const data = await dynamoDbClient.scan(params).promise()
    return data.Items
  } catch(error) {
    logError(operation, error)
  }

This change in the code prevents:
-the callback to be executed twice;
-the server to answer with an error that probably reflects an AWS security mechanism blocking multiple identical and consequential server query.

已回答 4 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南