forked from serverless-components/aws-lambda
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverless.js
199 lines (174 loc) · 5.26 KB
/
serverless.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const path = require('path')
const aws = require('aws-sdk')
const AwsSdkLambda = aws.Lambda
const { mergeDeepRight, pick } = require('ramda')
const { Component, utils } = require('@serverless/core')
const {
createLambda,
updateLambdaCode,
updateLambdaConfig,
getLambda,
deleteLambda,
configChanged,
pack,
waitUntilReady
} = require('./utils')
const outputsList = [
'name',
'hash',
'description',
'memory',
'timeout',
'code',
'bucket',
'shims',
'handler',
'runtime',
'architectures',
'env',
'role',
'layer',
'arn',
'region'
]
const defaults = {
description: 'AWS Lambda Component',
memory: 512,
timeout: 10,
code: process.cwd(),
bucket: undefined,
shims: [],
handler: 'handler.hello',
runtime: 'nodejs18.x',
architectures:["arm64"],
env: {},
region: 'us-east-1',
tracingConfig: {
Mode: 'PassThrough'
},
}
class AwsLambda extends Component {
async default(inputs = {}) {
this.context.status(`Deploying`);
const config = mergeDeepRight(defaults, inputs)
config.name = this.state.name || inputs.name || this.context.resourceId()
this.context.debug(
`Starting deployment of lambda ${config.name} to the ${config.region} region.`
)
const lambda = new AwsSdkLambda({
region: config.region,
credentials: this.context.credentials.aws,
maxRetries: 10
})
config.role = inputs.role;
this.context.status('Packaging')
this.context.debug(`Packaging lambda code from ${config.code}.`)
config.zipPath = await pack(config.code, config.shims)
config.hash = await utils.hashFile(config.zipPath);
const prevLambda = await getLambda({ lambda, ...config })
if (!prevLambda) {
this.context.debug(`Creating lambda ${config.name} in the ${config.region} region.`)
const createResult = await createLambda({ lambda, ...config })
config.arn = createResult.arn
config.hash = createResult.hash
await waitUntilReady(lambda, this.context, config.name);
} else {
config.arn = prevLambda.arn
if (configChanged(prevLambda, config)) {
if ((prevLambda.architectures[0] !== config.architectures[0]) || (!config.bucket && prevLambda.hash !== config.hash)) {
this.context.debug(`Uploading ${config.name} lambda code.`)
await updateLambdaCode({ lambda, ...config })
await waitUntilReady(lambda, this.context, config.name);
}
try {
this.context.debug(`Updating ${config.name} lambda config.`);
const updateResult = await updateLambdaConfig({ lambda, ...config });
this.context.debug(`Lambda config for ${config.description} updated.`);
config.hash = updateResult.hash;
await waitUntilReady(lambda, this.context, config.name);
}
catch(e){
console.error(e);
}
}
}
// todo we probably don't need this logic now that we auto generate names
if (this.state.name && this.state.name !== config.name) {
this.context.status(`Replacing`)
await deleteLambda({ lambda, name: this.state.name })
}
this.context.debug(
`Successfully deployed lambda ${config.name} in the ${config.region} region.`
)
const outputs = pick(outputsList, config)
this.state = outputs
await this.save()
return outputs
}
async publishVersion() {
const { name, region, hash } = this.state
const lambda = new AwsSdkLambda({
region,
credentials: this.context.credentials.aws,
maxRetries: 10
})
try{
const { Version } = await lambda.publishVersion({ FunctionName: name, CodeSha256: hash }).promise();
return { version:Version };
}
catch(e){
console.error(`Error publishing ${name} - ${e.stack || e}`);
return { version:'' };
}
}
async createAlias({ alias, version }) {
const { name, region } = this.state;
const lambda = new AwsSdkLambda({
region,
credentials: this.context.credentials.aws,
maxRetries: 10
})
try {
await lambda
.createAlias({
FunctionName: name,
Name: alias,
FunctionVersion: version
})
.promise()
}
catch (e) {
if (e.code === 'ResourceConflictException') {
this.context.debug('Alias already exists. Cool.')
return;
}
// otherwise bounce
throw Error(e)
}
}
async remove() {
this.context.status(`Removing`)
if (!this.state.name) {
this.context.debug(`Aborting removal. Function name not found in state.`)
return
}
const { name, region } = this.state
const lambda = new AwsSdkLambda({
region,
credentials: this.context.credentials.aws,
maxRetries: 10
})
const awsIamRole = await this.load('@serverless/aws-iam-role')
const layer = await this.load('@serverless/aws-lambda-layer')
await awsIamRole.remove()
await layer.remove()
this.context.debug(`Removing lambda ${name} from the ${region} region.`)
await deleteLambda({ lambda, name })
this.context.debug(`Successfully removed lambda ${name} from the ${region} region.`)
const outputs = pick(outputsList, this.state)
this.state = {}
await this.save()
return outputs
}
}
module.exports = AwsLambda