{"id":12695,"date":"2016-12-08T09:00:02","date_gmt":"2016-12-08T00:00:02","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=12695"},"modified":"2018-11-14T16:33:44","modified_gmt":"2018-11-14T07:33:44","slug":"how-does-golang-run-on-apex","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2016\/12\/08\/how-does-golang-run-on-apex\/","title":{"rendered":"Apex \u3067 Go \u306f\u3069\u3046\u5b9f\u884c\u3055\u308c\u3066\u3044\u308b\u306e\u304b"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u5c0f\u91ce\u5bfa\u3067\u3059\u3002<br \/>\n\u3053\u308c\u306f\u3001 <a href=\"\/blog\/2016\/11\/30\/techscore-advent-calendar-2016\/\">TECHSCORE Advent Calendar 2016<\/a> \u306e8\u65e5\u76ee\u306e\u8a18\u4e8b\u3067\u3059\u3002<\/p>\n<p>\u5148\u65e5\u306eAWS re:Invent 2016\u306e\u767a\u8868\u3067 AWS Lambda \u3067\u4f7f\u7528\u3067\u304d\u308b\u8a00\u8a9e\u306b\u3001\u3053\u308c\u307e\u3067\u306eJava\u3001Python\u3001Node.js\u306b\u52a0\u3048\u3066C#\u304c\u8ffd\u52a0\u3055\u308c\u307e\u3057\u305f\u306d\u3002<\/p>\n<p><a href=\"http:\/\/apex.run\/\" target=\"_blank\">Apex<\/a> \u3068\u3044\u3046LambdaFunction\u3092\u7c21\u5358\u306b\u69cb\u7bc9\u30fb\u5c55\u958b\u30fb\u7ba1\u7406\u3067\u304d\u308b\u30c4\u30fc\u30eb\u3092\u4f7f\u3046\u3068<br \/>\n\u6b63\u5f0f\u306b\u306f\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u306a\u3044Go\u3092Node.js\u3092\u4ecb\u3057\u3066Lambda\u3067\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u4eca\u56de\u306fApex\u304c\u3069\u306e\u3088\u3046\u306bGo\u3092Lambda\u3067\u5b9f\u884c\u3057\u3066\u3044\u308b\u306e\u304b\u898b\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<h2>Apex\u3067Go\u3092Lambda\u306b\u30c7\u30d7\u30ed\u30a4<\/h2>\n<p>\u307e\u305a\u3001Apex\u306b\u3042\u308bGo\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u53c2\u8003\u306bLambda\u306b\u30c7\u30d7\u30ed\u30a4\u3057\u307e\u3059\u3002<br \/>\n\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f<a href=\"https:\/\/github.com\/apex\/go-apex\" target=\"_blank\">\u3053\u3061\u3089<\/a>\u304b\u3089\u3054\u89a7\u304f\u3060\u3055\u3044\u3002(Apex\u306e\u8a2d\u5b9a\u306f\u5272\u611b\u3057\u307e\u3059)<\/p>\n<p>apex deploy \u3059\u308b\u3068\u3001Apex\u306e\u5185\u90e8\u30d1\u30c3\u30b1\u30fc\u30b8\u3067\u3042\u308b<a href=\"https:\/\/github.com\/apex\/apex\/tree\/master\/shim\" target=\"_blank\">shim<\/a>\u306b\u3088\u3063\u3066<br \/>\nLambda\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30d5\u30a1\u30a4\u30eb\u3092\u5c55\u958b\u3057\u307e\u3059\u3002  <\/p>\n<pre>byline.js\r\nindex.js\r\nmain<\/pre>\n<h2>Apex \u3067\u751f\u6210\u3055\u308c\u305f\u30b3\u30fc\u30c9\u3092\u8aad\u3080<\/h2>\n<p>\u5c55\u958b\u3055\u308c\u305f\u30b3\u30fc\u30c9\u3092\u8aad\u3093\u3067\u307f\u307e\u3057\u3087\u3046\u3002<br \/>\nindex.js \u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5185\u5bb9\u3067\u3059\u3002<\/p>\n<pre>var child = require('child_process')\r\nvar byline = require('.\/byline')\r\n\r\n\/**\r\n * Context for the request.\r\n *\/\r\n\r\nvar ctx\r\n\r\n\/**\r\n * Child process for binary I\/O.\r\n *\/\r\n\r\nvar proc = child.spawn('.\/main', { stdio: ['pipe', 'pipe', process.stderr] })\r\n\r\nproc.on('error', function(err){\r\n  console.error('error: %s', err)\r\n  process.exit(1)\r\n})\r\n\r\nproc.on('exit', function(code){\r\n  console.error('exit: %s', code)\r\n  process.exit(1)\r\n})\r\n\r\n\/**\r\n * Newline-delimited JSON stdout.\r\n *\/\r\n\r\nvar out = byline(proc.stdout)\r\n\r\nout.on('data', function(line){\r\n  if (process.env.DEBUG_SHIM) console.log('[shim] parsing: %j', line)\r\n  var msg = JSON.parse(line)\r\n  ctx.done(msg.error, msg.value)\r\n})\r\n\r\n\/**\r\n * Handle events.\r\n *\/\r\n\r\nexports.handle = function(event, context) {\r\n  ctx = context\r\n\r\n  proc.stdin.write(JSON.stringify({\r\n    \"event\": event,\r\n    \"context\": context\r\n  })+'\\n');\r\n}\r\n<\/pre>\n<p>index.js \u3092\u7d30\u304b\u304f\u898b\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<pre>\r\nvar child = require('child_process')\r\nvar byline = require('.\/byline')\r\n<\/pre>\n<p>index.js\u3067\u306f2\u3064\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u4f7f\u308f\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre>\r\nvar proc = child.spawn('.\/main', { stdio: ['pipe', 'pipe', process.stderr] })\r\n<\/pre>\n<p>child_process.spawn(command, [args], [options]) \u3067\u5b50\u30d7\u30ed\u30bb\u30b9\u3092\u4f5c\u6210\u3057\u3066\u3044\u307e\u3059\u3002  <\/p>\n<p><strong>command<\/strong><\/p>\n<p>\u5b9f\u884c\u3059\u308b\u30b3\u30de\u30f3\u30c9\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<br \/>\n.\/main \u306f Go \u3067\u4f5c\u6210\u3057\u305f main.go \u306e\u30d3\u30eb\u30c9\u6e08\u307f\u30d0\u30a4\u30ca\u30ea\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<p><strong>args<\/strong><\/p>\n<p>\u30b3\u30de\u30f3\u30c9\u306e\u5f15\u6570\u3092\u6307\u5b9a\u3067\u304d\u307e\u3059\u304c\u3001\u4eca\u56de\u306f\u7701\u7565\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><strong>options<\/strong><\/p>\n<p>stdio\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u5b50\u30d7\u30ed\u30bb\u30b9\u306e\u5165\u51fa\u529b\u8a2d\u5b9a\u3092\u3057\u3066\u3044\u307e\u3059\u3002\u305d\u308c\u305e\u308c\u3001<\/p>\n<pre>[ChildProcess.stdin\u3001ChildProcess.stdout\u3001 ChildProcess.stderr]<\/pre>\n<p>\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u3066\u3001\u6a19\u6e96\u5165\u529b\u3001\u6a19\u6e96\u51fa\u529b\u306f\u30d1\u30a4\u30d7\u306b\u3001\u6a19\u6e96\u30a8\u30e9\u30fc\u51fa\u529b\u306f\u89aa\u30d7\u30ed\u30bb\u30b9\u3068\u5171\u6709\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre>\r\nproc.on('error', function(err){\r\n  console.error('error: %s', err)\r\n  process.exit(1)\r\n})\r\n\r\nproc.on('exit', function(code){\r\n  console.error('exit: %s', code)\r\n  process.exit(1)\r\n})\r\n<\/pre>\n<p>\u307e\u305f\u3001\u5b50\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3057\u305f\u5834\u5408\u3001\u3082\u3057\u304f\u306f\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306b\u6a19\u6e96\u30a8\u30e9\u30fc\u51fa\u529b\u3092\u3057\u3066\u7d42\u4e86\u3057\u307e\u3059\u3002<\/p>\n<pre>\r\nvar out = byline(proc.stdout)\r\n\r\nout.on('data', function(line){\r\n  if (process.env.DEBUG_SHIM) console.log('[shim] parsing: %j', line)\r\n  var msg = JSON.parse(line)\r\n  ctx.done(msg.error, msg.value)\r\n})\r\n<\/pre>\n<p><a href=\"https:\/\/github.com\/jahewson\/node-byline\" target=\"_blank\">byline<\/a>\u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u3088\u3063\u3066\u3001\u5b50\u30d7\u30ed\u30bb\u30b9\u304b\u3089\u306e\u6a19\u6e96\u51fa\u529b\u30921\u884c\u305a\u3064\u51e6\u7406\u3057\u307e\u3059\u3002<br \/>\nctx.done()\u3067\u3001\u30ec\u30b9\u30dd\u30f3\u30b9(\u30a8\u30e9\u30fc\u304c\u3042\u308c\u3070\u30a8\u30e9\u30fc\u30ec\u30b9\u30dd\u30f3\u30b9)\u3092\u8fd4\u3057\u3066\u51e6\u7406\u3092\u7d42\u4e86\u3057\u307e\u3059\u3002<\/p>\n<pre>\r\nexports.handle = function(event, context) {\r\n  ctx = context\r\n\r\n  proc.stdin.write(JSON.stringify({\r\n    \"event\": event,\r\n    \"context\": context\r\n  })+'\\n');\r\n}<\/pre>\n<p>\u3053\u306e\u30cf\u30f3\u30c9\u30e9\u304cLambda\u3067\u5b9f\u884c\u3055\u308c\u308b\u95a2\u6570\u3067\u3059\u3002<br \/>\nevent \u3068 context \u3092\u5b50\u30d7\u30ed\u30bb\u30b9\u306e\u6a19\u6e96\u5165\u529b\u306b\u6e21\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>\u304a\u308f\u308a\u306b<\/h2>\n<p>Node.js\u3068Go\u304c\u6a19\u6e96\u5165\u51fa\u529b\u30d1\u30a4\u30d7\u3067\u3084\u308a\u53d6\u308a\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3057\u305f\u3002<br \/>\n\u4eca\u56de\u3001Go\u8a00\u8a9e\u306e\u8a18\u4e8b\u3092\u66f8\u3053\u3046\u3068\u601d\u3063\u3066\u3044\u305f\u306e\u3067\u3059\u304cNode.js\u306e\u30b3\u30fc\u30c9\u30ea\u30fc\u30c7\u30a3\u30f3\u30b0\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u307e\u3060Lambda\u3082Apex\u3082\u4f7f\u3044\u59cb\u3081\u305f\u3070\u304b\u308a\u306a\u306e\u3067\u3001\u3053\u308c\u304b\u3089\u8272\u3005\u8a66\u3057\u3066\u3044\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2>\u53c2\u8003\u30ea\u30f3\u30af<\/h2>\n<ul>\n<li><a href=\"https:\/\/nodejs.org\/docs\/v0.10.42\/api\/child_process.html\" target=\"_blank\">Node.js v0.10.42 Manual & Documentation<\/a><\/li>\n<li><a href=\"https:\/\/docs.aws.amazon.com\/ja_jp\/lambda\/latest\/dg\/nodejs-prog-model-using-old-runtime.html\" target=\"_blank\">\u4ee5\u524d\u306e Node.js \u30e9\u30f3\u30bf\u30a4\u30e0 v0.10.42 \u3092\u4f7f\u7528\u3059\u308b<\/a><\/li>\n<li><a href=\"http:\/\/qiita.com\/kinzal\/items\/ca3b8a104847c684a249\" target=\"_blank\">\u65b0\u3057\u3044Lambda function\u306econtext\u3092\u8abf\u3079\u308b<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u5c0f\u91ce\u5bfa\u3067\u3059\u3002<br \/>\n\u3053\u308c\u306f\u3001 TECHSCORE Advent Calendar 2016 \u306e8\u65e5\u76ee\u306e\u8a18\u4e8b\u3067\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2016\/12\/08\/how-does-golang-run-on-apex\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[239,18],"tags":[141,119,156,46],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/12695"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=12695"}],"version-history":[{"count":21,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/12695\/revisions"}],"predecessor-version":[{"id":12779,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/12695\/revisions\/12779"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=12695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=12695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=12695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}