generated from distinction-dev/serverless-ts-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (102 loc) · 2.89 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
'use strict';
const { get } = require('./util');
const parse = require('./parser');
const compile = require('./compiler');
class ServerlessFargate {
constructor(serverless) {
serverless.configSchemaHandler.defineTopLevelProperty(
'appRunner',
require('./schema')
);
const config = get(serverless, 'configurationInput.appRunner', {});
if (!config.services) {
return;
}
this.config = config;
this.serverless = serverless;
this.hooks = {
'package:compileFunctions': this.compileTasks.bind(this),
};
}
async compileTasks() {
const config = parse({
...this.config,
// environment: this.getEnvironmentVariables(),
// vpc: this.getVpcConfiguration(),
tags: this.getResourceTags(),
// iamRoleStatements: this.getIamRoleStatements(),
// iamManagedPolicies: this.getIamManagedPolicies(),
});
const images = await this.resolveTaskImages(config.services);
const compiled = compile(images, config, this.serverless);
const template =
this.serverless.service.provider.compiledCloudFormationTemplate;
template.Resources = {
...template.Resources,
...compiled.Resources,
};
template.Outputs = {
...template.Outputs,
...compiled.Outputs,
};
}
// Uses the frameworks internal means of building images, allowing the plugin
// to use the same ECR image defintion as you would with a Lambda function.
async resolveTaskImages(services) {
const images = {};
for (const service of services) {
this.serverless.service.functions[service.name] = {
image: service.image,
};
const { functionImageUri } = await this.serverless
.getProvider('aws')
.resolveImageUriAndSha(service.name);
images[service.name] = functionImageUri;
delete this.serverless.service.functions[service.name];
}
return images;
}
getIamRoleStatements() {
const providerStatements = get(
this.serverless.service.provider,
'iam.role.statements',
[]
);
return [
...providerStatements,
...(this.serverless.service.provider.iamRoleStatements || []),
...(this.config.iamRoleStatements || []),
];
}
getIamManagedPolicies() {
const providerManagedPolicies = get(
this.serverless.service.provider,
'iam.role.managedPolicies',
[]
);
return [
...providerManagedPolicies,
...(this.serverless.service.provider.iamManagedPolicies || []),
...(this.config.iamManagedPolicies || []),
];
}
getResourceTags() {
return {
...(this.serverless.service.provider.tags || {}),
...(this.config.tags || {}),
};
}
getEnvironmentVariables() {
return {
...(this.serverless.service.provider.environment || {}),
...(this.config.environment || {}),
};
}
getVpcConfiguration() {
return {
...(this.serverless.service.provider.vpc || {}),
...(this.config.vpc || {}),
};
}
}
module.exports = ServerlessFargate;