-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateLambdaCode.ts
More file actions
144 lines (133 loc) · 3.35 KB
/
updateLambdaCode.ts
File metadata and controls
144 lines (133 loc) · 3.35 KB
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
import {
type CloudFormationClient,
paginateListStackResources,
type StackResourceSummary,
} from '@aws-sdk/client-cloudformation'
import {
GetFunctionCommand,
type LambdaClient,
LastUpdateStatus,
TagResourceCommand,
UpdateFunctionCodeCommand,
UpdateFunctionConfigurationCommand,
} from '@aws-sdk/client-lambda'
import assert from 'node:assert'
import { readFile } from 'node:fs/promises'
import pRetry from 'p-retry'
import type { PackedLambda } from './packLambda.ts'
const updateLambda =
({
lambda,
packs,
debug,
forceUpdate,
}: {
lambda: LambdaClient
packs: Record<string, PackedLambda>
forceUpdate?: true
debug?: typeof console.debug
}) =>
async (lambdaResource: StackResourceSummary) => {
const info = await lambda.send(
new GetFunctionCommand({
FunctionName: lambdaResource.PhysicalResourceId,
}),
)
const packed = Object.values(packs).find(
(p) => p.id === info.Tags?.['packedLambda:id'],
)
if (packed === undefined) {
debug?.(`[${info.Tags?.['packedLambda:id']}]`, 'No pack found for lambda')
return
}
debug?.(`[${packed.id}]`, 'found pack')
if ((forceUpdate ?? false) === false) {
if (packed.hash === info.Tags?.['packedLambda:hash']) {
debug?.(`[${packed.id}]`, 'No update needed')
return
}
}
if (packed.hash !== info.Tags?.['packedLambda:hash']) {
debug?.(`[${packed.id}]`, 'Updating lambda')
}
const updateResult = await lambda.send(
new UpdateFunctionCodeCommand({
FunctionName: lambdaResource.PhysicalResourceId,
ZipFile: new Uint8Array(await readFile(packed.zipFilePath)),
}),
)
debug?.(`[${packed.id}]`, 'RevisionId', updateResult.RevisionId)
debug?.(`[${packed.id}]`, 'CodeSha256', updateResult.CodeSha256)
await pRetry(
async () => {
const updateStatus = (
await lambda.send(
new GetFunctionCommand({
FunctionName: lambdaResource.PhysicalResourceId,
}),
)
).Configuration?.LastUpdateStatus
assert.equal(
updateStatus,
LastUpdateStatus.Successful,
`Lambda update should have succeeded, got ${updateStatus}`,
)
},
{
minTimeout: 1000,
maxTimeout: 1000,
retries: 10,
},
)
debug?.(`[${packed.id}]`, `updating config to trigger reload ...`)
await Promise.all([
await lambda.send(
new UpdateFunctionConfigurationCommand({
FunctionName: lambdaResource.PhysicalResourceId,
Environment: {
Variables: {
...info.Configuration?.Environment?.Variables,
PACKED_LAMBDA_HASH: packed.hash,
},
},
}),
),
await lambda.send(
new TagResourceCommand({
Resource: info.Configuration!.FunctionArn,
Tags: {
'packedLambda:hash': packed.hash,
},
}),
),
])
}
export const updateLambdaCode =
({ cf, lambda }: { cf: CloudFormationClient; lambda: LambdaClient }) =>
async (
stackName: string,
packedLambdas: Record<string, PackedLambda>,
debug?: (...args: Array<any>) => void,
): Promise<void> => {
const u = updateLambda({
lambda,
packs: packedLambdas,
debug,
})
const p: Array<Promise<void>> = []
for await (const page of paginateListStackResources(
{ client: cf },
{
StackName: stackName,
},
)) {
p.push(
...((page.StackResourceSummaries ?? [])
.filter(
(resource) => resource.ResourceType === 'AWS::Lambda::Function',
)
.map(u) ?? []),
)
}
await Promise.all(p)
}