fix(lambda): return a downloadable GetFunction Code.Location (#1718)

* fix(lambda): return a downloadable GetFunction Code.Location

GetFunction returned a fabricated real-AWS S3 URL for Code.Location that
404s, so clients (and browser UIs) cannot download the deployment package.
Persist the uploaded zip in Floci's own S3 (awslambda-<region>-tasks) on
every successful code deploy and return a path-style URL on the local
endpoint (built from the request via UriInfo), matching AWS/LocalStack.
Bytes are exact, so the download hashes to the stored CodeSha256.

Centralized in extractZipCode so it covers ZipFile, S3-source, and the
reactive S3-update listener; hot-reload is excluded by design. The stored
object is overwritten on redeploy and removed on DeleteFunction.

* fix(lambda): log deleteFunction S3 cleanup failure instead of swallowing it

The best-effort deleteObject cleanup caught and ignored exceptions with an
empty catch block, violating the AGENTS.md rule that a tolerated exception
must be logged with enough context to diagnose it. Log it via LOG.warnv,
matching storeDeploymentPackage.
This commit is contained in:
Jongsic Choi authored and GitHub committed 2026-07-09 23:43:44 -05:00
1 parent 342bb52df4
commit 768019836f
3 files changed
+167 -12

No files matched your search

@@ -24,6 +24,8 @@ import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriBuilder;
import jakarta.ws.rs.core.UriInfo;
import org.jboss.logging.Logger;
import java.time.Instant;
@@ -79,6 +81,7 @@ public class LambdaController {
@GET
@Path("/functions/{functionName}")
public Response getFunction(@Context HttpHeaders headers,
@Context UriInfo uriInfo,
@PathParam("functionName") String functionName) {
String region = regionResolver.resolveRegion(headers);
LambdaFunction fn = lambdaService.getFunction(region, functionName);
@@ -91,8 +94,13 @@ public class LambdaController {
code.put("ImageUri", fn.getImageUri());
code.put("ResolvedImageUri", fn.getImageUri());
} else {
code.put("Location", "https://awslambda-" + region + "-tasks.s3." + region
+ ".amazonaws.com/" + fn.getFunctionName());
// Path-style URL to the package in Floci's own S3, built from the
// request so it targets the same endpoint the client is talking to.
String location = UriBuilder.fromUri(uriInfo.getBaseUri())
.path(LambdaService.tasksBucketName(region))
.path(LambdaService.codeObjectKey(fn))
.build().toString();
code.put("Location", location);
code.put("RepositoryType", "S3");
}
@@ -354,7 +354,7 @@ public class LambdaService {
if (zipFileBase64 != null) {
fn.setS3Bucket(null);
fn.setS3Key(null);
extractZipCode(fn, zipFileBase64);
extractZipCode(fn, zipFileBase64, region);
}
String s3Bucket = (String) code.get("S3Bucket");
String s3Key = (String) code.get("S3Key");
@@ -362,7 +362,7 @@ public class LambdaService {
if ("hot-reload".equals(s3Bucket)) {
applyHotReload(fn, s3Key);
} else {
extractZipCodeFromS3(fn, s3Bucket, s3Key);
extractZipCodeFromS3(fn, s3Bucket, s3Key, region);
}
}
}
@@ -424,7 +424,7 @@ public class LambdaService {
if (zipFileBase64 != null) {
fn.setS3Bucket(null);
fn.setS3Key(null);
extractZipCode(fn, zipFileBase64);
extractZipCode(fn, zipFileBase64, region);
}
if (imageUri != null) {
fn.setImageUri(imageUri);
@@ -433,7 +433,7 @@ public class LambdaService {
if ("hot-reload".equals(s3Bucket)) {
applyHotReload(fn, s3Key);
} else {
extractZipCodeFromS3(fn, s3Bucket, s3Key);
extractZipCodeFromS3(fn, s3Bucket, s3Key, region);
}
}
@@ -594,6 +594,15 @@ public class LambdaService {
}
}
}
// Best-effort: drop the stored deployment package.
if (s3Service != null) {
try {
s3Service.deleteObject(tasksBucketName(region), codeObjectKey(fn));
} catch (Exception e) {
LOG.warnv("Could not delete deployment package for {0}: {1}",
functionName, e.getMessage());
}
}
LOG.infov("Deleted Lambda function: {0}", functionName);
}
@@ -1189,11 +1198,23 @@ public class LambdaService {
return null;
}
private void extractZipCode(LambdaFunction fn, String zipFileBase64) {
extractZipCodeBytes(fn, Base64.getDecoder().decode(zipFileBase64));
/** Per-region bucket that mirrors AWS's Lambda code bucket naming. */
public static String tasksBucketName(String region) {
String r = (region == null || region.isBlank()) ? "us-east-1" : region;
return "awslambda-" + r + "-tasks";
}
private void extractZipCodeBytes(LambdaFunction fn, byte[] zipBytes) {
/** Stable, account-scoped S3 key for a function's current deployment package. */
public static String codeObjectKey(LambdaFunction fn) {
String account = fn.getAccountId() != null ? fn.getAccountId() : "000000000000";
return "snapshots/" + account + "/" + fn.getFunctionName();
}
private void extractZipCode(LambdaFunction fn, String zipFileBase64, String region) {
extractZipCodeBytes(fn, Base64.getDecoder().decode(zipFileBase64), region);
}
private void extractZipCodeBytes(LambdaFunction fn, byte[] zipBytes, String region) {
Path codePath = codeStore.getCodePath(fn.getFunctionName());
try {
zipExtractor.extractTo(zipBytes, codePath);
@@ -1227,6 +1248,10 @@ public class LambdaService {
"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);
} catch (AwsException e) {
throw e;
} catch (IOException e) {
@@ -1235,7 +1260,30 @@ public class LambdaService {
}
}
private void extractZipCodeFromS3(LambdaFunction fn, String s3Bucket, String s3Key) {
private void storeDeploymentPackage(LambdaFunction fn, byte[] zipBytes, String region) {
if (s3Service == null) {
return;
}
String bucket = tasksBucketName(region);
try {
try {
s3Service.createBucket(bucket, region);
} catch (AwsException e) {
// createBucket is idempotent only in us-east-1; elsewhere it 409s if it exists.
if (!"BucketAlreadyOwnedByYou".equals(e.getErrorCode())) {
throw e;
}
}
s3Service.putObject(bucket, codeObjectKey(fn), zipBytes,
"application/zip", java.util.Map.of());
} catch (Exception e) {
// Never fail a deploy because the convenience copy failed.
LOG.warnv("Could not store deployment package for {0}: {1}",
fn.getFunctionName(), e.getMessage());
}
}
private void extractZipCodeFromS3(LambdaFunction fn, String s3Bucket, String s3Key, String region) {
if (s3Service == null) {
throw new AwsException("ServiceUnavailableException", "S3 service not available", 503);
}
@@ -1249,7 +1297,7 @@ public class LambdaService {
throw new AwsException("InvalidParameterValueException",
"Unable to fetch code from s3://" + s3Bucket + "/" + s3Key + ": " + e.getMessage(), 400);
}
extractZipCodeBytes(fn, obj.getData());
extractZipCodeBytes(fn, obj.getData(), region);
}
private String resolveHandlerFilePath(LambdaFunction fn) {
@@ -1542,7 +1590,7 @@ public class LambdaService {
fn.getFunctionName(), event.bucketName(), event.key());
try {
S3Object obj = s3Service.getObject(event.bucketName(), event.key());
extractZipCodeBytes(fn, obj.getData());
extractZipCodeBytes(fn, obj.getData(), region);
fn.setLastModified(Instant.now().toEpochMilli());
fn.setRevisionId(UUID.randomUUID().toString());
functionStore.save(region, fn);