Skip to content

Commit

Permalink
Merge pull request #17 from nasa/CUMULUS-953
Browse files Browse the repository at this point in the history
Allow lambdaArn to be specified as an arn or a function name
  • Loading branch information
Marc authored Oct 10, 2018
2 parents c07eeef + 7dd0fc6 commit 99204ae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 24 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 99204ae

Please sign in to comment.