LambdaのNode.jsが使っているSDKのバージョンを確かめる

この記事は、[ハンズラボ Advent Calendar 2018](https://qiita.com/advent-calendar/2018/handslab) 3日目の記事です。

## はじめに

LambdaでNode.jsをよく使います。

LambdaのNode.jsが使っているSDKのバージョンは公式ページに記載されていますが、少し反映が遅れたりもします。
日本語ページは更に遅れたりもします。

https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html

LambdaのNode.jsが使っているSDKの正確なバージョンが何とか分からんかなと考え、Lambdaが使っているaws-sdkの `package.json` を直接見ればええじゃんという結論にいたりました。

## aws-sdkのpackage.jsonを表示する

まず、Lambdaの環境変数については公式ページに記載されています。

https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/current-supported-versions.html

その中の `LAMBDA_RUNTIME_DIR` でaws-sdkが展開されるパスが定義されています。

そのため以下のパスのファイルを参照すれば、aws-sdkの `package.json` がわかります。

```js
`${process.env.LAMBDA_RUNTIME_DIR}/node_modules/aws-sdk/package.json`
```

fsを使えばファイルの読み込みができるので、以下のプログラムを実行することで `package.json` のファイルの中身を表示することができます。

```js
    const packageFile = `${process.env.LAMBDA_RUNTIME_DIR}/node_modules/aws-sdk/package.json`;
    if (fs.existsSync(packageFile)) {
        console.log(fs.readFileSync(packageFile, 'utf-8'));
    }
```

実際に `package.json` を表示させると、こんな感じです。

```json:package.json
{  
   "_from":"aws-sdk",
   "_id":"aws-sdk@2.290.0",
   "_inBundle":false,
   "_integrity":"sha512-4AiExL06NsjrYqec/GdZP6qsquppFhaJE9hKZNw1c4ApjiGCRucfSlMvaZ6aZw76MG9b2Mi8mboGXOYW8nTGJQ==",
   "_location":"/aws-sdk",
   "_phantomChildren":{  

   },
   "_requested":{  
      "type":"tag",
      "registry":true,
      "raw":"aws-sdk",
      "name":"aws-sdk",
      "escapedName":"aws-sdk",
      "rawSpec":"",
      "saveSpec":null,
      "fetchSpec":"latest"
   },
   "_requiredBy":[  
      "#USER",
      "/"
   ],
   "_resolved":"https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.290.0.tgz",
   "_shasum":"e0e4777a62ad29df3b86521a103b3f02189a30f5",
   "_spec":"aws-sdk",
   "_where":"/workplace/lambda-deployments/src/LambdaWorkerExternalSdks",
   "author":{  
      "name":"Amazon Web Services",
      "url":"https://aws.amazon.com/"
   },
   "browser":{  
      "lib/aws.js":"./lib/browser.js",
      "fs":false,
      "./global.js":"./browser.js",
      "./lib/node_loader.js":"./lib/browser_loader.js"
   },
   "browserify":{  
      "transform":"./dist-tools/transform.js"
   },
   "bugs":{  
      "url":"https://github.com/aws/aws-sdk-js/issues"
   },
   "bundleDependencies":false,
   "contributors":[  
      {  
         "name":"Loren Segal",
         "email":"lsegal@amazon.com"
      },
      {  
         "name":"Trevor Rowe",
         "email":"trevrowe@amazon.com"
      }
   ],
   "dependencies":{  
      "buffer":"4.9.1",
      "events":"1.1.1",
      "ieee754":"1.1.8",
      "jmespath":"0.15.0",
      "querystring":"0.2.0",
      "sax":"1.2.1",
      "url":"0.10.3",
      "uuid":"3.1.0",
      "xml2js":"0.4.19"
   },
   "deprecated":false,
   "description":"AWS SDK for JavaScript",
   "devDependencies":{  
      "@types/node":"6.0.92",
      "browserify":"13.1.0",
      "chai":"^3.0",
      "codecov":"^1.0.1",
      "coffee-script":"1.6.3",
      "coffeeify":"*",
      "cucumber":"0.5.x",
      "eslint":"1.x",
      "hash-test-vectors":"^1.3.2",
      "insert-module-globals":"^7.0.0",
      "istanbul":"*",
      "jasmine":"^2.5.3",
      "jasmine-core":"^2.5.2",
      "json-loader":"^0.5.4",
      "karma":"^1.4.1",
      "karma-jasmine":"^1.1.0",
      "karma-phantomjs-launcher":"1.0.2",
      "mocha":"^3.0.0",
      "phantomjs-prebuilt":"2.1.15",
      "repl.history":"*",
      "semver":"*",
      "typescript":"2.0.8",
      "uglify-js":"2.x",
      "webpack":"^1.15.0"
   },
   "directories":{  
      "lib":"lib"
   },
   "engines":{  
      "node":">= 0.8.0"
   },
   "homepage":"https://github.com/aws/aws-sdk-js",
   "keywords":[  
      "api",
      "amazon",
      "aws",
      "ec2",
      "simpledb",
      "s3",
      "sqs",
      "ses",
      "sns",
      "route53",
      "rds",
      "elasticache",
      "cloudfront",
      "fps",
      "cloudformation",
      "cloudwatch",
      "dynamodb",
      "iam",
      "swf",
      "autoscaling",
      "cloudsearch",
      "elb",
      "loadbalancing",
      "emr",
      "mapreduce",
      "importexport",
      "storagegateway",
      "workflow",
      "ebs",
      "vpc",
      "beanstalk",
      "glacier",
      "kinesis",
      "cloudtrail",
      "waf"
   ],
   "license":"Apache-2.0",
   "main":"lib/aws.js",
   "name":"aws-sdk",
   "react-native":{  
      "fs":"./lib/empty.js",
      "./lib/node_loader.js":"./lib/react-native-loader.js",
      "./lib/browser_loader.js":"./lib/react-native-loader.js",
      "./lib/core.js":"./dist/aws-sdk-core-react-native.js",
      "xml2js":"./dist/xml2js.js"
   },
   "repository":{  
      "type":"git",
      "url":"git://github.com/aws/aws-sdk-js.git"
   },
   "scripts":{  
      "add-change":"node ./scripts/changelog/add-change.js",
      "browsertest":"rake browser:test && karma start",
      "build-react-native":"npm -s run-script build-react-native-deps && npm -s run-script build-react-native-core && npm -s run-script build-react-native-dist",
      "build-react-native-core":"webpack --config dist-tools/webpack.config.rn-core.js",
      "build-react-native-deps":"webpack --config dist-tools/webpack.config.rn-dep.js",
      "build-react-native-dist":"webpack --config dist-tools/webpack.config.rn.js",
      "build-typings":"node ./scripts/typings-generator.js",
      "buildertest":"mocha --compilers coffee:coffee-script -s 1000 -t 10000 dist-tools/test",
      "console":"./scripts/console",
      "coverage":"istanbul cover ./node_modules/mocha/bin/_mocha --reporter=lcovonly -- test test/json test/model test/protocol test/query test/services test/signers test/xml test/s3 test/cloudfront test/dynamodb test/polly test/rds test/event-stream",
      "integration":"cucumber.js",
      "lint":"eslint lib dist-tools/*.js",
      "react-native-test":"npm -s run-script build-react-native && rake reactnative:test && karma start",
      "region-check":"node ./scripts/region-checker/index.js",
      "test":"npm -s run-script lint && npm -s run-script unit && npm -s run-script buildertest && npm -s run-script browsertest && npm -s run-script react-native-test && npm -s run-script tstest && ([ -f configuration ] && npm -s run-script integration || true)",
      "testfiles":"istanbul `[ $COVERAGE ] && echo 'cover _mocha' || echo 'test mocha'`",
      "translate-api-test":"mocha scripts/lib/translate-api.spec.js",
      "tstest":"npm -s run-script build-typings && tsc -p ./ts",
      "typings-generator-test":"mocha scripts/lib/prune-shapes.spec.js",
      "unit":"mocha -- test test/json test/model test/protocol test/query test/services test/signers test/xml test/s3 test/cloudfront test/dynamodb test/polly test/rds test/event-stream"
   },
   "types":"index.d.ts",
   "typings":"index.d.ts",
   "version":"2.290.0"
}
```

最後の方に載っているversionの項目を見ることで、SDKのバージョンがわかります。
2018/12/02現在は、バージョン `2.290.0` を使用しているみたいです。

これで今Lambdaが使用している正確なaws-sdkのバージョンを知ることができます。

参考に実際にLambdaのNode.js 8.10で動かせるソースを載せておきます。

```js:Lambda
const fs = require('fs');

exports.handler = async (event) => {
    const packageFile = `${process.env.LAMBDA_RUNTIME_DIR}/node_modules/aws-sdk/package.json`;
    if (fs.existsSync(packageFile)) {
        console.log(fs.readFileSync(packageFile, 'utf-8'));
    }
}
```

[ハンズラボ Advent Calendar 2018](https://qiita.com/advent-calendar/2018/handslab) 明日4日目は、@hxrxchangです！