fix(lambda): defer handler validation to invocation (#3332)

Fixes #3311
This commit is contained in:
BingEdward authored and GitHub committed 2026-09-10 08:09:46 -04:00
1 parent 8a4c6494ed
commit 8b25e5003d
3 files changed
+18 -53

No files matched your search

@@ -2773,30 +2773,6 @@ public class LambdaService implements ResourceProvider {
}
}
// For file-based runtimes, verify handler file exists (skip Java and .NET which use different handler formats)
if (fn.getRuntime() != null && !fn.getRuntime().startsWith("java") && !fn.getRuntime().startsWith("dotnet")) {
String handlerFile = resolveHandlerFilePath(fn);
boolean pythonRuntime = fn.getRuntime().startsWith("python");
boolean found;
try (var walk = Files.walk(codePath)) {
found = walk
.filter(Files::isRegularFile)
.anyMatch(p -> {
String relative = codePath.relativize(p).toString();
String withoutExt = relative.contains(".")
? relative.substring(0, relative.lastIndexOf('.'))
: relative;
String normalized = withoutExt.replace('\\', '/');
return normalized.equals(handlerFile)
|| (pythonRuntime && normalized.equals(handlerFile + "/__init__"));
});
}
if (!found) {
throw new AwsException("InvalidParameterValueException",
"Handler file '" + handlerFile + "' not found in deployment package", 400);
}
}
// Deploy succeeded: keep the exact package so GetFunction can serve
// a real Code.Location.
storeDeploymentPackage(fn, zipBytes, region);
@@ -2895,22 +2871,6 @@ public class LambdaService implements ResourceProvider {
extractZipCodeBytes(fn, obj.getData(), region);
}
private String resolveHandlerFilePath(LambdaFunction fn) {
String handler = fn.getHandler();
int lastDot = handler.lastIndexOf('.');
String modulePath = lastDot >= 0 ? handler.substring(0, lastDot) : handler;
if (fn.getRuntime().startsWith("python")) {
return modulePath.replace('.', '/');
}
// A file-based handler may be given with a leading "./" (e.g.
// "./v1/lambda-handlers/entry.handler"); deployment-package entries are stored without
// it, so normalize the prefix away before matching.
if (modulePath.startsWith("./")) {
modulePath = modulePath.substring(2);
}
return modulePath;
}
private void applyHotReload(LambdaFunction fn, String hostPath) {
if (config == null || !config.services().lambda().hotReload().enabled()) {
throw new AwsException("InvalidParameterValueException",
@@ -34,6 +34,9 @@ class LambdaLayerIntegrationTest {
zos.putNextEntry(new ZipEntry("nodejs/node_modules/my-lib/index.js"));
zos.write("module.exports = { hello: () => 'world' };".getBytes());
zos.closeEntry();
zos.putNextEntry(new ZipEntry("python/entrypoint.py"));
zos.write("def lambda_handler(event, context): return {'from': 'layer'}".getBytes());
zos.closeEntry();
}
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
@@ -58,7 +61,7 @@ class LambdaLayerIntegrationTest {
.body("""
{
"Content": { "ZipFile": "%s" },
"CompatibleRuntimes": ["nodejs20.x", "nodejs22.x"],
"CompatibleRuntimes": ["nodejs20.x", "nodejs22.x", "python3.12"],
"CompatibleArchitectures": ["x86_64"],
"Description": "Shared utilities v1",
"LicenseInfo": "MIT"
@@ -73,7 +76,7 @@ class LambdaLayerIntegrationTest {
.body("LayerVersionArn", containsString(":layer:" + LAYER_NAME + ":1"))
.body("Description", equalTo("Shared utilities v1"))
.body("LicenseInfo", equalTo("MIT"))
.body("CompatibleRuntimes", hasItems("nodejs20.x", "nodejs22.x"))
.body("CompatibleRuntimes", hasItems("nodejs20.x", "nodejs22.x", "python3.12"))
.body("CompatibleArchitectures", hasItem("x86_64"))
.body("Content.CodeSize", greaterThan(0))
.body("Content.CodeSha256", not(emptyString()))
@@ -188,15 +191,15 @@ class LambdaLayerIntegrationTest {
@Test
@Order(9)
void createFunction_withLayers_storesLayerArns() throws Exception {
void createFunction_acceptsHandlerProvidedByLayer() throws Exception {
given()
.contentType("application/json")
.body("""
{
"FunctionName": "%s",
"Runtime": "nodejs20.x",
"Runtime": "python3.12",
"Role": "arn:aws:iam::000000000000:role/lambda-role",
"Handler": "index.handler",
"Handler": "entrypoint.lambda_handler",
"Code": { "ZipFile": "%s" },
"Layers": ["arn:aws:lambda:us-east-1:000000000000:layer:%s:1"]
}
@@ -538,7 +538,7 @@ class LambdaServiceTest {
}
@Test
void createFunctionWithMissingHandler() throws Exception {
void createFunctionDoesNotValidateHandlerAtDeployTime() throws Exception {
Map<String, Object> req = new java.util.HashMap<>(Map.of(
"FunctionName", "missing-handler-fn",
"Runtime", "nodejs20.x",
@@ -546,22 +546,24 @@ class LambdaServiceTest {
"Handler", "src/index.handler",
"Code", Map.of("ZipFile", createZipBase64("other.js"))
));
AwsException ex = assertThrows(AwsException.class, () -> service.createFunction(REGION, req));
assertEquals("InvalidParameterValueException", ex.getErrorCode());
LambdaFunction fn = service.createFunction(REGION, req);
assertEquals("src/index.handler", fn.getHandler());
}
@Test
void createFunctionWithMissingNestedPythonModuleHandler() throws Exception {
void updateFunctionCodeDoesNotValidateHandlerAtDeployTime() throws Exception {
Map<String, Object> req = new java.util.HashMap<>(Map.of(
"FunctionName", "missing-nested-python-handler-fn",
"Runtime", "python3.11",
"Role", "arn:aws:iam::000000000000:role/test-role",
"Handler", "apps.foo.src.lambda_handler.lambda_handler",
"Code", Map.of("ZipFile", createZipBase64("apps/foo/src/other.py"))
"Code", Map.of("ZipFile", createZipBase64("apps/foo/src/lambda_handler.py"))
));
AwsException ex = assertThrows(AwsException.class, () -> service.createFunction(REGION, req));
assertEquals("InvalidParameterValueException", ex.getErrorCode());
assertTrue(ex.getMessage().contains("apps/foo/src/lambda_handler"));
service.createFunction(REGION, req);
LambdaFunction fn = service.updateFunctionCode(REGION, "missing-nested-python-handler-fn",
Map.of("ZipFile", createZipBase64("apps/foo/src/other.py")));
assertEquals("apps.foo.src.lambda_handler.lambda_handler", fn.getHandler());
}
@Test