簡體   English   中英

Typescript 模塊在通過 cdk 部署時未在 lambda function 中導入

[英]Typescript modules are not been imported in lambda function while its deploying through cdk

我使用 CDK 在 AWS 中開發我的無服務器應用程序。 When I try to deploy a lambda function post after TS file compilation, its not importing TS modules in lambda function as JS modules.

因此,當我調用 lambda 時,我遇到了未找到模塊的錯誤。

我在部署堆棧之前遵循的步驟:

  1. tsc -> 編譯 TS 文件
  2. cdk合成器
  3. cdk 部署

ts配置:

{
  "compilerOptions": {
    "target": "ES2018",
    "module": "commonjs",
    "lib": [
      "es2018"
    ],
    "noEmit": false,
    "declaration": false,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": false,
    "inlineSourceMap": true,
    "inlineSources": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "typeRoots": [
      "./node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules",
    "cdk.out"
  ]
}

代碼:

import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'aws-lambda';
import { DynamoDBClient, GetItemCommand, GetItemCommandInput } from "@aws-sdk/client-dynamodb";
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";

export async function handler(event: APIGatewayProxyEventV2,): Promise<APIGatewayProxyResultV2> {
console.log('event', event);

let userLat = event.pathParameters?.lat;
let userLng = event.pathParameters?.lng;    
let tableName = process.env.TABLE_NAME;
let results;

const dbClient: DynamoDBClient = new DynamoDBClient({ region: "ap-south-1" });
let params: GetItemCommandInput;

if (tableName) {
     params = {
        TableName: tableName,
        Key: marshall({
          "lat": userLat, 
          "lng": userLng
        }),
      };
}

const run = async function () {
    try {
        const resp = await dbClient.send(new GetItemCommand(params));
        results = unmarshall(resp.Item || {});
    } catch(err) {
        results = err;
    }
};

run();

return {
    body: JSON.stringify(
        results
    ),
    statusCode: 200
};     }

細節:

  • 節點:v14.20.0
  • NPM:6.14.17
  • CDK:2.40.0(構建 56ba2ab)
  • Ts:版本 4.8.2

錯誤:

{ "errorType": "Runtime.ImportModuleError", "errorMessage": "錯誤:找不到模塊 '@aws-sdk/client-dynamodb'\n需要堆棧:\n- /var/task/fetchpartner.js\n- / var/runtime/UserFunction.js\n- /var/runtime/Runtime.js\n- /var/runtime/index.js", "trace": [ "Runtime.ImportModuleError: Error: 找不到模塊'@aws- sdk/client-dynamodb'", "需要堆棧:", "- /var/task/fetchpartner.js", "- /var/runtime/UserFunction.js", "- /var/runtime/Runtime.js", “- /var/runtime/index.js”、“在 _loadUserApp (/var/runtime/UserFunction.js:221:13)”、“在 Object.module.exports.load (/var/runtime/UserFunction.js: 279:17)”, “在 Object. (/var/runtime/index.js:43:34)”, “在 Module._compile (internal/modules/cjs/loader.js:1085:14)”, “在Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)", "在 Module.load (internal/modules/cjs/loader.js:950:32)", "在 Z86408593C34AF77FDD16Z0DF932F8B52 .Module._load(內部 /modules/cjs/loader.js:790:12)", "在 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)", "在 internal/main/run_main_module.js:17 :47"]}

您可以在包含 lambda 代碼的目錄中運行npm init並在那里安裝您想要的模塊。 另一種方法是使用 webpack 之類的東西來編譯並將您的代碼和模塊組合到一個文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM