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/index.js b/index.js index d57b68a..7b1bd1e 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,24 @@ 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]; + } + + return lambdaId; +} + +// eslint-disable-next-line require-jsdoc +const getLogSenderFromLambdaId = (lambdaId) => + `cumulus-ecs-task/${getFunctionName(lambdaId)}`; + /** * Download a URL to file * @@ -253,14 +273,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 = getLogSenderFromLambdaId(lambdaArn); // the cumulus-message-adapter dir is in an unexpected place, // so tell the adapter where to find it @@ -308,14 +321,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 = getLogSenderFromLambdaId(lambdaArn); // the cumulus-message-adapter dir is in an unexpected place, // so tell the adapter where to find it @@ -391,14 +397,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 = getLogSenderFromLambdaId(lambdaArn); // the cumulus-message-adapter dir is in an unexpected place, // so tell the adapter where to find it 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": {