简体   繁体   中英

How to do AWS Signature Version 4 from a local machine?

I'm trying to test some Node.js code from my local machine for use in an AWS Lambda function. This involves signing a request with Signature Version 4.

I've signed in with my access key using AWS CLI but when I try to make a request using the following code I get this error at signer.addAuthorization . What step am I missing? It works fine from a Lambda function.

Code:

const AWS = require('aws-sdk');
const creds = new AWS.EnvironmentCredentials('AWS');
...
var signer = new AWS.Signers.V4(req, 'es');
signer.addAuthorization(creds, new Date());

Error:

TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be one of type string, TypedArray, or DataView. Received type undefined
    at new Hmac

According to the documentation of EnvironmentCredentials ,

By default, this class will look for the matching environment variables prefixed by a given envPrefix

Therefore you need to set AWS_ACCESS_KEY_ID , AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN environment variables before invoking your code.

In AWS Lambda environment, these environment variables are already set, that's why it is working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM