{"id":9831,"date":"2023-03-10T16:41:33","date_gmt":"2023-03-10T08:41:33","guid":{"rendered":"http:\/\/123.57.164.21\/?p=9831"},"modified":"2023-03-10T16:41:33","modified_gmt":"2023-03-10T08:41:33","slug":"%e4%bd%bf%e7%94%a8aws-lambda-%e7%9a%84%e5%b1%82-layer-%e5%8a%9f%e8%83%bd%e5%ae%9e%e7%8e%b0%e4%be%9d%e8%b5%96%e5%8c%85%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=9831","title":{"rendered":"\u4f7f\u7528AWS Lambda \u7684\u201c\u5c42 (Layer) \u201d\u529f\u80fd\u5b9e\u73b0\u4f9d\u8d56\u5305\u7ba1\u7406"},"content":{"rendered":"\n<p>AWS Lambda \u662f\u975e\u5e38\u53d7\u5f00\u53d1\u8005\u6b22\u8fce\u7684\u65e0\u670d\u52a1\u5668\u8fd0\u884c\u4ee3\u7801\u7684\u670d\u52a1\uff0c\u73b0\u5df2\u539f\u751f\u652f\u6301\u5404\u79cd\u4e3b\u6d41\u5f00\u53d1\u8bed\u8a00\uff0c\u5305\u62ecPython\/Node.js\/Go\/Java\/Ruby\/.NET(PowerShell)\uff0c\u8fd8\u652f\u6301\u81ea\u5b9a\u4e49\u8fd0\u884c\u65f6\uff0c\u4f7f\u5f97\u8fd0\u884c C++\/Rust\/Php\/Shell \u7b49\u90fd\u6210\u4e3a\u53ef\u80fd\u3002AWS Lambda Runtime\u53ea\u652f\u6301\u539f\u751f\u7684runtime\uff0c\u5e76\u6ca1\u6709\u9884\u88c5\u4efb\u4f55library\u3002\u5728Lambda\u4e2d\u5f15\u5165library\uff0c\u5c06library\u5b89\u88c5\u5230\u9879\u76ee\u5de5\u7a0b\u4e2d, \u7136\u540e\u4e00\u5e76\u6253\u6210zip\u5305\u4e0a\u4f20\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5728\u5f00\u53d1 Lambda \u5e94\u7528\u7684\u65f6\u5019\uff0c\u901a\u5e38\u4f1a\u6709\u591a\u4e2a Lambda \u51fd\u6570\u90fd\u5171\u7528\u7684\u4ee3\u7801\u4ee5\u53ca\u4f9d\u8d56\u7684\u7b2c\u4e09\u65b9\u5305\u3002 Lambda \u51fd\u6570\u8981\u8c03\u7528\u8fd9\u4e9b\u4f9d\u8d56\u5305\uff0c\u539f\u6765\u7684\u505a\u6cd5\u662f\u5728\u6bcf\u4e2a Lambda \u51fd\u6570\u6253\u5305\u7684\u65f6\u5019\uff0c\u8fde\u5e26\u4f9d\u8d56\u5305\u4e00\u8d77\u6253\u5305\u3002\u6bcf\u4e2a\u51fd\u6570\u90fd\u8fd9\u4e48\u505a\u662f\u4e0d\u65b9\u4fbf\uff0c\u4e5f\u5f71\u54cd\u901f\u5ea6\u7684\u3002\u6216\u8005\u53ef\u4ee5\u591a\u4e2a Lambda \u51fd\u6570\u8fde\u540c\u6240\u6709\u4f9d\u8d56\u5305\u6253\u5305\u6210\u4e00\u4e2a\u5927\u7684\u5305\uff0c\u4f46\u8fd9\u6837\u4e5f\u4e0d\u597d\u7ba1\u7406\u3002<\/p>\n\n\n\n<p>\u73b0\u5728 AWS Lambda \u00a0\u65b0\u589e\u4e86\u201c\u5c42( Layer )\u201d \u529f\u80fd\u4f7f\u5f97\u53ef\u4ee5\u5355\u72ec\u4e0a\u4f20\u5e76\u96c6\u4e2d\u7ba1\u7406 \u00a0Lambda \u00a0\u7684\u4f9d\u8d56\u5305\u4e86\uff0c\u8fd9\u6837 Lambda \u51fd\u6570\u6253\u5305\u7684\u65f6\u5019\u5c31\u53ea\u9700\u8981\u6253\u5305\u8be5\u51fd\u6570\u672c\u8eab\u7684\u4ee3\u7801\uff0c\u4f9d\u8d56\u5305\u53ea\u9700\u8981\u5f15\u7528\u5373\u53ef\u3002\u8fd9\u6837\u7684\u597d\u5904\u662f\u663e\u800c\u6613\u89c1\u7684\uff1a<\/p>\n\n\n\n<ul><li>\u5f00\u53d1\u8005\u4e0d\u7528\u518d\u5173\u6ce8\u4f9d\u8d56\u5305\uff0c\u800c\u53ef\u4ee5\u66f4\u4e13\u6ce8\u4e8e\u4e1a\u52a1\u903b\u8f91\u7684\u5f00\u53d1<\/li><li>\u4f7f\u5f97\u51fd\u6570\u4ee3\u7801\u53d8\u5c0f\u4e86\uff0c\u52a0\u5feb\u4e86\u90e8\u7f72\u7684\u901f\u5ea6<\/li><li>\u53ef\u4ee5\u8de8 AWS \u8d26\u6237\u5171\u4eab\u4ee3\u7801\u548c\u4f9d\u8d56\u5305\uff0c\u53ef\u4ee5\u901a\u8fc7 Policy \u63a7\u5236\u53ea\u5171\u4eab\u7ed9\u67d0\u4e2a\u8d26\u6237\uff0c\u6216\u8005\u5171\u4eab\u7ed9\u6240\u6709\u8d26\u6237\u90fd\u80fd\u5206\u4eab\u4f60\u7684\u4ee3\u7801\u6210\u679c\u3002<\/li><\/ul>\n\n\n\n<p>\u8fd9\u6837\u591a\u4e2a\u8d26\u6237\u7684\u5f00\u53d1\u56e2\u961f\u5171\u4eab\u4f9d\u8d56\u5305\u4e5f\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u4e86\u3002\u8fd9\u771f\u8ba9\u4eba\u5174\u594b\uff01<\/p>\n\n\n\n<p>\u8be5\u529f\u80fd\u5728\u5168\u7403\u63d0\u4f9b Lambda \u670d\u52a1\u7684 AWS \u533a\u57df\uff0c\u5305\u62ec\u7531\u897f\u4e91\u6570\u636e\u8fd0\u8425\u7684 AWS \u4e2d\u56fd\uff08\u5b81\u590f\uff09\u533a\u57df\u548c\u7531\u5149\u73af\u65b0\u7f51\u8fd0\u8425 AWS \u4e2d\u56fd\uff08\u5317\u4eac\uff09\u533a\u57df\u5747\u5df2\u652f\u6301\u3002\u4ee5\u4e0b\u5c31\u4ee5 Python \u5f15\u7528\u672c\u8d26\u6237\u4e0b\u7684\u5171\u4eab\u4ee3\u7801\uff0c\u4ee5\u53ca\u5f15\u7528\u53e6\u4e00\u8d26\u6237\u5206\u4eab\u7684 requests \u5305\u4e24\u4e2a\u4f8b\u5b50\uff0c\u5177\u4f53\u4ecb\u7ecd\u4e00\u4e0b\u5c42\u529f\u80fd\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><strong>\u5f15\u7528\u672c\u8d26\u6237\u7684\u5171\u4eab\u4ee3\u7801<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1.\u6253\u5305<\/strong><\/p>\n\n\n\n<p>\u5148\u628a\u8981\u5171\u4eab\u4ee3\u7801\u4fdd\u5b58\u5728\u672c\u5730\u7535\u8111\u7684 python \u76ee\u5f55\u4e0b\uff0c\u672c\u4f8b\u6587\u4ef6\u540d\u4e3a test2.py \uff0c\u56e0\u4e3a\u662f\u793a\u4f8b\uff0c\u6211\u4eec\u7684\u4ee3\u7801\u53ea\u5199\u4e86\u5982\u4e0b\u4e00\u884c\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=\"\">Python\nvar_from_test2 = 'this is str in test2'<\/pre>\n\n\n\n<p>\u7136\u540e\u628a\u8be5 test2.py \u6587\u4ef6 zip \u6210 share_package.zip \uff0c\u6253 zip \u5305\u65f6\u6587\u4ef6\u540d\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u4e60\u60ef\u6765\u547d\u540d\uff0c\u4f46\u8981\u6ce8\u610f\u76ee\u5f55\u7ed3\u6784\uff0c python \u7684\u8981\u653e\u5728 python \u76ee\u5f55\u4e0b\uff0c\u7136\u540e\u5bf9 python \u76ee\u5f55\u8fdb\u884c zip \u3002\u6240\u4ee5\u65b0\u5efa\u4e00\u4e2a python \u76ee\u5f55\uff0c\u6253\u5305\u540e\u7684\u76ee\u5f55\u7ed3\u6784\u662f\u8fd9\u6837\u7684(<strong>\u6b64\u5904\u4e00\u5b9a\u8981\u6ce8\u610f\u5305\u7684\u76ee\u5f55\uff0c\u76ee\u5f55\u9519\u8bef\uff0c\u5c42\u4f1a\u8c03\u7528\u4e0d\u6210\u529f<\/strong>)\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=\"\">share_package.zip\n\t|----python\/test2.py<\/pre>\n\n\n\n<p>Lambda \u8fd0\u884c\u7684\u65f6\u5019\u4f1a\u628a\u4f9d\u8d56\u5305\u653e\u5728\u8fd0\u884c\u73af\u5883\u7684 \/opt \u76ee\u5f55\u4e0b\uff0c\u5e76\u4f9d\u636e\u4e0d\u540c\u8fd0\u884c\u73af\u5883\u7684\u4e0b\u7ea7\u76ee\u5f55\u6765\u5f15\u7528\uff0c\u6240\u4ee5\u9700\u8981\u4e25\u683c\u6309\u7167\u5982\u4e0b\u56fe\u6240\u793a\u7684\u76ee\u5f55\u7ed3\u6784\u6253\u5305\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-24-937x1024.png\" alt=\"\" class=\"wp-image-9832\" width=\"437\" height=\"477\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-24-937x1024.png 937w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-24-275x300.png 275w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-24-768x839.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-24-830x907.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-24-230x251.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-24-350x383.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-24-480x525.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-24.png 1206w\" sizes=\"(max-width: 437px) 100vw, 437px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2.\u521b\u5efa\u5c42( Layer )<\/strong><\/p>\n\n\n\n<p>\u5230 AWS Lambda \u63a7\u5236\u53f0\u7684\u201c\u5c42\u201d\u83dc\u5355\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5c42<\/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-25-1024x317.png\" alt=\"\" class=\"wp-image-9833\" width=\"484\" height=\"150\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-25-1024x317.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-25-300x93.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-25-768x237.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-25-830x257.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-25-230x71.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-25-350x108.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-25-480x148.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-25.png 1268w\" sizes=\"(max-width: 484px) 100vw, 484px\" \/><\/figure><\/div>\n\n\n\n<p>\u8f93\u5165\u5c42 \u540d\u79f0\u3001\u63cf\u8ff0\uff0c\u9009\u62e9\u521a\u624d\u6253\u5305\u7684zip\u6587\u4ef6\u4e0a\u4f20\uff0c\u5e76\u9009\u62e9\u8fd0\u884c\u65f6\u3002\u7136\u540e\u70b9\u201c\u521b\u5efa\u201d<\/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-26-1024x766.png\" alt=\"\" class=\"wp-image-9834\" width=\"502\" height=\"375\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-26-1024x766.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-26-300x224.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-26-768x574.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-26-830x621.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-26-230x172.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-26-350x262.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-26-480x359.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-26.png 1284w\" sizes=\"(max-width: 502px) 100vw, 502px\" \/><\/figure><\/div>\n\n\n\n<p>\u8fd9\u6837\u5c42\u5c31\u521b\u5efa\u6210\u529f\u4e86\u3002\u4ee5\u540e\u5982\u679c\u8981\u66f4\u65b0\uff0c\u5219\u70b9\u201c\u521b\u5efa\u7248\u672c\u201d\u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a<\/p>\n\n\n\n<p>\u5c42\u7248\u672c\u662f\u4e0d\u53ef\u4fee\u6539\u7684\uff0c\u5982\u679c\u4e00\u4e2a\u7248\u672c\u88ab\u5220\u9664\uff0c\u6216\u8005\u5c42\u7248\u672c\u5171\u4eab\u6743\u9650\u88ab\u53d6\u6d88\u4e86\uff0c\u5df2\u7ecf\u5f15\u7528\u4e86\u8be5\u7248\u672c\u7684 Lambda \u51fd\u6570\u4f9d\u7136\u662f\u80fd\u5de5\u4f5c\u7684\uff0c\u4f46\u4e0d\u80fd\u518d\u65b0\u5efa Lambda \u51fd\u6570\u53bb\u5f15\u7528\u8fd9\u4e2a\u88ab\u5220\u9664\u7684\u5c42\u7248\u672c\u3002<\/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-27-1024x640.png\" alt=\"\" class=\"wp-image-9835\" width=\"506\" height=\"316\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-27-1024x640.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-27-300x188.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-27-768x480.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-27-830x519.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-27-230x144.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-27-350x219.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-27-480x300.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-27.png 1296w\" sizes=\"(max-width: 506px) 100vw, 506px\" \/><\/figure><\/div>\n\n\n\n<p><strong>3.\u521b\u5efa\u51fd\u6570( Function )<\/strong><\/p>\n\n\n\n<p>\u5230\u201c\u51fd\u6570\u201d\u83dc\u5355\u4e0b\u521b\u5efa Lambda \u7684\u4e3b\u51fd\u6570 test_main \uff0c\u4e0a\u4f20\u4ee3\u7801\uff0c\u5e76\u914d\u7f6e\u5bf9\u5e94\u7684\u6267\u884c\u89d2\u8272(\u4f8b\u5982 lambda_base_execution )\u4ee5\u53ca\u5176\u4ed6\u53c2\u6570\uff0c\u8fd9\u65f6\u5019\u8fd8\u6ca1\u5f15\u5165\u4f9d\u8d56\u5305\u3002\u5148\u5199\u4e00\u6bb5\u7b80\u5355\u4ee3\u7801\uff0c\u770b\u770b\u8fd0\u884c\u7ed3\u679c\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=\"\">Python\n\nimport json\nimport os\n\ndef lambda_handler(event, context):\n    os.system('df')\n\n    return {\n        'statusCode': 200,\n        'body': json.dumps('Hello from Lambda!')\n}\n<\/pre>\n\n\n\n<p>\u4fdd\u5b58\u5e76\u6d4b\u8bd5\u6267\u884c\uff0c\u8fd9\u65f6\u5019\u770b\u5230\u6267\u884c\u4e86 Linux shell \u7684 df \u547d\u4ee4\uff0c\u5217\u51fa\u4e86 Lambda \u8fd0\u884c\u73af\u5883\u7684\u672c\u5730\u76ee\u5f55\uff0c\u9664\u4e86\u6839\u76d8\uff0c\u53ea\u6709\u4e00\u4e2a 512MB \u00a0\u7684 \u00a0\/tmp \uff08\u8fd9\u662f\u53ef\u4ee5\u4f9b\u5ba2\u6237\u5199\u5165\u7684\u4e34\u65f6\u76d8\uff09<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-28.png\" alt=\"\" class=\"wp-image-9836\" width=\"523\" height=\"212\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-28.png 944w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-28-300x122.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-28-768x312.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-28-830x338.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-28-230x94.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-28-350x142.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-28-480x195.png 480w\" sizes=\"(max-width: 523px) 100vw, 523px\" \/><\/figure><\/div>\n\n\n\n<p><strong>4.\u5f15\u5165\u4f9d\u8d56<\/strong><\/p>\n\n\n\n<p>\u4e0b\u9762\u6765\u5f15\u5165\u4f9d\u8d56\u5305 test2 \uff0c\u4e3b\u51fd\u6570 test_main \u4ee3\u7801\u53d8\u6210\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=\"\">Python\n\nimport json\nimport os\nimport test2\n\ndef lambda_handler(event, context):\n    os.system('df')\n    print(test2.var_from_test2)\n    return{\n        'statusCode': 200,\n        'body': json.dumps('Hello from Lambda!')\n}\n<\/pre>\n\n\n\n<p>\u8fd9\u91cc import \u4e86 test2 \u5e76\u4e14\u8c03\u7528\u4e86\u5176\u4e2d\u7684 var_from_test2 \u3002<\/p>\n\n\n\n<p>\u7136\u540e\u70b9 Layers \u9009\u9879\u53bb\u8fde\u63a5\u5c42<\/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-29-1024x690.png\" alt=\"\" class=\"wp-image-9837\" width=\"411\" height=\"277\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-29-1024x690.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-29-300x202.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-29-768x518.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-29-830x559.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-29-230x155.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-29-350x236.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-29-480x323.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-29.png 1294w\" sizes=\"(max-width: 411px) 100vw, 411px\" \/><\/figure><\/div>\n\n\n\n<p>\u6dfb\u52a0\u5c42\uff0c\u9009\u62e9\u521a\u624d\u521b\u5efa\u7684\u5c42\uff0c\u5e76\u9009\u62e9\u5bf9\u5e94\u7684\u5c42\u7248\u672c\uff0c\u7136\u540e\u201c\u8fde\u63a5\u201d<\/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-30-1024x506.png\" alt=\"\" class=\"wp-image-9838\" width=\"419\" height=\"207\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-30-1024x506.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-30-300x148.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-30-768x379.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-30-830x410.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-30-230x114.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-30-350x173.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-30-480x237.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-30.png 1288w\" sizes=\"(max-width: 419px) 100vw, 419px\" \/><\/figure><\/div>\n\n\n\n<p>\u6700\u540e\u8bb0\u5f97\u4fdd\u5b58 Lambda \u51fd\u6570\u3002\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u4f9d\u8d56\u5305\u7684\u914d\u7f6e\u5f15\u7528\u4e86\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a<\/p>\n\n\n\n<p>\u6bcf\u4e2a Lambda \u51fd\u6570\u6700\u591a\u53ef\u4ee5\u914d\u7f6e5\u4e2a\u5c42\uff0c\u51fd\u6570\u52a0\u4e0a\u5c42\u7684\u603b\u5927\u5c0f\u4e0d\u80fd\u8d85\u8fc7 \u00a0250 MB \uff08 unzipped size \uff09 \u3002\u8be6\u89c1 AWS Lambda Limits <a href=\"https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/limits.html\">https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/limits.html<\/a><\/p>\n\n\n\n<p><strong>5.\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<p>\u6d4b\u8bd5\u6267\u884c\uff0c\u6211\u4eec\u53d1\u73b0 test2 \u5305\u7684 var_from_test2 \u5df2\u7ecf\u88ab\u4e3b\u51fd\u6570\u6240\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u5e76\u4e14\u6211\u4eec\u770b\u5230 Linux shell \u6267\u884c df \u7684\u7ed3\u679c\uff0c Lambda \u73af\u5883\u591a\u4e86\u4e00\u4e2a \/opt \u76ee\u5f55\uff0c\u8fd9\u4e2a\u5c31\u662f\u4fdd\u5b58\u4f9d\u8d56\u5305\u7684\u76ee\u5f55\u3002<\/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-31-1024x628.png\" alt=\"\" class=\"wp-image-9839\" width=\"501\" height=\"307\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-31-1024x628.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-31-300x184.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-31-768x471.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-31-830x509.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-31-230x141.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-31-350x215.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-31-480x294.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-31.png 1278w\" sizes=\"(max-width: 501px) 100vw, 501px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u5f15\u7528\u5176\u4ed6 AWS \u8d26\u6237\u7684\u5305<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1.\u672c\u5730\u5b89\u88c5 requests \u5305\u5e76\u4e0a\u4f20\u5230\u53e6\u4e00\u4e2a AWS \u8d26\u6237\uff08\u4ee5\u4e0b\u79f0\u6e90\u8d26\u6237\uff09<\/strong><\/p>\n\n\n\n<p>\u5728\u672c\u5730\u521a\u624d\u5df2\u521b\u5efa\u7684 python \u76ee\u5f55\u4e0b\uff0c\u5b89\u88c5 requests \u5305\u5230\u672c\u76ee\u5f55\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=\"\">pip install requests -t .\n\nzip -r9 python-requests.zip ..\/python<\/pre>\n\n\n\n<p>\u7136\u540e\u5728\u6e90\u8d26\u6237\u7684 Lambda \u63a7\u5236\u53f0\uff0c\u521b\u5efa\u5c42\uff0c\u5e76\u4e0a\u4f20 python-requests.zip \uff0c\u5c42\u540d\u79f0\u5b9a\u4e3a requests-layer \uff0c\u7248\u672c\u4e3a1\u3002\u8bb0\u5f55\u4e0b\u5c42\u7248\u672c\u7684 ARN \uff0c\u4e0b\u9762\u4f1a\u7528\u5230\u3002<\/p>\n\n\n\n<p><strong>2.\u5728\u6e90\u8d26\u6237\u8bbe\u7f6e\u5c42\u5171\u4eab<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528 AWS CLI \u547d\u4ee4\u884c\u8bbe\u7f6e\u5c42\u5171\u4eab\u6743\u9650\uff0c\u5982\u679c CLI \u6ca1\u6709 add-layer-version-permission \u547d\u4ee4\uff0c\u5219\u8bf7\u5347\u7ea7 CLI \u5230\u6700\u65b0\u7248\u672c<\/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=\"\">$ aws lambda add-layer-version-permission --layer-name requests-layer \\\n--statement-id engineering-org --version-number 1 --principal '*' \\\n--action lambda:GetLayerVersion<\/pre>\n\n\n\n<p>\u5bf9\u6240\u6709 AWS \u8d26\u6237\u90fd\u5206\u4eab\u8be5\u5305\uff0c\u53ef\u4ee5\u8bbe\u7f6e principal \u00a0\u4e3a \u2018*\u2019\uff0c\u5982\u679c\u53ea\u9488\u5bf9\u67d0\u4e2a\u8d26\u6237\u5206\u4eab\u5219\u8bbe\u7f6e\u5bf9\u5e94\u7684\u4fe1\u4efb\u5b9e\u4f53\u3002<\/p>\n\n\n\n<p>\u2013principal \u2018&lt;account&gt;\u2019 &nbsp;\u7b49\u540c\u4e8e &nbsp;\u2013principal \u2018arn:aws-cn:iam:: &lt;account&gt;:root\u2019 \u3002\u628a&lt;account&gt;\u66ff\u6362\u4e3a\u76ee\u7684\u8d26\u6237\u7684\u53f7\u7801<\/p>\n\n\n\n<p>\u2013statement-id &nbsp;\u662f\u81ea\u5b9a\u4e49\u7684\u5bf9\u8be5\u6761\u7b56\u7565\u7684\u7f16\u53f7\uff0c\u5982\u8981\u5bf9\u591a\u4e2a\u8d26\u6237\u5206\u4eab\u6743\u9650\uff0c\u53ef\u4ee5\u591a\u6b21\u4e0b\u53d1\u8be5<code>add-layer-version-permission <\/code>\u547d\u4ee4\uff0c\u914d\u7f6e\u4e0d\u540c\u7684\u5e10\u53f7\u548c statement-id\u3002<\/p>\n\n\n\n<p>\u7528 \u00a0get-layer-version-policy \u00a0\u547d\u4ee4\u67e5\u8be2\u8bbe\u7f6e\u7684\u6743\u9650\u7b56\u7565<\/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=\"\">$ aws lambda add-layer-version-permission --layer-name requests-layer \\\n--statement-id engineering-org --version-number 1 --principal '*' \\\n--action lambda:GetLayerVersion<\/pre>\n\n\n\n<p><strong>3.\u5728\u76ee\u7684\u8d26\u6237\u8bbe\u7f6e Lambda \u7684 GetLayerVersion \u6743\u9650<\/strong><\/p>\n\n\n\n<p>\u5230\u8981\u8c03\u7528\u5171\u4eab\u4f9d\u8d56\u5305\u7684\u90a3\u4e2a\u8d26\u6237\uff08\u76ee\u7684\u8d26\u6237\uff09\u5728 IAM \u63a7\u5236\u53f0\u4e3a Lambda \u7684\u6267\u884c\u89d2\u8272\u589e\u52a0\u4e00\u4e2a\u5185\u8054\u7b56\u7565\uff0c\u8d4b GetLayerVersion \u7684\u6743\u9650\uff0c\u4f7f\u5f97 Lambda \u53ef\u4ee5\u83b7\u53d6\u5176\u4ed6\u8d26\u6237\u7684\u5c42<\/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{\n    \"Version\": \"2012-10-17\",\n    \"Statement\": [\n        {\n            \"Sid\": \"VisualEditor0\",\n            \"Effect\": \"Allow\",\n            \"Action\": \"lambda:GetLayerVersion\",\n            \"Resource\": \"*\"\n        }\n    ]\n}\n<\/pre>\n\n\n\n<p>\u8003\u8651\u6700\u5c0f\u6743\u9650\u539f\u5219\uff0c\u5728\u751f\u4ea7\u73af\u5883\u91cc Resource \u5e94\u8be5\u7528\u6e90\u5e10\u53f7\u5c42\u7248\u672c\u7684 ARN \u6765\u4ee3\u66ff *<\/p>\n\n\n\n<p><strong>4.Lambda \u51fd\u6570\u8fde\u63a5\u5c42<\/strong><\/p>\n\n\n\n<p>\u5230\u76ee\u7684\u8d26\u6237\u7684 Lambda \u51fd\u6570\u83dc\u5355\uff0c\u9009\u62e9\u5bf9\u5e94\u7684\u51fd\u6570\uff0c\u5e76\u6dfb\u52a0\u5c42\u5230\u5f53\u524d\u51fd\u6570\u3002<\/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-32-1024x506.png\" alt=\"\" class=\"wp-image-9840\" width=\"546\" height=\"270\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-32-1024x506.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-32-300x148.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-32-768x379.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-32-830x410.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-32-230x114.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-32-350x173.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-32-480x237.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-32.png 1284w\" sizes=\"(max-width: 546px) 100vw, 546px\" \/><\/figure><\/div>\n\n\n\n<p>\u9009\u62e9\u63d0\u4f9b\u5c42 ARN \uff0c\u7136\u540e\u7c98\u8d34\u6211\u4eec\u521a\u624d\u5728\u6e90\u8d26\u6237\u521b\u5efa\u7684\u5c42 ARN \u3002\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u8c03\u7528\u7b2c\u4e09\u65b9\u8d26\u6237\u5171\u4eab\u4f9d\u8d56\u5305\u7684\u5de5\u4f5c\u4e86\u3002<\/p>\n\n\n\n<p><strong>5.\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<p>\u6d4b\u8bd5\u6267\u884c\uff0c Lambda \u5df2\u7ecf\u53ef\u4ee5 import requests \uff0c\u5e76\u53d1\u8d77\u8bbf\u95ee\u3002<\/p>\n\n\n\n<p>\u68c0\u67e5\u770b\u5230 \/opt\/python \u76ee\u5f55\u5df2\u7ecf\u6709\u4e86\u521a\u624d\u6253\u5305\u7684\u6240\u6709\u4f9d\u8d56\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-33.png\" alt=\"\" class=\"wp-image-9841\" width=\"523\" height=\"424\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-33.png 984w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-33-300x243.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-33-768x623.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-33-830x673.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-33-230x187.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-33-350x284.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-33-480x389.png 480w\" sizes=\"(max-width: 523px) 100vw, 523px\" \/><\/figure><\/div>\n\n\n\n<p>\u53c2\u8003\u6587\u6863\uff1a<\/p>\n\n\n\n<ul><li>Lambda \u5c42\u8bf4\u660e<\/li><\/ul>\n\n\n\n<p><a href=\"https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/configuration-layers.html\">https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/configuration-layers.html<\/a><\/p>\n\n\n\n<ul><li>Lambda \u8fd0\u884c\u73af\u5883\u548c\u5185\u7f6e\u7684\u5e93\uff1a<\/li><\/ul>\n\n\n\n<p><a href=\"https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/current-supported-versions.html\">https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/current-supported-versions.html<\/a><\/p>\n\n\n\n<ul><li>Lambda \u7684 IAM Policy<\/li><\/ul>\n\n\n\n<p><a href=\"https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/access-control-identity-based.html\">https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/access-control-identity-based.html<\/a><\/p>\n\n\n\n<ul><li>IAM Policy \u8bf4\u660e<\/li><\/ul>\n\n\n\n<p><a href=\"https:\/\/docs.aws.amazon.com\/AmazonS3\/latest\/dev\/s3-bucket-user-policy-specifying-principal-intro.html\">https:\/\/docs.aws.amazon.com\/AmazonS3\/latest\/dev\/s3-bucket-user-policy-specifying-principal-intro.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>AWS Lambda \u662f\u975e\u5e38\u53d7\u5f00\u53d1\u8005\u6b22\u8fce\u7684\u65e0\u670d\u52a1\u5668\u8fd0\u884c\u4ee3\u7801\u7684\u670d\u52a1\uff0c\u73b0\u5df2\u539f\u751f\u652f\u6301\u5404\u79cd\u4e3b\u6d41\u5f00\u53d1\u8bed\u8a00\uff0c\u5305\u62ecPytho [&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\/9831"}],"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=9831"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9831\/revisions"}],"predecessor-version":[{"id":9843,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9831\/revisions\/9843"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}