forked from theserverfault/aws-lambda-layer-release-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
179 lines (171 loc) · 5.28 KB
/
helper.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
const { readFileSync, statSync } = require('fs');
const { LambdaClient, PublishLayerVersionCommand, UpdateFunctionConfigurationCommand, ListFunctionsCommand } = require('@aws-sdk/client-lambda');
const { S3Client, PutObjectCommand, DeleteObjectCommand } = require('@aws-sdk/client-s3');
const non_error_response_codes = [200, 201, 204];
/**
* Reusable function to initiate the lambda client
* @param {*} param0
* @returns
*/
const lambdaClient = ({ region, accessKeyId, secretAccessKey }) => new LambdaClient({
region,
credentials: { accessKeyId, secretAccessKey }
});
/**
* Reusable function to initiate the s3 client
* @param {*} param0
* @returns
*/
const s3Client = ({ region, accessKeyId, secretAccessKey }) => new S3Client({
region,
credentials: { accessKeyId, secretAccessKey }
})
const errored = response => non_error_response_codes.indexOf(response.$metadata.httpStatusCode) == -1 ? true : false;
exports.getArchiveSize = archive => statSync(archive).size
/**
* Resuable function to publish lambda layer. This function dynamically identifies the config
* based on whether to pick archive from file or S3
* @param {*} param0
*/
exports.publishLambdaLayer = async ({
region,
accessKeyId,
secretAccessKey,
layerName,
archive,
architectures,
runtimes,
s3Bucket = null
}) => {
/**
* Initiate the lambda client
*/
const client = lambdaClient({ region, accessKeyId, secretAccessKey });
const payload = {
LayerName: layerName,
Description: "",
/**
* If s3Bucket is defined in the input Parameters then value S3 uploaded layer.
*/
Content: s3Bucket ? {
S3Bucket: s3Bucket,
S3Key: `${layerName}.zip`
} : {
/**
* Direct parse layer only if s3Bucket is null in params
*/
ZipFile: readFileSync(archive)
},
CompatibleArchitectures: architectures,
CompatibleRuntimes: runtimes
}
const command = new PublishLayerVersionCommand(payload);
const response = await client.send(command);
if (errored(response)) {
console.log(JSON.stringify(response));
throw new Error("Error While publishing layer. If you feel this is a bug, raise a ticket on the repo.");
}
console.log("Success Uploading Layer!");
return response;
};
/**
* Reusable function that will publish layer to S3 just in case of layer size is higher than
* the expected size. The code will identify whether this archive can be directly uploaded and if not,
* It should be uploaded to S3 first using this function and then layer parsing is done by dynamoc Content config
* in @function publishLambdaLayer
* @param {*} param0
*/
exports.publishS3LayerArchive = async ({
region,
accessKeyId,
secretAccessKey,
s3Bucket,
layerName,
archive
}) => {
const client = s3Client({ region, accessKeyId, secretAccessKey });
const command = new PutObjectCommand({
Bucket: s3Bucket,
Key: `${layerName}.zip`,
Body: readFileSync(archive)
});
const response = await client.send(command);
if (errored(response)) {
console.log(JSON.stringify(response));
throw new Error("Error While publishing layer to S3. If you feel this is a bug, raise a ticket on the repo.");
}
console.log("Success Uoloading Layer to S3!");
return response;
}
/**
* Reusable function to cleanup the temporary created layer archive on S3 to reduce the accumulating charges over time.
* @param {*} param0
*/
exports.deleteTemporaryArchiveFromS3 = async ({
region,
accessKeyId,
secretAccessKey,
s3Bucket,
s3Key
}) => {
const client = s3Client({ region, accessKeyId, secretAccessKey });
const command = new DeleteObjectCommand({
Bucket: s3Bucket,
Key: s3Key
});
const response = await client.send(command);
if (errored(response)) {
console.log(JSON.stringify(response));
throw new Error("Error While Deleting Layer From S3. If you feel this is a bug, raise a ticket on the repo.");
}
console.log("Success Deleting Layer From S3!");
return response;
}
/**
* Refresh lambda function to use the latest version of layer
*/
exports.refreshLambdaLayerVersion = async ({
region,
accessKeyId,
secretAccessKey,
functionNames,
layerARN,
}) => {
const client = lambdaClient({ region, accessKeyId, secretAccessKey });
const commands = []
for (const functionName of functionNames)
commands.push(client.send(new UpdateFunctionConfigurationCommand({
FunctionName: functionName,
Layers: [layerARN]
})));
const response = await Promise.all(commands);
return response;
}
/**
* List all the lambda functions that use the specified layer
*/
exports.listLambdaFunctionsWithLayer = async ({
region,
accessKeyId,
secretAccessKey,
layerARN
}) => {
try {
const client = lambdaClient({ region, accessKeyId, secretAccessKey });
const allFunctions = [];
let nextMarker = null;
do {
const listFunctionsCommand = new ListFunctionsCommand({ Marker: nextMarker });
const { Functions: functions, NextMarker: nextPageMarker } = await client.send(listFunctionsCommand);
allFunctions.push(...functions);
nextMarker = nextPageMarker;
} while (nextMarker);
const layerARNWithoutVersion = layerARN.split(':').slice(0, -1).join(':')
const matchinFunctions = allFunctions.filter((func) => func.Layers && func.Layers.some((layer) => layer.Arn.includes(layerARNWithoutVersion)))
const functionNames = matchinFunctions.map((func) => func.FunctionName);
return functionNames;
} catch (error) {
console.error("Error:", error);
return [];
}
}