Lambda函数与UpdateRestApiCommand一起使用,如何更新以在Node 18版本中运行

huangapple go评论59阅读模式
英文:

Lambda function with UpdateRestApiCommand, how to update to work in version node 18

问题

    new UpdateRestApiCommand(params)
      .on('success', function(response) {
        console.log("成功!");
        resolve(response.data);
      })
      .on('error', function(error, response) {
        console.log("错误!");
        reject(response.error);
      })
      .on('complete', function(response) {
        console.log("完成!");
      })
      .send()
  });
const https = require("https");
const env = process.env.ENV;
const resource = process.env.RESOURCE;
const restApiId = process.env.REST_API_ID;
const ce_base_url = process.env.CE_BASE_URL;
const { APIGatewayClient, UpdateRestApiCommand  } = require("@aws-sdk/client-api-gateway");
const stage = process.env.STAGE;
ERROR 调用错误
{
    "errorType": "TypeError",
    "errorMessage": "(中间值).on 不是一个函数",
    "stack": [
        "TypeError: (中间值).on 不是一个函数",
        "    at /var/task/index.js:64:8",
        "    at new Promise (<anonymous>)",
        "    at exports.handler (/var/task/index.js:36:25)",
        "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"
    ]
}
英文:

I updated a lambda function to node 18, but there are changes to do with my UpdateRestApiCommand.

Here's the original that worked in the older version:

    const request = apigateway.updateRestApi(params);
    request
      .on(&#39;success&#39;, function(response) {
        console.log(&quot;Success!&quot;);
        resolve(response.data);
      }).
      on(&#39;error&#39;, function(error, response) {
        console.log(&quot;Error!&quot;);
        reject(response.error);
      }).
      on(&#39;complete&#39;, function(response) {
        console.log(&quot;Done!&quot;);
      })
      .send()
  });

Here's my imports:

const https = require(&quot;https&quot;);
const env = process.env.ENV;
const resource = process.env.RESOURCE;
const restApiId = process.env.REST_API_ID;
const ce_base_url = process.env.CE_BASE_URL;
const { APIGatewayClient, UpdateRestApiCommand  } = require(&quot;@aws-sdk/client-api-gateway&quot;);
const stage = process.env.STAGE;

And now I've found I need to use UpdateRestApiCommand I think so I've got this:

    new  UpdateRestApiCommand(params)
      .on(&#39;success&#39;, function(response) {
        console.log(&quot;Success!&quot;);
        resolve(response.data);
      }).
      on(&#39;error&#39;, function(error, response) {
        console.log(&quot;Error!&quot;);
        reject(response.error);
      }).
      on(&#39;complete&#39;, function(response) {
        console.log(&quot;Done!&quot;);
      })
      .send()
  });

Here's the error I'm getting:

ERROR	Invoke Error 	
{
    &quot;errorType&quot;: &quot;TypeError&quot;,
    &quot;errorMessage&quot;: &quot;(intermediate value).on is not a function&quot;,
    &quot;stack&quot;: [
        &quot;TypeError: (intermediate value).on is not a function&quot;,
        &quot;    at /var/task/index.js:64:8&quot;,
        &quot;    at new Promise (&lt;anonymous&gt;)&quot;,
        &quot;    at exports.handler (/var/task/index.js:36:25)&quot;,
        &quot;    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)&quot;
    ]
}

答案1

得分: 0

The nodejs18.x runtime has the AWS JS SDK v3 pre-installed. Earlier runtime versions came with v2.

Here's the basic pattern to use the v3 SDK clients in a Lambda handler:

const client = new APIGatewayClient({
  region: process.env.AWS_REGION,
});

export async function handler(event, context) {
  // ...

  const cmd = new UpdateRestApiCommand(params);

  const response = await client.send(cmd);
}
英文:

The nodejs18.x runtime has the AWS JS SDK v3 pre-installed. Earlier runtime versions came with v2.

Here's the basic pattern to use the v3 SDK clients in a Lambda handler:

const client = new APIGatewayClient({
  region: process.env.AWS_REGION,
});

export async function handler(event, context) {
  // ...

  const cmd = new UpdateRestApiCommand(params);

  const response = await client.send(cmd);
}

huangapple
  • 本文由 发表于 2023年2月8日 23:04:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定