Last active
December 7, 2018 03:27
-
-
Save goldeneggg/4099bc80eb0208ade693d4080b05272b to your computer and use it in GitHub Desktop.
AWS Lambdaの言語サポート状況を取得する為のLambda Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const cheerio = require('cheerio'); | |
const url = 'https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/lambda-runtimes.html'; | |
let $; | |
let response; | |
/** | |
* | |
* Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format | |
* @param {Object} event - API Gateway Lambda Proxy Input Format | |
* @param {string} event.resource - Resource path. | |
* @param {string} event.path - Path parameter. | |
* @param {string} event.httpMethod - Incoming request's method name. | |
* @param {Object} event.headers - Incoming request headers. | |
* @param {Object} event.queryStringParameters - query string parameters. | |
* @param {Object} event.pathParameters - path parameters. | |
* @param {Object} event.stageVariables - Applicable stage variables. | |
* @param {Object} event.requestContext - Request context, including authorizer-returned key-value pairs, requestId, sourceIp, etc. | |
* @param {Object} event.body - A JSON string of the request payload. | |
* @param {boolean} event.body.isBase64Encoded - A boolean flag to indicate if the applicable request payload is Base64-encode | |
* | |
* Context doc: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html | |
* @param {Object} context | |
* @param {string} context.logGroupName - Cloudwatch Log Group name | |
* @param {string} context.logStreamName - Cloudwatch Log stream name. | |
* @param {string} context.functionName - Lambda function name. | |
* @param {string} context.memoryLimitInMB - Function memory. | |
* @param {string} context.functionVersion - Function version identifier. | |
* @param {function} context.getRemainingTimeInMillis - Time in milliseconds before function times out. | |
* @param {string} context.awsRequestId - Lambda request ID. | |
* @param {string} context.invokedFunctionArn - Function ARN. | |
* | |
* Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html | |
* @returns {Object} object - API Gateway Lambda Proxy Output Format | |
* @returns {boolean} object.isBase64Encoded - A boolean flag to indicate if the applicable payload is Base64-encode (binary support) | |
* @returns {string} object.statusCode - HTTP Status Code to be returned to the client | |
* @returns {Object} object.headers - HTTP Headers to be returned | |
* @returns {Object} object.body - JSON Payload to be returned | |
* | |
*/ | |
exports.lambdaHandler = async (event, context) => { | |
try { | |
const res = await axios(url); | |
verInfos = getVerInfos(res.data); | |
response = { | |
'statusCode': 200, | |
'body': JSON.stringify({ | |
result: verInfos | |
}) | |
} | |
} catch (err) { | |
console.log(err); | |
return err; | |
} | |
return response | |
}; | |
function getVerInfos(html) { | |
$ = cheerio.load(html); | |
const h = {}; | |
h.node = getLangInfos('w370aac27c17'); | |
h.java = getLangInfos('w370aac27c21'); | |
h.python = getLangInfos('w370aac27c25'); | |
h.go = getLangInfos('w370aac27c29'); | |
h.dotnet = getLangInfos('w370aac27c33'); | |
return h; | |
} | |
function getLangInfos(id) { | |
const infos = []; | |
const colCount = $("#" + id + " > tbody > tr > th").length; | |
const elems = $('#' + id + ' > tbody > tr > td'); | |
elems.each(function(i, elem) { | |
if (i % colCount == 0) { | |
infos.push($(elem).text().trim()); | |
} | |
}); | |
return infos; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実行結果例。
body
→result
に 「言語名:[対応バージョン一覧配列]
」という形式で出力