NodeJS TypeError:command.resolveMiddleware不是函数AWS Lambda Javascript V3

piztneat  于 2023-04-05  发布在  Node.js
关注(0)|答案(2)|浏览(90)

我尝试使用"@aws-sdk/client-secrets-manager": "^3.299.0"以编程方式将密钥上传到AWS Secret Manager示例,但我不断收到错误。我已按照此处的文档操作:https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-secrets-manager/classes/getsecretvaluecommand.html
我使用Typescript版本5.0.2和Node 18.x作为运行时,下面是我的tsconfig.json:

{
  "compilerOptions": {
    "module": "NodeNext",
    "target": "ESNext",
    "noImplicitAny": true,
    "preserveConstEnums": true,
    "outDir": "./dist",
    "sourceMap": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

这是失败的代码:

const client = new SecretsManagerClient({ region: 'my-region' });

async function getExistingSecret(connectionDetails: ConnectionDetails): Promise<string> {
  console.log('checking if secret exists');
  const command = getCommand(connectionDetails);
  console.log(`GetSecretValueCommand: ${JSON.stringify(command)}`);
  return client
    .send(command) // <----- FAILS HERE
    .catch(() => {
      console.log(`secret does not exist`);
      return null;
    })
    .then((response: GetSecretValueCommandOutput) => {
      console.log(`secret exists arn: ${response.ARN}`);
      return response.ARN;
    });
}

const getCommand = (connectionDetails: ConnectionDetails): GetSecretValueCommand => {
  const body = getCommandRequest(connectionDetails);
  console.log(`GetSecretValueRequest: ${JSON.stringify(body)}`);
  return { input: body } as GetSecretValueCommand;
};

const getCommandRequest = (connectionDetails: ConnectionDetails): GetSecretValueCommandInput => {
  return { SecretId: secretName(connectionDetails) } as GetSecretValueCommandInput;
};

我运行这段代码时得到的错误是这样的:

TypeError: command.resolveMiddleware is not a function
    at SecretsManagerClient.send (/var/task/node_modules/@aws-sdk/smithy-client/dist-cjs/client.js:13:33)
    at getExistingSecret (/var/task/aws/secrets-client.js:35:10)
    at addSecret (/var/task/aws/secrets-client.js:8:12)
    at Runtime.handler (/var/task/index.js:9:29)
    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1085:29)

我已经检查了这些地方沿着上面提到的官方文档。
https://github.com/aws/aws-sdk-js-v3/issues/4456
command.resolveMiddleware is not a function for AWS SDK when creating AMI from EC2 instanceID, wondering why?
AWS Timestream - SDK V3 Nodejs, TimestreamWriteClient.send() - TypeError: command.resolveMiddleware is not a function. How to solve this?
提前感谢您的帮助!
亚历克斯
我尝试了以下文档,并期望代码不会返回此错误。

toe95027

toe950271#

这是我如何使用它与Typescript,但它也将工作与Javascript
函数定义

import { SecretsManagerClient, GetSecretValueCommand, GetSecretValueCommandOutput } from "@aws-sdk/client-secrets-manager";

async function ImportSecrets(secretId?: string): Promise<GetSecretValueCommandOutput> {

    const secretManagerClient = new SecretsManagerClient({ region: region });
    const secretManagerCommand = new GetSecretValueCommand({ SecretId: secretId });

    const secretManagerResponse = await secretManagerClient.send(secretManagerCommand);
    return secretManagerResponse  
}

函数调用

let secrets = await ImportSecrets('my-secret-name')

注意:-我将秘密ID名称传递给函数。当然,在我的情况下,我手动创建秘密。

mznpcxlj

mznpcxlj2#

非常感谢你的投入;我绞尽脑汁地想弄明白这个问题。结果发现GetSecretValueCommand不应该被示例化为:
const command = { Input: someInput } as GetSecretValueCommand
相反,应该使用提供的构造函数:
const command = new GetSecretValueCommand(someInput)
只有这样,对象才被正确地示例化。
再次,非常感谢您的投入在这方面!
注意那些谁发现这个:在我看来,这个相同的“模式”在所有JavaScript V3客户端上都是统一的,所以即使你正在使用Kinesis,S3等,这也会有所帮助。
亚历克斯

相关问题