/
index.js
91 lines (78 loc) · 2.96 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
'use strict';
const _ = require('lodash');
class AwsCompileAlexaSkillEvents {
constructor(serverless) {
this.serverless = serverless;
this.provider = this.serverless.getProvider('aws');
this.hooks = {
'package:compileEvents': this.compileAlexaSkillEvents.bind(this),
};
this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'alexaSkill', {
anyOf: [
{ $ref: '#/definitions/awsAlexaEventToken' },
{
type: 'object',
properties: {
appId: { $ref: '#/definitions/awsAlexaEventToken' },
enabled: { type: 'boolean' },
},
required: ['appId'],
additionalProperties: false,
},
],
});
}
compileAlexaSkillEvents() {
this.serverless.service.getAllFunctions().forEach(functionName => {
const functionObj = this.serverless.service.getFunction(functionName);
let alexaSkillNumberInFunction = 0;
if (functionObj.events) {
functionObj.events.forEach(event => {
if (event === 'alexaSkill' || event.alexaSkill) {
let enabled = true;
let appId;
if (event === 'alexaSkill') {
this.serverless._logDeprecation(
'ALEXA_SKILL_EVENT_WITHOUT_APP_ID',
'Starting with next major version, support for alexaSkill event without appId specified will be removed.'
);
} else if (typeof event.alexaSkill === 'string') {
appId = event.alexaSkill;
} else {
appId = event.alexaSkill.appId;
// Parameter `enabled` is optional, hence the explicit non-equal check for false.
enabled = event.alexaSkill.enabled !== false;
}
alexaSkillNumberInFunction++;
const lambdaLogicalId = this.provider.naming.getLambdaLogicalId(functionName);
const permissionTemplate = {
Type: 'AWS::Lambda::Permission',
Properties: {
FunctionName: {
'Fn::GetAtt': [lambdaLogicalId, 'Arn'],
},
Action: enabled ? 'lambda:InvokeFunction' : 'lambda:DisableInvokeFunction',
Principal: 'alexa-appkit.amazon.com',
},
};
if (appId) {
permissionTemplate.Properties.EventSourceToken = appId.replace(/\\n|\\r/g, '');
}
const lambdaPermissionLogicalId = this.provider.naming.getLambdaAlexaSkillPermissionLogicalId(
functionName,
alexaSkillNumberInFunction
);
const permissionCloudForamtionResource = {
[lambdaPermissionLogicalId]: permissionTemplate,
};
_.merge(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
permissionCloudForamtionResource
);
}
});
}
});
}
}
module.exports = AwsCompileAlexaSkillEvents;