Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow lambdaArn to be specified as an arn or a function name #17

Merged
merged 4 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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