目录图像 我试图部署一个包含 lambda 函数的 lambda 堆栈。该函数将运行的代码位于源文件夹下的 services 文件夹中。即使我的文件目录在 lambda 函数中似乎是正确的,我也会遇到找不到资产错误。有谁知道我错过了什么?
import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import {
Code,
Function as LambdaFunction,
Runtime,
} from "aws-cdk-lib/aws-lambda";
import { join } from "path";
export class LambdaStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
new LambdaFunction(this, "getBooksLambda", {
runtime: Runtime.NODEJS_18_X,
handler: "getBooks.main",
code: Code.fromAsset(join(__dirname, "..", "..", "services", "getBooks")),
});
}
}
exports.main = async function(event, context) {
return {
statusCode: 200,
body: JSON.stringify({
message: "Hello from Lambda"
})
}
}
Error: Cannot find asset at C:\Users\User\Documents\Bookeroo\bookeroo-cdk\src\services\getBooks
at new AssetStaging (C:\Users\User\Documents\Bookeroo\bookeroo-cdk\node_modules\aws-cdk-lib\core\lib\asset-staging.js:1:2119)
at new Asset (C:\Users\User\Documents\Bookeroo\bookeroo-cdk\node_modules\aws-cdk-lib\aws-s3-assets\lib\asset.js:1:1141)
at AssetCode.bind (C:\Users\User\Documents\Bookeroo\bookeroo-cdk\node_modules\aws-cdk-lib\aws-lambda\lib\code.js:5:3487)
at new Function (C:\Users\User\Documents\Bookeroo\bookeroo-cdk\node_modules\aws-cdk-lib\aws-lambda\lib\function.js:1:10003)
at new LambdaStack (C:\Users\User\Documents\Bookeroo\bookeroo-cdk\src\infra\stacks\LambdaStack.ts:13:32)
at Object.<anonymous> (C:\Users\User\Documents\Bookeroo\bookeroo-cdk\src\infra\Launcher.ts:12:1)
at Module._compile (node:internal/modules/cjs/loader:1241:14)
at Module.m._compile (C:\Users\User\Documents\Bookeroo\bookeroo-cdk\node_modules\ts-node\src\index.ts:1618:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
at Object.require.extensions.<computed> [as .ts] (C:\Users\User\Documents\Bookeroo\bookeroo-cdk\node_modules\ts-node\src\index.ts:1621:12)
我只是尝试运行它,希望 cdk 合成器不会抛出错误。我还尝试移动 getBooks 文件并将 .js 添加到目录末尾。他们都没有工作
根据docs,
Code.fromAsset
接受Either a directory with the Lambda code bundle or a .zip file.
将您传递的路径更改为包含资产的文件夹(即删除
"getBooks"
)