Spance
Spance

Reputation: 607

AWS4 signature in Node.js

I'm trying to make a call to a private Amazon API with Javascript using the aws4 package, but I can't get it to work. I'm able to do the call successfully with Postman, but I'm trying to get it to work with code, and I'm failing.

Here is the postman screenshot:

enter image description here

And here is the code that is trying to replicate this:

request(aws4.sign({
    service: 'execute-api',
    region: 'us-east-1',
    method: 'POST',
    url: 'https://test.amazonAPI.com/test/doThing',
    body: load
  },
  {
    accessKeyId: tempCreds.Credentials.AccessKeyId,
    secretAccessKey: tempCreds.Credentials.SecretAccessKey,
    sessionToken: tempCreds.Credentials.SessionToken
  }))

And the error I'm currently getting:

Error: getaddrinfo ENOTFOUND execute-api.us-east-1.amazonaws.com execute-api.us-east-1.amazonaws.com:443
    at errnoException (dns.js:53:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:95:26)

Upvotes: 4

Views: 10303

Answers (1)

Hieu Nguyen
Hieu Nguyen

Reputation: 511

I think you're missing hostname in requestOptions. Correct:

request(aws4.sign({
    hostname: 'test.amazonAPI.com',
    service: 'execute-api',
    region: 'us-east-1',
    method: 'POST',
    url: 'https://test.amazonAPI.com/test/doThing', // this field is not recommended in the document.
    body: load
  },
  {
    accessKeyId: tempCreds.Credentials.AccessKeyId,
    secretAccessKey: tempCreds.Credentials.SecretAccessKey,
    sessionToken: tempCreds.Credentials.SessionToken
  }))

Reference: https://github.com/mhart/aws4

Upvotes: 4

Related Questions