Skip to content

Commit

Permalink
feat: Support sls default custom mapping template
Browse files Browse the repository at this point in the history
Adds support for the serverless framework legacy `lambda`
integration which uses custom-mapping templates instead of
the now-default lambda-proxy.

This input type uses the ApiGateway event type fields as
the meaning is similar, although only a subset of those
fields are supported by this event type.

Signed-off-by: Erica Windisch <73207+ewindisch@users.noreply.github.com>
Co-authored-by: Corey Light <coreylight@gmail.com>
  • Loading branch information
ewindisch and coreylight committed Mar 7, 2018
1 parent b34551a commit 2033c3e
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ Object {
}
`;

exports[`slsIntegrationLambda reports snapshot 1`] = `
Object {
"@iopipe/event-info.apiGateway.headers.X-Amz-Cf-Id": "l06CAg2QsrALeQcLAUSxGXbm8lgMoMIhR2AjKa4AiKuaVnnGsOFy5g==",
"@iopipe/event-info.apiGateway.headers.X-Amzn-Trace-Id": "Root=1-5970ef3e249c0321b2eef14aa513ae",
"@iopipe/event-info.apiGateway.httpMethod": "GET",
"@iopipe/event-info.apiGateway.requestContext.accountId": "1234",
"@iopipe/event-info.apiGateway.requestContext.httpMethod": "GET",
"@iopipe/event-info.apiGateway.requestContext.identity.userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
"@iopipe/event-info.apiGateway.requestContext.stage": "dev",
"@iopipe/event-info.eventType": "apiGateway",
"@iopipe/event-info.eventType.source": "slsIntegrationLambda",
}
`;

exports[`sns reports snapshot 1`] = `
Object {
"@iopipe/event-info.eventType": "sns",
Expand Down
12 changes: 11 additions & 1 deletion src/eventSamples/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import apiGateway from './apiGateway';
import slsIntegrationLambda from './slsIntegrationLambda';
import cloudFront from './cloudFront';
import firehose from './firehose';
import kinesis from './kinesis';
import s3 from './s3';
import scheduled from './scheduled';
import sns from './sns';

export { apiGateway, cloudFront, firehose, kinesis, s3, scheduled, sns };
export {
apiGateway,
slsIntegrationLambda,
cloudFront,
firehose,
kinesis,
s3,
scheduled,
sns
};
50 changes: 50 additions & 0 deletions src/eventSamples/slsIntegrationLambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export default {
body: {},
method: 'GET',
principalId: '1234',
stage: 'dev',
cognitoPoolClaims: {
sub: ''
},
enhancedAuthContext: {},
headers: {
Accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-GB,en-US;q=0.8,en;q=0.6,zh-CN;q=0.4',
'CloudFront-Forwarded-Proto': 'https',
'CloudFront-Is-Desktop-Viewer': 'true',
'CloudFront-Is-Mobile-Viewer': 'false',
'CloudFront-Is-SmartTV-Viewer': 'false',
'CloudFront-Is-Tablet-Viewer': 'false',
'CloudFront-Viewer-Country': 'GB',
Host: 'ec5ycylws8.execute-api.us-east-1.amazonaws.com',
'upgrade-insecure-requests': '1',
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
Via: '2.0 f165ce34daf8c0da182681179e863c24.cloudfront.net (CloudFront)',
'X-Amz-Cf-Id': 'l06CAg2QsrALeQcLAUSxGXbm8lgMoMIhR2AjKa4AiKuaVnnGsOFy5g==',
'X-Amzn-Trace-Id': 'Root=1-5970ef3e249c0321b2eef14aa513ae',
'X-Forwarded-For': '94.117.120.169, 116.132.62.73',
'X-Forwarded-Port': '443',
'X-Forwarded-Proto': 'https'
},
query: {},
path: {},
identity: {
cognitoIdentityPoolId: '',
accountId: '1234',
cognitoIdentityId: '',
caller: '',
apiKey: '',
sourceIp: '94.197.120.169',
accessKey: '',
cognitoAuthenticationType: '',
cognitoAuthenticationProvider: '',
userArn: '',
userAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
user: ''
},
stageVariables: {}
};
12 changes: 11 additions & 1 deletion src/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import * as apiGateway from './apiGateway';
import * as slsIntegrationLambda from './slsIntegrationLambda';
import * as cloudFront from './cloudFront';
import * as firehose from './firehose';
import * as kinesis from './kinesis';
import * as s3 from './s3';
import * as scheduled from './scheduled';
import * as sns from './sns';

export { apiGateway, cloudFront, firehose, kinesis, s3, scheduled, sns };
export {
apiGateway,
slsIntegrationLambda,
cloudFront,
firehose,
kinesis,
s3,
scheduled,
sns
};
54 changes: 54 additions & 0 deletions src/plugins/slsIntegrationLambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import get from 'lodash.get';

import logFromKeys from '../util/logFromKeys';
import { pluginName } from '../util/constants';

/* This module supports the "lambda" type integration
for the serverless framework which provides a
default custom mapping template.
The sls mapping template is described here:
https://serverless.com/framework/docs/providers/aws/events/apigateway/#example-lambda-event-before-customization
*/

const type = 'apiGateway';
const source = 'slsIntegrationLambda';

const keys = ['body', 'method', 'principalId', 'stage'];

const keysThatNeedValues = [
'identity.userAgent',
'identity.sourceIp',
'identity.accountId'
];

function eventType(event) {
if (typeof event === 'object') {
const keysArePresent = keys.every(s => s in event);
const valuesArePresent =
keysThatNeedValues
.map(k => {
return typeof get(event, k) !== 'undefined';
})
.filter(Boolean).length === keysThatNeedValues.length;
return keysArePresent && valuesArePresent ? source : false;
}
return false;
}

const pluginKeyMapping = [
['method', 'httpMethod'],
['identity.accountId', 'requestContext.accountId'],
['method', 'requestContext.httpMethod'],
['identity.userAgent', 'requestContext.identity.userAgent'],
['stage', 'requestContext.stage'],
'headers.X-Amz-Cf-Id',
'headers.X-Amzn-Trace-Id'
];

function plugin(event, log) {
logFromKeys({ event, type, keys: pluginKeyMapping, log });
log(`${pluginName}.eventType.source`, source);
}

export { eventType, plugin };
4 changes: 4 additions & 0 deletions src/util/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*eslint-disable import/prefer-default-export*/
const pluginName = '@iopipe/event-info';

export { pluginName };
2 changes: 2 additions & 0 deletions src/util/eventType.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { eventType as firehose } from '../plugins/firehose';
import { eventType as kinesis } from '../plugins/kinesis';
import { eventType as s3 } from '../plugins/s3';
import { eventType as scheduled } from '../plugins/scheduled';
import { eventType as slsIntegrationLambda } from '../plugins/slsIntegrationLambda';
import { eventType as sns } from '../plugins/sns';

// e === original lambda event
Expand All @@ -15,6 +16,7 @@ export default function(e) {
kinesis(e) ||
s3(e) ||
scheduled(e) ||
slsIntegrationLambda(e) ||
sns(e)
);
}
2 changes: 1 addition & 1 deletion src/util/logFromKeys.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import get from 'lodash.get';

const pluginName = '@iopipe/event-info';
import { pluginName } from './constants';

export default function logFromWantedKeys({
event = {},
Expand Down

0 comments on commit 2033c3e

Please sign in to comment.