Skip to content

Commit c8febd5

Browse files
fix: Prevent creating multiple versions of the same Lambda Layer Version
1 parent a792cc9 commit c8febd5

File tree

1 file changed

+54
-36
lines changed

1 file changed

+54
-36
lines changed

src/infraDeploy.ts

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,36 @@ function getIAMClient(): IAMClient {
8080
/**
8181
* Find an existing layer
8282
* @param layerName
83+
* @param description
8384
* @returns
8485
*/
85-
async function findExistingLayer(layerName: string) {
86-
const listLayerVersionsCommand = new ListLayerVersionsCommand({
87-
LayerName: layerName,
88-
});
86+
async function findExistingLayerVersion(
87+
layerName: string,
88+
description: string,
89+
) {
90+
let nextMarker: string | undefined;
8991

90-
const response = await getLambdaClient().send(listLayerVersionsCommand);
91-
if (response.LayerVersions && response.LayerVersions.length > 0) {
92-
const latestLayer = response.LayerVersions[0];
92+
do {
93+
const listLayerVersionsCommand = new ListLayerVersionsCommand({
94+
LayerName: layerName,
95+
Marker: nextMarker,
96+
});
9397

94-
Logger.verbose(
95-
`Latest layer version: ${latestLayer.Version}, description: ${latestLayer.Description}`,
96-
);
98+
const response = await getLambdaClient().send(listLayerVersionsCommand);
99+
if (response.LayerVersions && response.LayerVersions.length > 0) {
100+
const matchingLayer = response.LayerVersions.find(
101+
(layer) => layer.Description === description,
102+
);
103+
if (matchingLayer) {
104+
Logger.verbose(
105+
`Matching layer version: ${matchingLayer.Version}, description: ${matchingLayer.Description}`,
106+
);
107+
return matchingLayer;
108+
}
109+
}
97110

98-
return latestLayer;
99-
}
111+
nextMarker = response.NextMarker;
112+
} while (nextMarker);
100113

101114
Logger.verbose("No existing layer found.");
102115

@@ -110,31 +123,11 @@ async function findExistingLayer(layerName: string) {
110123
async function deployLayer() {
111124
const layerDescription = `Lambda Live Debugger Layer version ${await getVersion()}`;
112125

113-
let layerZipPathFullPath = path.resolve(
114-
path.join(getModuleDirname(), "./extension/extension.zip"),
126+
// Check if the layer already exists
127+
const existingLayer = await findExistingLayerVersion(
128+
layerName,
129+
layerDescription,
115130
);
116-
117-
Logger.verbose(`Layer ZIP path: ${layerZipPathFullPath}`);
118-
119-
// check if file exists
120-
try {
121-
await fs.access(layerZipPathFullPath);
122-
} catch {
123-
// if I am debugging
124-
const layerZipPathFullPath2 = path.join(
125-
getModuleDirname(),
126-
"../dist/extension/extension.zip",
127-
);
128-
129-
try {
130-
await fs.access(layerZipPathFullPath2);
131-
layerZipPathFullPath = layerZipPathFullPath2;
132-
} catch {
133-
throw new Error(`File for the layer not found: ${layerZipPathFullPath}`);
134-
}
135-
}
136-
137-
const existingLayer = await findExistingLayer(layerName);
138131
if (
139132
existingLayer &&
140133
existingLayer.LayerVersionArn &&
@@ -156,6 +149,31 @@ async function deployLayer() {
156149
}
157150
}
158151

152+
// check the ZIP
153+
let layerZipPathFullPath = path.resolve(
154+
path.join(getModuleDirname(), "./extension/extension.zip"),
155+
);
156+
157+
// get the full path to the ZIP file
158+
try {
159+
await fs.access(layerZipPathFullPath);
160+
} catch {
161+
// if I am debugging
162+
const layerZipPathFullPath2 = path.join(
163+
getModuleDirname(),
164+
"../dist/extension/extension.zip",
165+
);
166+
167+
try {
168+
await fs.access(layerZipPathFullPath2);
169+
layerZipPathFullPath = layerZipPathFullPath2;
170+
} catch {
171+
throw new Error(`File for the layer not found: ${layerZipPathFullPath}`);
172+
}
173+
}
174+
175+
Logger.verbose(`Layer ZIP path: ${layerZipPathFullPath}`);
176+
159177
// Read the ZIP file containing your layer code
160178
const layerContent = await fs.readFile(layerZipPathFullPath);
161179

0 commit comments

Comments
 (0)