Skip to content

Commit d33e1a6

Browse files
committed
Bypass lambda execution when exepection warmer event
1 parent fc94248 commit d33e1a6

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

src/utils/run-handler-with-middleware.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import composeMiddleware from './compose-middleware'
2+
import isCloudwatchTrigger from './is-cloudwatch-trigger'
23
import PluginHook from '../enums/hooks'
34

45
export default function runHandlerWithMiddleware (fn, cb, responseObject, registeredPlugins = [], options = {}) {
@@ -48,6 +49,12 @@ export default function runHandlerWithMiddleware (fn, cb, responseObject, regist
4849
})
4950
}
5051

52+
// Skip the invocation if we expecting the event to be a cloudwatch trigger used for
53+
// warming the lambda
54+
if ((options.allowWarming || false) && isCloudwatchTrigger(event)) {
55+
return Promise.resolve(cb(null, null, callback))
56+
}
57+
5158
// Execute the middleware stack using the above request and response
5259
return Promise.resolve()
5360
.then(() => runInit())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa",
3+
"detail-type": "Scheduled Event",
4+
"source": "aws.events",
5+
"account": "123456789012",
6+
"time": "2015-10-08T16:53:06Z",
7+
"region": "us-east-1",
8+
"resources": [ "arn:aws:events:us-east-1:123456789012:rule/MyScheduledRule" ],
9+
"detail": {}
10+
}

test/unit/handlers/api-gateway.spec.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-unused-expressions */
22
import { should, expect } from 'chai'
3-
import { ApiGateway, AbstractLambdaPlugin, Enums } from '../../../src/index.js'
3+
import { ApiGateway, AbstractLambdaPlugin, Enums } from '../../../src'
4+
import SampleCloudwatchEvent from '../../support/sample-cloudwatch-event'
45

56
describe('api-gateway decorator', () => {
67
before(() => {
@@ -86,5 +87,23 @@ describe('api-gateway decorator', () => {
8687
done()
8788
})
8889
})
90+
91+
it('should not execute anything if the lambda allows to be warmed and a cloudwatch schedule event invokes it', (done) => {
92+
class Test {
93+
@ApiGateway({ allowWarming: true })
94+
testMethod (event) {
95+
return Promise.resolve(event.test)
96+
}
97+
}
98+
99+
const test = new Test()
100+
101+
test.testMethod(SampleCloudwatchEvent, null, (err, res) => {
102+
expect(err).to.be.null()
103+
expect(res).to.be.null()
104+
105+
done()
106+
})
107+
})
89108
})
90109
})

test/unit/utils/is-cloudwatch-trigger.spec.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
import isCloudwatchTrigger from '../../../src/utils/is-cloudwatch-trigger.js'
2-
3-
const sampleCloudwatchEvent = {
4-
id: '53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa',
5-
'detail-type': 'Scheduled Event',
6-
source: 'aws.events',
7-
account: '123456789012',
8-
time: '2015-10-08T16:53:06Z',
9-
region: 'us-east-1',
10-
resources: [ 'arn:aws:events:us-east-1:123456789012:rule/MyScheduledRule' ],
11-
detail: {}
12-
}
1+
import isCloudwatchTrigger from '../../../src/utils/is-cloudwatch-trigger'
2+
import sampleCloudwatchEvent from '../../support/sample-cloudwatch-event'
133

144
describe('isCloudwatchTrigger', () => {
155
it('should return true for a cloudwatch event', () => {

0 commit comments

Comments
 (0)