This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (113 loc) · 3.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict'
const pify = require('pify')
const AWS = require('aws-sdk')
const _ = require('underscore')
const c = require('rho-cc-promise').mixin(require('rho-contracts-fork'))
const cc = {}
cc.awsRegion = require('rho-cc-aws-region')
cc.lambdaSchedulerConfig = c
.toContract({
region: cc.awsRegion,
accessKeyId: c.optional(c.string),
secretAccessKey: c.optional(c.string),
sessionToken: c.optional(c.string),
functionName: c.string.doc(
'The name of the Lambda function being scheduled'
),
ruleName: c.string.doc(
"A name for the CloudWatch Events rule, e.g. '10am'"
),
scheduleExpression: c.string.doc(
'http://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html'
),
})
.rename('lambdaSchedulerConfig')
cc.ruleArn = c.string.rename('ruleArn')
cc.lambdaScheduler = c.fun({ config: cc.lambdaSchedulerConfig }).constructs({
updateEvent: c.fun().returnsPromise(cc.ruleArn),
authorizeRule: c
.fun({ ruleArn: cc.ruleArn })
.returnsPromise(c.value(undefined)),
updateEventTarget: c.fun().returnsPromise(c.value(undefined)),
schedule: c.fun().returnsPromise(c.value(undefined)),
})
class LambdaSchedulerImpl {
constructor(config) {
const awsAttrs = _(config).pick(
'region',
'secretKeyId',
'secretAccessKey',
'secretToken'
)
this.cloudWatchEvents = new AWS.CloudWatchEvents(awsAttrs)
this.lambda = new AWS.Lambda(awsAttrs)
this.functionName = config.functionName
this.ruleName = config.ruleName
this.scheduleExpression = config.scheduleExpression
}
// Update the CloudWatch Events rule and return its ARN via promise.
updateEvent() {
const cloudWatchEvents = this.cloudWatchEvents
const listRules = pify(cloudWatchEvents.listRules.bind(cloudWatchEvents))
const putRule = pify(cloudWatchEvents.putRule.bind(cloudWatchEvents))
const ruleAttrs = {
Name: this.ruleName,
ScheduleExpression: this.scheduleExpression,
}
return listRules({}).then(rules => {
const matching = _(rules.Rules).findWhere(ruleAttrs)
if (matching) {
return matching.Arn
} else {
return putRule(ruleAttrs).then(data => data.RuleArn)
}
})
}
authorizeRule(ruleArn) {
const addPermission = pify(this.lambda.addPermission.bind(this.lambda))
const permissionAttrs = {
FunctionName: this.functionName,
StatementId: 'InvokeFromCloudWatchEvent',
Action: 'lambda:InvokeFunction',
Principal: 'events.amazonaws.com',
SourceArn: ruleArn,
}
return addPermission(permissionAttrs).catch(err => {
if (err.code !== 'ResourceConflictException') {
throw err
}
// A conflict means there is already a statement in place with
// the ID 'InvokeFromCloudWatchEvent'. We're going to assume
// this is a rule we have created during a previous deploy which
// is still in effect.
})
}
_getFunctionArn() {
const getFunction = pify(this.lambda.getFunction.bind(this.lambda))
return getFunction({ FunctionName: this.functionName }).then(
functionData => functionData.Configuration.FunctionArn
)
}
updateEventTarget() {
const cloudWatchEvents = this.cloudWatchEvents
const putTargets = pify(cloudWatchEvents.putTargets.bind(cloudWatchEvents))
return (
this._getFunctionArn()
.then(functionArn => {
const targetAttrs = {
Rule: this.ruleName,
Targets: [{ Id: '1', Arn: functionArn }],
}
return putTargets(targetAttrs)
})
// Discard spurious success return value.
.then(() => undefined)
)
}
schedule() {
return this.updateEvent()
.then(ruleArn => this.authorizeRule(ruleArn))
.then(() => this.updateEventTarget())
}
}
exports.LambdaScheduler = cc.lambdaScheduler.wrap(LambdaSchedulerImpl)