From a1f1821bf6d481062bb7c1b7e9941a6c70e1502c Mon Sep 17 00:00:00 2001 From: mhuffnagle Date: Wed, 10 Oct 2018 12:34:13 -0400 Subject: [PATCH 1/4] Allow lambdaArn to be specified as an arn or a function name --- index.js | 46 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index d57b68a..b731436 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ +/* eslint max-len: "off" */ + 'use strict'; -/* eslint-disable no-console, max-len */ + const https = require('https'); const path = require('path'); const execSync = require('child_process').execSync; @@ -15,6 +17,21 @@ const log = new Logger(); const region = process.env.AWS_DEFAULT_REGION || 'us-east-1'; AWS.config.update({ region: region }); +// eslint-disable-next-line require-jsdoc +const isLambdaFunctionArn = (id) => id.startsWith('arn:aws:lambda'); + +// eslint-disable-next-line require-jsdoc +function getFunctionName(lambdaId) { + if (isLambdaFunctionArn(lambdaId)) { + const FUNCTION_NAME_FIELD = 6; + + return lambdaId.split(':')[FUNCTION_NAME_FIELD]; + } + else { + return lambdaId; + } +} + /** * Download a URL to file * @@ -253,14 +270,7 @@ async function runTask(options) { const taskDir = options.taskDirectory; const workDir = options.workDirectory; - const lambdaFunctionNameMatch = lambdaArn.match(/:function:([^:]+)/); - if (lambdaFunctionNameMatch) { - log.sender = `cumulus-ecs-task/${lambdaFunctionNameMatch[1]}`; - } - else { - const err = new TypeError(`Unable to determine lambda function name from ${lambdaArn}`); - log.error(`Unable to determine lambda function name from ${lambdaArn}`, err); - } + log.sender = `cumulus-ecs-task/${getFunctionName(lambdaArn)}`; // the cumulus-message-adapter dir is in an unexpected place, // so tell the adapter where to find it @@ -308,14 +318,7 @@ async function runServiceFromSQS(options) { const workDir = options.workDirectory; const runForever = options.runForever || true; - const lambdaFunctionNameMatch = lambdaArn.match(/:function:([^:]+)/); - if (lambdaFunctionNameMatch) { - log.sender = `cumulus-ecs-task/${lambdaFunctionNameMatch[1]}`; - } - else { - const err = new TypeError(`Unable to determine lambda function name from ${lambdaArn}`); - log.error(`Unable to determine lambda function name from ${lambdaArn}`, err); - } + log.sender = `cumulus-ecs-task/${getFunctionName(lambdaArn)}`; // the cumulus-message-adapter dir is in an unexpected place, // so tell the adapter where to find it @@ -391,14 +394,7 @@ async function runServiceFromActivity(options) { let runForever = true; - const lambdaFunctionNameMatch = lambdaArn.match(/:function:([^:]+)/); - if (lambdaFunctionNameMatch) { - log.sender = `cumulus-ecs-task/${lambdaFunctionNameMatch[1]}`; - } - else { - const err = new TypeError(`Unable to determine lambda function name from ${lambdaArn}`); - log.error(`Unable to determine lambda function name from ${lambdaArn}`, err); - } + log.sender = `cumulus-ecs-task/${getFunctionName(lambdaArn)}`; // the cumulus-message-adapter dir is in an unexpected place, // so tell the adapter where to find it From 7530cda0e42842e30ff77bcfe7db0ba7de59d491 Mon Sep 17 00:00:00 2001 From: mhuffnagle Date: Wed, 10 Oct 2018 12:48:53 -0400 Subject: [PATCH 2/4] Fix linter warning --- index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index b731436..84cde92 100644 --- a/index.js +++ b/index.js @@ -27,9 +27,8 @@ function getFunctionName(lambdaId) { return lambdaId.split(':')[FUNCTION_NAME_FIELD]; } - else { - return lambdaId; - } + + return lambdaId; } /** From 31324d78bc0900484c4250b612a9f29161ace072 Mon Sep 17 00:00:00 2001 From: mhuffnagle Date: Wed, 10 Oct 2018 13:12:08 -0400 Subject: [PATCH 3/4] Add release info --- CHANGELOG.md | 6 ++++++ package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02f0500..cc38b3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p ## [Unreleased] +## [v1.2.4] + +### Fixed +- **CUMULUS-953** - The lambdaArn parameter will accept a Lambda ARN or a Lambda + function name. + ## [v1.2.3] ### Fixed diff --git a/package.json b/package.json index 5793b0c..6ab1ea6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cumulus/cumulus-ecs-task", - "version": "1.2.3", + "version": "1.2.4", "description": "Run lambda functions in ECS", "main": "index.js", "bin": { From 7dd0fc6953688bf213b7f32c05848248484487e5 Mon Sep 17 00:00:00 2001 From: mhuffnagle Date: Wed, 10 Oct 2018 13:20:22 -0400 Subject: [PATCH 4/4] Extract log sender building into a function --- index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 84cde92..7b1bd1e 100644 --- a/index.js +++ b/index.js @@ -31,6 +31,10 @@ function getFunctionName(lambdaId) { return lambdaId; } +// eslint-disable-next-line require-jsdoc +const getLogSenderFromLambdaId = (lambdaId) => + `cumulus-ecs-task/${getFunctionName(lambdaId)}`; + /** * Download a URL to file * @@ -269,7 +273,7 @@ async function runTask(options) { const taskDir = options.taskDirectory; const workDir = options.workDirectory; - log.sender = `cumulus-ecs-task/${getFunctionName(lambdaArn)}`; + log.sender = getLogSenderFromLambdaId(lambdaArn); // the cumulus-message-adapter dir is in an unexpected place, // so tell the adapter where to find it @@ -317,7 +321,7 @@ async function runServiceFromSQS(options) { const workDir = options.workDirectory; const runForever = options.runForever || true; - log.sender = `cumulus-ecs-task/${getFunctionName(lambdaArn)}`; + log.sender = getLogSenderFromLambdaId(lambdaArn); // the cumulus-message-adapter dir is in an unexpected place, // so tell the adapter where to find it @@ -393,7 +397,7 @@ async function runServiceFromActivity(options) { let runForever = true; - log.sender = `cumulus-ecs-task/${getFunctionName(lambdaArn)}`; + log.sender = getLogSenderFromLambdaId(lambdaArn); // the cumulus-message-adapter dir is in an unexpected place, // so tell the adapter where to find it