{"id":234,"date":"2016-05-21T20:54:23","date_gmt":"2016-05-21T12:54:23","guid":{"rendered":"https:\/\/vinta.ws\/code\/?p=234"},"modified":"2026-03-17T00:02:56","modified_gmt":"2026-03-16T16:02:56","slug":"aws-lambda-notes","status":"publish","type":"post","link":"https:\/\/vinta.ws\/code\/aws-lambda-notes.html","title":{"rendered":"AWS Lambda Cookbook"},"content":{"rendered":"<p>AWS Lambda is an event-driven service that lets you upload your code and run it on-demand without having your own servers.<\/p>\n<p>ref:<br \/>\n<a href=\"http:\/\/aws.amazon.com\/lambda\/\">http:\/\/aws.amazon.com\/lambda\/<\/a><br \/>\n<a href=\"http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/limits.html\">http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/limits.html<\/a><\/p>\n<p>API Gateway \u5c31\u662f URL routing<br \/>\nLambda \u5247\u662f\u90a3\u4e9b route (endpoint) \u5c0d\u61c9\u7684 handler<br \/>\n\u5982\u679c\u4f60\u662f\u7528 event \u6216 schedule \u7684\u65b9\u5f0f\u547c\u53eb Lambda function \u7684\u8a71<br \/>\n\u53ef\u4ee5\u4e0d\u7528 API Gateway<\/p>\n<p>AWS Lambda \u6709\u5169\u7a2e invocation type<br \/>\n\u4e00\u662f RequestResponse\uff0c\u540c\u6b65\uff08\u4f8b\u5982\u7d81\u5b9a API Gateway \u548c\u4f60\u5728 Lambda Management Console \u64cd\u4f5c\u7684\u6642\u5019\uff09<br \/>\n\u4e8c\u662f Event\uff0c\u975e\u540c\u6b65<\/p>\n<h2>Runtimes<\/h2>\n<p>AWS Lambda supports the following runtime versions:<\/p>\n<ul>\n<li><code>nodejs<\/code> (Node v0.10)<\/li>\n<li><code>nodejs4.3<\/code><\/li>\n<li><code>java<\/code><\/li>\n<li><code>python<\/code><\/li>\n<\/ul>\n<p>ref:<br \/>\n<a href=\"http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/current-supported-versions.html\">http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/current-supported-versions.html<\/a><\/p>\n<h3>Node.js<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-js\">const aws = require('aws-sdk');\n\nexports.handle = (event, context, callback) =&gt; {\n  doYourShit();\n  callback(null, 'DONE');\n};<\/code><\/pre>\n<p>\u6bcf\u500b Lambda function \u6703\u63a5\u6536\u4e09\u500b\u53c3\u6578 <code>event<\/code>\u3001<code>context<\/code> \u548c <code>callback<\/code><\/p>\n<p><code>event<\/code> \u662f\u5f9e\u5916\u90e8\u7684 input<br \/>\n\u53ef\u80fd\u662f\u4f86\u81ea S3 object event\u3001DynamoDB stream \u6216\u662f\u7531 API Gateway POST \u9032\u4f86\u7684 JSON payload<\/p>\n<p><code>context<\/code> \u5247\u6703\u5305\u542b\u7576\u524d\u9019\u500b Lambda fuction \u7684\u4e00\u4e9b metadata<br \/>\n\u4f8b\u5982 <code>context.getRemainingTimeInMillis()<\/code><\/p>\n<p><code>callback<\/code> \u53c3\u6578\u53ea\u6709 Node.js runtime v4.3 \u624d\u652f\u63f4<br \/>\nv0.10 \u7684\u8a71\u5f97\u7528 <code>context.succeed()<\/code>\u3001<code>context.fail()<\/code> \u548c <code>context.done()<\/code><br \/>\n\u4e0d\u904e\u8ab0\u4ed6\u5abd\u9084\u5728\u7528 Node.js v0.10<\/p>\n<p>ref:<br \/>\n<a href=\"http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/programming-model.html\">http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/programming-model.html<\/a><br \/>\n<a href=\"http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/nodejs-prog-model-handler.html\">http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/nodejs-prog-model-handler.html<\/a><br \/>\n<a href=\"http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/nodejs-prog-model-context.html\">http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/nodejs-prog-model-context.html<\/a><br \/>\n<a href=\"http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/best-practices.html\">http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/best-practices.html<\/a><\/p>\n<p>Calling another Lambda function in a Lambda function.<\/p>\n<p>\u8981\u6ce8\u610f\u7684\u662f<br \/>\n\u4f60\u7684 Lambda function \u7684 role \u5f97\u8981\u6709 invoke \u5176\u4ed6 Lambda function \u7684\u6b0a\u9650\u624d\u884c<\/p>\n<pre class=\"line-numbers\"><code class=\"language-js\">const util = require('util');\n\nconst aws = require('aws-sdk');\n\nconst params = {\n  FunctionName: 'LambdaBaku_syncIssue',\n  InvocationType: 'Event', \/\/ means asynchronous execution\n  Payload: JSON.stringify({ issue_number: curatedIssue.number }),\n};\n\nlambda.invoke(params, (err, data) =&gt; {\n  if (err) {\n    console.log('FAIL', params);\n    console.log(util.inspect(err));\n  } else {\n    console.log(data);\n  }\n});<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"http:\/\/docs.aws.amazon.com\/AWSJavaScriptSDK\/latest\/AWS\/Lambda.html\">http:\/\/docs.aws.amazon.com\/AWSJavaScriptSDK\/latest\/AWS\/Lambda.html<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/31714788\/can-an-aws-lambda-function-call-another\">http:\/\/stackoverflow.com\/questions\/31714788\/can-an-aws-lambda-function-call-another<\/a><\/p>\n<p>\u5b8c\u6574\u7684\u7a0b\u5f0f\u78bc\u653e\u5728 GitHub \u4e0a<br \/>\n<a href=\"https:\/\/github.com\/CodeTengu\/lambdabaku\">https:\/\/github.com\/CodeTengu\/lambdabaku<\/a><\/p>\n<h2>Users and Roles<\/h2>\n<p>\u5982\u679c\u4f60\u662f\u7528 apex \u4f86\u7ba1\u7406 Lambda functions \u7684\u8a71<br \/>\n\u78ba\u4fdd\u4f60\u7528\u7684 AWS credential (User) \u64c1\u6709 <code>AWSLambdaFullAccess<\/code> \u548c <code>AWSLambdaRole<\/code> \u9019\u5169\u500b permissions<\/p>\n<p>\u4ee5 project \u70ba\u55ae\u4f4d\u5efa\u7acb Role \u5373\u53ef<br \/>\n\u4f8b\u5982 <code>lambdabaku_role<\/code><br \/>\n\u4f60\u53ef\u4ee5\u5728 IAM Management Console \u627e\u5230\u90a3\u4e9b\u4f60\u5efa\u7acb\u7684 roles<br \/>\n\u57fa\u672c\u4e0a\u7528 Basic execution role \u5c31\u5920\u4e86<br \/>\n\u53cd\u6b63\u4e4b\u5f8c\u53ef\u4ee5\u96a8\u6642\u4fee\u6539 Role \u7684 permission \/ policy<br \/>\nLambda function \u5c6c\u65bc\u54ea\u500b VPC \u662f\u984d\u5916\u6307\u5b9a\u7684<br \/>\n\u8ddf Role \u6c92\u6709\u95dc\u4fc2<br \/>\n\u4e5f\u5c31\u662f\u8aaa\u4f60\u7528 Basic execution role \u9084\u662f\u53ef\u4ee5\u652f\u63f4 VPC<\/p>\n<p>\u5982\u679c\u60f3\u5728 Lambda function \u88e1\u5b58\u53d6 DynamoDB<br \/>\n\u8981\u8a18\u5f97\u5728 Role \u88e1\u65b0\u589e\u5c0d\u61c9\u7684\u8a2d\u5b9a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-json\">{\n    \"Version\": \"2012-10-17\",\n    \"Statement\": [\n        {\n            \"Sid\": \"\",\n            \"Effect\": \"Allow\",\n            \"Action\": [\n                \"logs:CreateLogGroup\",\n                \"logs:CreateLogStream\",\n                \"logs:PutLogEvents\"\n            ],\n            \"Resource\": \"*\"\n        },\n        {\n            \"Sid\": \"Stmt1428341300017\",\n            \"Effect\": \"Allow\",\n            \"Action\": [\n                \"dynamodb:*\"\n            ],\n            \"Resource\": [\n                \"arn:aws:dynamodb:ap-northeast-1:004615714446:table\/CodeTengu_Preference\",\n                \"arn:aws:dynamodb:ap-northeast-1:004615714446:table\/CodeTengu_WeeklyIssue\",\n                \"arn:aws:dynamodb:ap-northeast-1:004615714446:table\/CodeTengu_WeeklyPost\"\n            ]\n        }\n    ]\n}<\/code><\/pre>\n<h2>Scheduled Events<\/h2>\n<p>ref:<br \/>\n<a href=\"http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/with-scheduled-events.html\">http:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/with-scheduled-events.html<\/a><\/p>\n<h2>API Gateway<\/h2>\n<p>\u55ae\u7d14\u4e00\u9ede\u7684\u8a71<br \/>\nSecurity \u53ef\u4ee5\u9078 <code>Open with access key<\/code><br \/>\n\u7136\u5f8c\u5230 API Gateway \u4ecb\u9762\u7684 API Keys \u5e95\u4e0b\u65b0\u589e\u4e00\u7d44 access key<br \/>\n\u7136\u5f8c\u5206\u914d\u4e00\u500b API stage \u7d66\u5b83<\/p>\n<p>\u4f7f\u7528\u7684\u6642\u5019\u5728 HTTP header \u52a0\u4e0a <code>x-api-key: YOUR_API_KEY<\/code> \u5373\u53ef<\/p>\n<p>ref:<br \/>\n<a href=\"http:\/\/docs.aws.amazon.com\/apigateway\/latest\/developerguide\/how-to-api-keys.html\">http:\/\/docs.aws.amazon.com\/apigateway\/latest\/developerguide\/how-to-api-keys.html<\/a><\/p>\n<h2>Related Projects<\/h2>\n<p>ref:<br \/>\n<a href=\"https:\/\/github.com\/serverless\/serverless\">https:\/\/github.com\/serverless\/serverless<\/a><br \/>\n<a href=\"https:\/\/github.com\/apex\/apex\">https:\/\/github.com\/apex\/apex<\/a><br \/>\n<a href=\"https:\/\/github.com\/claudiajs\/claudia\">https:\/\/github.com\/claudiajs\/claudia<\/a><br \/>\n<a href=\"https:\/\/github.com\/garnaat\/kappa\">https:\/\/github.com\/garnaat\/kappa<\/a><br \/>\n<a href=\"https:\/\/github.com\/Miserlou\/Zappa\">https:\/\/github.com\/Miserlou\/Zappa<\/a><br \/>\n<a href=\"https:\/\/github.com\/nficano\/python-lambda\">https:\/\/github.com\/nficano\/python-lambda<\/a><\/p>\n<p>\u6dfa\u6790 serverless \u67b6\u69cb\u8207\u5be6\u4f5c<br \/>\n<a href=\"http:\/\/abalone0204.github.io\/2016\/05\/22\/serverless-simple-crud\/\">http:\/\/abalone0204.github.io\/2016\/05\/22\/serverless-simple-crud\/<\/a><\/p>\n<h3>Deploy Lambda Functions via apex<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-bash\">$ curl https:\/\/raw.githubusercontent.com\/apex\/apex\/master\/install.sh | sh\n\n$ apex deploy\n$ apex invoke syncPublishedIssues --logs\n$ echo -n '{\"issue_number\": 43}' | apex invoke syncIssue --logs<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/github.com\/apex\/apex\">https:\/\/github.com\/apex\/apex<\/a><br \/>\n<a href=\"http:\/\/apex.run\/\">http:\/\/apex.run\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>AWS Lambda is an event-driven service that you can upload your code to it and run those code on-demand without having your own servers.<\/p>\n","protected":false},"author":1,"featured_media":561,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,7,116],"tags":[16,107,124,66,11,12],"class_list":["post-234","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-about-devops","category-about-javascript","category-about-web-development","tag-amazon-web-services","tag-aws-dynamodb","tag-aws-lambda","tag-go","tag-javascript","tag-node-js"],"_links":{"self":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/comments?post=234"}],"version-history":[{"count":0,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/234\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media\/561"}],"wp:attachment":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media?parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/categories?post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/tags?post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}