TypeError [ERR_INVALID_ARG_TYPE]:“ key”参数必须是字符串,TypedArray或DataView类型之一

时间:2019-04-19 17:12:30

标签: aws-lambda serverless

尝试使用AWS Lambda在Elasticsearch中建立索引。 堆栈跟踪

  

TypeError [ERR_INVALID_ARG_TYPE]:“ key”参数必须是以下其中一项   类型字符串,TypedArray或DataView       在新的Hmac上(internal / crypto / hash.js:84:11)       在Object.createHmac(crypto.js:122:10)       在Object.hmac(/home/projects/serverless-todo-app/.webpack/service/src/indexer/createIndex.js:698:30)       在Object.getSigningKey(/home/projects/serverless-todo-app/.webpack/service/src/indexer/createIndex.js:7109:8)       在V4.signature(/home/projects/serverless-app/.webpack/service/src/indexer/createIndex.js:12708:36)       在V4.authorization(/home/projects/serverless-app/.webpack/service/src/indexer/createIndex.js:12703:36)       在V4.addAuthorization(/home/projects/serverless-app/.webpack/service/src/indexer/createIndex.js:12645:12)       在ElasticsearchService.put(/home/projects/serverless-app/.webpack/service/src/indexer/createIndex.js:8150:12)       在处理中(/home/projects/serverless-app/.webpack/service/src/indexer/createIndex.js:8115:24)       在BbPromise(/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:567:30)       在AwsInvokeLocal.invokeLocalNodeJs(/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:521:12)       在AwsInvokeLocal.invokeLocal(/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:152:19)   从上一个事件:       在Object.invoke:local:invoke [作为钩子](/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:34:10)

const credentials = new AWS.EnvironmentCredentials('AWS');
let signer = new AWS.Signers.V4(this.request, 'es');
signer.addAuthorization(credentials, new Date());

尝试使用AWS Lambda在elastisearch中建立一些数据的索引。

1 个答案:

答案 0 :(得分:1)

您应该有一个端点并已启动您的请求。您可以像下面那样进行操作,还可以检查代码后粘贴的链接。

/* == Globals == */
var esDomain = {
    region: 'us-east-1',
    endpoint: 'my-domain-search-endpoint',
    index: 'myindex',
    doctype: 'mytype'
};
var endpoint = new AWS.Endpoint(esDomain.endpoint);
/*
 * The AWS credentials are picked up from the environment.
 * They belong to the IAM role assigned to the Lambda function.
 * Since the ES requests are signed using these credentials,
 * make sure to apply a policy that allows ES domain operations
 * to the role.
 */
var creds = new AWS.EnvironmentCredentials('AWS');

var req = new AWS.HttpRequest(endpoint);

    req.method = 'POST';
    req.path = path.join('/', esDomain.index, esDomain.doctype);
    req.region = esDomain.region;
    req.headers['presigned-expires'] = false;
    req.headers['Host'] = endpoint.host;
    req.body = doc;

    var signer = new AWS.Signers.V4(req , 'es');  // es: service code
    signer.addAuthorization(creds, new Date());

amazon-elasticsearch-lambda-samples中检查第45-55行 (对不起,由于声誉低下,我无法发表评论)