我想使用codebuild打包lambda层。
我的代码构建buildspec如下:
version: 0.2
phases:
install:
runtime-versions:
nodejs: 10
commands:
- npm init -y
- npm install --save middy
artifacts:
files:
- 'node_modules/**/*'
- 'package-lock.json'
- 'package.json'
这会将nodejs.zip
文件夹保存到我的s3存储桶中
生成的zip文件如下所示:
package.json如下:
{
"name": "src",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"middy": "^0.30.4"
}
}
但是,当我将此层添加到我的lambda(node10.x)
并导入我的国防部:
'use strict';
var AWS = require('aws-sdk');
const middy = require('middy')
const { cors } = require('middy/middlewares')
返回以下错误:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'middy'\nRequire stack:\n- /var/task/function_code/verify_zipcode.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'middy'",
"Require stack:",
"- /var/task/function_code/verify_zipcode.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:956:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)",
" at Module.load (internal/modules/cjs/loader.js:812:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:724:14)",
" at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)",
" at internal/main/run_main_module.js:17:11"
]
}
添加envoronment变量:
NODE_PATH : ./:/opt/node_modules
使我的lambda可以访问我的图层,但丢失了aws-sdk
的上下文
添加env var后,出现以下错误:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'aws-sdk'\nRequire stack:\n- /var/task/function_code/verify_zipcode.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'",
"Require stack:",
"- /var/task/function_code/verify_zipcode.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:956:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)",
" at Module.load (internal/modules/cjs/loader.js:812:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:724:14)",
" at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)",
" at internal/main/run_main_module.js:17:11"
]
}
是否可以使用本机aws-sdk
和我的图层?还是我每次使用其他自定义图层时都需要使用aws-sdk
图层?
答案 0 :(得分:1)
node.js层(在您的示例中为nodejs.zip)的目录结构应为:
├── nodejs
└── package.json
└── node_modules
└── middy(version z.z.z)
更新您的buildspec文件以添加一个父nodejs文件夹:
version: 0.2
phases:
install:
runtime-versions:
nodejs: 10
commands:
- mkdir nodejs # NEW LINE
- cd nodejs # NEW LINE
- npm init -y
- npm install bcrypt
artifacts:
files:
- 'nodejs/**/*' # CHANGE LINE
- 'package.json'
并删除环境变量:
NODE_PATH