Skip to content

Instantly share code, notes, and snippets.

@goldeneggg
Last active December 7, 2018 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldeneggg/4099bc80eb0208ade693d4080b05272b to your computer and use it in GitHub Desktop.
Save goldeneggg/4099bc80eb0208ade693d4080b05272b to your computer and use it in GitHub Desktop.
AWS Lambdaの言語サポート状況を取得する為のLambda Function
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;
}
@goldeneggg
Copy link
Author

goldeneggg commented Dec 7, 2018

実行結果例。 bodyresult に 「言語名:[対応バージョン一覧配列]」という形式で出力

{"statusCode":200,"body":"{\"result\":{\"node\":[\"Node.js 8.10\",\"Node.js 6.10\"],\"java\":[\"Java 8\"],\"python\":[\"Python 3.6\",\"Python 3.7\",\"Python 2.7\"],\"go\":[\"Go 1.x\"],\"dotnet\":[\".NET Core 2.1\",\".NET Core 2.0\",\".NET Core 1.0\"]}}"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment