ソースコードから理解する技術-UnderSourceCode

手を動かす(プログラムを組む)ことで技術を理解するブログ

nodejs(Lambda用)の自分流の開発環境構築〜実行まで

私がAWS Lambdaで実行するnode.jsを開発する際の、実行環境・開発ツールのセットアップ〜実行までの手順のメモ。

改善点があればアップデートしていくつもり。

前提条件、開発環境方針

  • mac上で構築する。諸々のインストールにはnpmを使用。
  • node.jsはv6.10.0を使用。
  • Lintによるチェック、スクリプト実行はコマンドで行う。

プロジェクト作成

参考サイト

作成したサンプルソース

  • main.js(メイン処理)
exports.handler = (event, context, callback) => {
  // TODO implement
  console.log("main called!");
  callback(null, "Hello from Lambda");
};
    • driver.js(main.jsをローカル実行時に呼ぶドライバー)
var event = {
  "Records": [
    {
      "awsRegion": "ap-northeast-1",
      "sequenceNumber": "xxxxxx",
      "partitionKey": "xxxxxx",
      "eventSource": "aws:s3",
      "data": "xxxxxx"
    }
  ]
};

var context = {
  invokeid: "invokeid",
  done: function(err, message){
    console.log(message);
    return;
  }
};

var lambda = require("./main");
lambda.handler(event, context, function(error, result){
  console.log("main callbacked!");
  console.log(error);
  console.log(result);
});

実行環境構築手順

ESLint構築手順

  • $ npm install eslint
    • プロジェクトのローカルにESLintをインストール。
  • $ ./node_modules/.bin/eslint -v
  • $ ./node_modules/.bin/eslint —init
    • 案内に従いESLintの設定を作る。
? How would you like to configure ESLint? Answer questions about your style
? Are you using ECMAScript 6 features? Yes
? Are you using ES6 modules? No
? Where will your code run? Node
? Do you use JSX? No
? What style of indentation do you use? Spaces
? What quotes do you use for strings? Double
? What line endings do you use? Unix
? Do you require semicolons? Yes
? What format do you want your config file to be in? YAML
  • esllintにconsole.logを許可。
    • .eslintrc.ymlに以下を追加。
    • 「no-console: 0」

実行手順

  • ESLint
    • $ ./node_modules/.bin/eslint *.js