{"id":9853,"date":"2023-03-13T10:59:25","date_gmt":"2023-03-13T02:59:25","guid":{"rendered":"http:\/\/123.57.164.21\/?p=9853"},"modified":"2023-03-13T10:59:25","modified_gmt":"2023-03-13T02:59:25","slug":"lambda-local-%e5%91%bd%e4%bb%a4%e8%a1%8c%e5%b7%a5%e5%85%b7-%e7%94%a8%e4%ba%8e%e5%9c%a8%e6%9c%ac%e5%9c%b0%e8%ae%a1%e7%ae%97%e6%9c%ba%e4%b8%8a%e8%bf%90%e8%a1%8camazon-lambda%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=9853","title":{"rendered":"lambda-local: \u547d\u4ee4\u884c\u5de5\u5177 \u7528\u4e8e\u5728\u672c\u5730\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884cAmazon Lambda\u51fd\u6570"},"content":{"rendered":"\n<p><strong>Lambda-local<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Lambda-local\u5141\u8bb8\u60a8\u5728\u672c\u5730\u673a\u5668\u4e0a\u6d4b\u8bd5nodejsamazon Lambda\u51fd\u6570\uff0c\u65b9\u6cd5\u662f\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684API\u548ccommand-line\u5de5\u5177\u3002<\/p>\n\n\n\n<p>\u5b83\u7684\u76ee\u6807\u5e76\u4e0d\u662f\u50cfserverless-offline\u6216docker-lambda\u90a3\u6837\u5b8c\u7f8e\u5730\u8bc1\u660e\u7279\u6027\uff0c\u800c\u662f\u4fdd\u6301\u975e\u5e38\u8f7b\uff08\u5b83\u4ecd\u7136\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u5168\u6784\u5efa\u7684<code>Context<\/code>\uff0c\u5904\u7406\u5176\u6240\u6709\u53c2\u6570\u548c\u51fd\u6570\uff0c\u5e76\u4e14\u6240\u6709\u7684\u4e1c\u897f\u90fd\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9a\u5236\uff09\u3002<\/p>\n\n\n\n<p>\u4e3b\u8981\u76ee\u6807\u662f\u5355\u5143\u6d4b\u8bd5\u548c\u672c\u5730\u8fd0\u884clambda\u51fd\u6570\u3002<\/p>\n\n\n\n<p><strong>Install<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npm install -g lambda-local<\/pre>\n\n\n\n<p><strong>Build<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">make build<\/pre>\n\n\n\n<p>Or<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npm install\nnpm install --only=dev\nnpm run build<\/pre>\n\n\n\n<p><strong>Usage<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>\u4f5c\u4e3aAPI\uff1a\u4e5f\u53ef\u4ee5\u5728\u811a\u672c\u4e2d\u76f4\u63a5\u4f7f\u7528Lambda local\u3002\u4f8b\u5982\uff0c\u4e3a\u4e86\u83b7\u5f97\u6d4b\u8bd5\u8986\u76d6\u7387\uff0c\u5728MochaJS\u6d4b\u8bd5\u5957\u4ef6\u4e2d\u5f88\u6709\u8da3\u3002<\/li><li>\u4f5c\u4e3a\u547d\u4ee4\u884c\u5de5\u5177\uff1a\u53ef\u4ee5\u4f7f\u7528Lambda-local\u4f5c\u4e3a\u547d\u4ee4\u884c\u5de5\u5177\u3002<\/li><\/ul>\n\n\n\n<p id=\"sh8o6y2u0gc\"><strong>About: API<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>LambdaLocal<\/strong><\/p>\n\n\n\n<p>API\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8bbf\u95ee\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const lambdaLocal = require(\"lambda-local\");<\/pre>\n\n\n\n<p>\u6216\u8005\u5728TypeScript\u4e0a\uff08\u652f\u63011.7.0+)\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import lambdaLocal = require(\"lambda-local\");<\/pre>\n\n\n\n<p><code>lambdaLocal.execute(options)<\/code><\/p>\n\n\n\n<p>\u6267\u884c\u7ed9\u5b9a<code>options<\/code>\u5bf9\u8c61\u7684lambda\uff0c\u8be5\u5bf9\u8c61\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u7684\u952e\u53ef\u80fd\u662f\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-40-1024x712.png\" alt=\"\" class=\"wp-image-9854\" width=\"544\" height=\"378\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-40-1024x712.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-40-300x209.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-40-768x534.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-40-1536x1068.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-40-830x577.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-40-230x160.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-40-350x243.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-40-480x334.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-40.png 1688w\" sizes=\"(max-width: 544px) 100vw, 544px\" \/><\/figure><\/div>\n\n\n\n<p><code>lambdaLocal.setLogger(logger)<\/code><\/p>\n\n\n\n<p><code>lambdaLocal.getLogger()<\/code><\/p>\n\n\n\n<p>\u8fd9\u4e9b\u51fd\u6570\u5141\u8bb8\u8bbf\u95eelambda-local\u4f7f\u7528\u7684winston\u8bb0\u5f55\u5668\u3002<\/p>\n\n\n\n<p><strong>API examples<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728\u8fd9\u91cc\u7684\u6d4b\u8bd5\u6587\u4ef6\u4e2d\u53ef\u4ee5\u627e\u5230\u5f88\u591a\u4f8b\u5b50\uff0c\u5c24\u5176\u662f\u5728\u6469\u5361\u4e2d\u4f7f\u7528\u7684\u4f8b\u5b50<\/p>\n\n\n\n<p>\u57fa\u672c\u7528\u6cd5\uff1a\u4f7f\u7528\u627f\u8bfa<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const lambdaLocal = require('lambda-local');\n\nvar jsonPayload = {\n    'key': 1,\n    'another_key': \"Some text\"\n}\n\nlambdaLocal.execute({\n    event: jsonPayload,\n    lambdaPath: path.join(__dirname, 'path_to_index.js'),\n    profilePath: '~\/.aws\/credentials',\n    profileName: 'default',\n    timeoutMs: 3000\n}).then(function(done) {\n    console.log(done);\n}).catch(function(err) {\n    console.log(err);\n});<\/pre>\n\n\n\n<p>\u57fa\u672c\u7528\u6cd5\uff1a\u4f7f\u7528\u56de\u8c03<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const lambdaLocal = require('lambda-local');\n\nvar jsonPayload = {\n    'key': 1,\n    'another_key': \"Some text\"\n}\n\nlambdaLocal.execute({\n    event: jsonPayload,\n    lambdaPath: path.join(__dirname, 'path_to_index.js'),\n    profilePath: '~\/.aws\/credentials',\n    profileName: 'default',\n    timeoutMs: 3000,\n    callback: function(err, data) {\n        if (err) {\n            console.log(err);\n        } else {\n            console.log(data);\n        }\n    },\n    clientContext: JSON.stringify({clientId: 'xxxx'})\n});\n\n<\/pre>\n\n\n\n<p id=\"950ri1z481w\"><strong>About: CLI<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Available Arguments<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\n    -l, --lambda-path &lt;lambda index path>\uff08\u5fc5\u9700\uff09\u6307\u5b9aLambda\u51fd\u6570\u6587\u4ef6\u540d\u3002\n    -e, --event-path &lt;event path>\uff08\u5fc5\u9700\uff09\u6307\u5b9a\u4e8b\u4ef6\u6570\u636e\u6587\u4ef6\u540d\u3002\n    -h, --handler &lt;handler name>\uff08\u53ef\u9009\uff09Lambda\u51fd\u6570\u5904\u7406\u7a0b\u5e8f\u540d\u79f0\u3002\u9ed8\u8ba4\u4e3a\u201c\u5904\u7406\u7a0b\u5e8f\u201d\u3002\n    -t, --timeout &lt;timeout>\uff08\u53ef\u9009\uff09\u79d2\uff0c\u76f4\u5230lambda\u51fd\u6570\u8d85\u65f6\u3002\u9ed8\u8ba4\u503c\u4e3a3\u79d2\u3002\n    -r, --region &lt;aws region>\uff08\u53ef\u9009\uff09\u8bbe\u7f6eAWS\u533a\u57df\uff0c\u9ed8\u8ba4\u4e3aus-east-1\u3002\n    -P, --profile-path &lt;aws profile name>\uff08\u53ef\u9009\uff09\u8bfb\u53d6\u6307\u5b9a\u7684AWS\u51ed\u636e\u6587\u4ef6\u3002\n    -p, --profile &lt;aws profile name>\uff08\u53ef\u9009\uff09\u4e0e-P\u4e00\u8d77\u4f7f\u7528\uff1a\u8bfb\u53d6\u6587\u4ef6\u7684AWS\u914d\u7f6e\u6587\u4ef6\u3002\n    -E, --environment &lt;JSON {key:value}>\uff08\u53ef\u9009\uff09\u4e3alambda\u8bbe\u7f6e\u989d\u5916\u7684\u73af\u5883\u53d8\u91cf\n    --wait-empty-event-loop\uff08\u53ef\u9009\uff09Sets callbackitsforemptyeventloop=True=>\u5c06\u5728\u8fd4\u56de\u4e4b\u524d\u7b49\u5f85\u7a7a\u5faa\u73af\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u9519\u8bef\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u5b9e\u73b0\u5e76\u4e0d\u5b8c\u7f8e\uff0c\u53ea\u201c\u6a21\u62df\u201d\u5b83\u3002\n    --envdestroy\uff08\u53ef\u9009\uff09\u5173\u95ed\u65f6\u9500\u6bc1\u6dfb\u52a0\u7684\u73af\u5883\u3002\u9ed8\u8ba4\u4e3afalse\n    -v, --verboselevel &lt;3\/2\/1\/0>\uff08\u53ef\u9009\uff09\u9ed8\u8ba4\u503c3\u3002\u7ea7\u522b2\u6d88\u9664handler\uff08\uff09\u6587\u672c\uff0c\u7ea7\u522b1\u6d88\u9664lambda-local\u6587\u672c\uff0c\u7ea7\u522b0\u4e5f\u6d88\u9664\u7ed3\u679c\u3002\n    --envfile &lt;path\/to\/env\/file>\uff08\u53ef\u9009\uff09\u4eceenv\u6587\u4ef6\u8bbe\u7f6e\u989d\u5916\u7684\u73af\u5883\u53d8\u91cf\n    --inspect [[host:]port]\uff08\u53ef\u9009\uff09\u4f7f\u7528NodeJS\u68c0\u67e5\u5668\u542f\u52a8lambda-local\uff08\u5728NodeJS>8.0.0\u4e2d\u63d0\u4f9b\uff09\n    -W, --watch [port]\uff08\u53ef\u9009\uff09\u5728\u76d1\u89c6\u6a21\u5f0f\u4e0b\u542f\u52a8lambda-local\uff0c\u76d1\u542c\u6307\u5b9a\u7684\u7aef\u53e3[1-65535]\u3002\n<\/pre>\n\n\n\n<p><strong>CLI examples<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Simple usage\nlambda-local -l index.js -h handler -e examples\/s3-put.js\n\n# Input environment variables\nlambda-local -l index.js -h handler -e examples\/s3-put.js -E '{\"key\":\"value\",\"key2\":\"value2\"}'<\/pre>\n\n\n\n<p><strong>\u5c06lambda\u51fd\u6570\u4f5c\u4e3aHTTP\u670d\u52a1\u5668\u8fd0\u884c<\/strong><\/p>\n\n\n\n<p>\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u53ef\u4ee5\u5728\u672c\u5730\u8fd0\u884clambda\u51fd\u6570\uff0c\u800c\u4e0d\u9700\u8981\u521b\u5efa\u4efb\u4f55\u7279\u6b8a\u7684\u6a21\u677f\u6587\u4ef6\uff08\u6bd4\u5982serverlessplugin\u548cSAM\u6240\u9700\u7684\uff09\uff0c\u53ea\u9700\u6dfb\u52a0\u53c2\u6570<code>--watch<\/code>\u3002\u5b83\u5c06\u5f15\u53d1\u4e00\u4e2a\u76d1\u542c\u6307\u5b9a\u7aef\u53e3\u7684http\u670d\u52a1\u5668\uff08\u9ed8\u8ba4\u503c\u4e3a8008\uff09\uff0c\u7136\u540e\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bf7\u6c42\u4f53\u5c06\u4e8b\u4ef6\u8d1f\u8f7d\u4f20\u9012\u7ed9\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">lambda-local -l examples\/handler_helloworld.js -h handler --watch 8008\n\ncurl --request POST \\\n  --url http:\/\/localhost:8008\/ \\\n  --header 'content-type: application\/json' \\\n  --data '{\n\"event\": {\n\"key1\": \"value1\",\n\"key2\": \"value2\",\n\"key3\": \"value3\"\n}\n}'<\/pre>\n\n\n\n<p id=\"xinpv3rr62\"><strong>About: Definitions<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Event data<\/strong><\/p>\n\n\n\n<p>\u4e8b\u4ef6\u793a\u4f8b\u6570\u636e\u88ab\u653e\u5728<code>examples<\/code>\u6587\u4ef6\u5939\u4e2d-\u8bf7\u968f\u610f\u4f7f\u7528\u6b64\u5904\u7684\u6587\u4ef6\uff0c\u6216\u521b\u5efa\u60a8\u81ea\u5df1\u7684\u4e8b\u4ef6\u6570\u636e\u3002\u4e8b\u4ef6\u6570\u636e\u53ea\u662f\u5bfc\u51fa\u7684JSON\u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Sample event data\nmodule.exports = {\n\tfoo: \"bar\"\n};\n<\/pre>\n\n\n\n<p><strong>Context<\/strong><\/p>\n\n\n\n<p><code>context<\/code>\u5bf9\u8c61\u662f\u4ece\u5728AWS\u4e0a\u8fd0\u884c\u5b9e\u9645Lambda\u51fd\u6570\u65f6\u53ef\u89c1\u7684\u5185\u5bb9\u4e2d\u53d6\u6837\u7684\uff0c\u4ee5\u53ca\u53ef\u7528\u7684\u6587\u6863\uff0c\u5b83\u4eec\u53ef\u80fd\u4f1a\u66f4\u6539\u8be5\u5bf9\u8c61\u7684\u5185\u90e8\u7ed3\u6784\uff0cLambda-local\u5e76\u4e0d\u4fdd\u8bc1\u4e0e\u5b9e\u9645\u4e0a\u4e0b\u6587\u5bf9\u8c61\u603b\u662fup-to-date\u3002<\/p>\n\n\n\n<p><strong>AWS-SDK<\/strong><\/p>\n\n\n\n<p>\u7531\u4e8eAmazon Lambda\u53ef\u4ee5\u5728\u4e0d\u5b89\u88c5\u7684\u60c5\u51b5\u4e0b\u52a0\u8f7dAWS-SDKnpm\uff0cLambda-local\u8fd8\u5c06AWS-SDK\u6253\u5305\u5728\u5176\u4f9d\u8d56\u9879\u4e2d\u3002\u5982\u679c\u60a8\u60f3\u4f7f\u7528\u8fd9\u4e2a\uff0c\u8bf7\u5728aws\u51ed\u8bc1\u6587\u4ef6\u4e2d\u4f7f\u7528<code>-p<\/code>\u6216<code>-P<\/code>\u9009\u9879\uff08\u6216\u5b83\u4eec\u7684API\u5bf9\u5e94\u9879\uff09\u3002\u66f4\u591a\u4fe1\u606f\u8bf7\u70b9\u51fb\uff1ahttp:\/\/docs.aws.amazon.com\/cli\/latest\/userguide\/cli-chap-getting-started.html#cli-config-files<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lambda-local Lambda-local\u5141\u8bb8\u60a8\u5728\u672c\u5730\u673a\u5668\u4e0a\u6d4b\u8bd5nodejsamazon Lambda [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,9],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9853"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9853"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9853\/revisions"}],"predecessor-version":[{"id":9856,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9853\/revisions\/9856"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}