Light, fluffy, and always free - The AWS Local Emulator alternative
fix(lambdaweb): do not clean up the revision a LatestRevision endpoint leaves (#6565)
TATSUNO “Taz” Yasuhiroc7b3d04e5b5e
diff --git a/docs/services/lambda-web-functions.md b/docs/services/lambda-web-functions.mdindex 77616dad75..a2c03e29ef 100644--- a/docs/services/lambda-web-functions.md+++ b/docs/services/lambda-web-functions.md@@ -38,7 +38,7 @@ The `Lambda Web` service model (apiVersion 2025-03-07, `aws lambda-web` in the A- **Failed revisions:** a revision whose code is not a zip, or whose files add up to more than 512 MiB, is stored `Failed`, as on AWS. It cannot be deployed: weights that name it, or `LatestRevision` while it is the newest revision, are `409 ConflictException`, and `LatestRevision` endpoints stay on their revision. The endpoint of a `CreateWebFunction` whose revision fails is `Failed` too. It answers every request with `409` and `Platform.EndpointNotActive` and refuses updates until it is deleted. In a stack, the revision's resource fails with the revision's `stateReason` and the rollback deletes it. An empty object is a `500 InternalServerException` with the body `{}`, and an object over 512 MiB is a `400 ValidationException`, as on AWS.- **Code comes from S3 only.** The object named in `buildConfig.codeConfig.s3Object` must exist in Floci's S3, as on AWS. A request without `versionId` is refused when the latest version is the null version, so the object has to be written after the bucket's versioning is enabled, as on AWS. When IAM enforcement is on, the bucket policy must also let `lambda.amazonaws.com` take `s3:GetObjectVersion` on the object. The policy sees the new revision ARN as `aws:SourceArn`, the function's account as `aws:SourceAccount` and the version Lambda reads as `s3:VersionId`. As on AWS, `s3:GetObject` is not evaluated, although the error names it. Floci does not check the caller's own S3 permissions or the KMS key policy of an encrypted object.- **Each revision keeps its own copy of the code**, read at creation from the version the request names or the current one, under the Lambda `code-path`. As on AWS, a later write or delete in the bucket does not change what the revision runs. AWS moves a revision to `Failed` when it later finds its object version gone, which Floci does not do.-- **Revision cleanup:** when a revision turns `Active` and the function has more than 50, the oldest revisions that no endpoint routes to are deleted until 50 are left, `Failed` ones included, as on AWS. A new `Failed` revision deletes nothing.+- **Revision cleanup:** when a revision turns `Active` and the function has more than 50, the oldest revisions that no endpoint routes to are deleted until 50 are left, `Failed` ones included, as on AWS. A new `Failed` revision deletes nothing. The revision that a `LatestRevision` endpoint leaves is not deleted before the next revision turns `Active`, as on AWS.- **Execution role:** a role in another account is `403 AccessDeniedException` "Cross-account pass role is not allowed.". A role that does not exist in IAM, or whose trust policy does not let `lambda.amazonaws.com` assume it, is `400 ValidationException` "The execution role <arn> cannot be assumed by lambda.amazonaws.com. Verify its trust policy. If the role was just created, retry in a few seconds.". As on AWS, this check sets no `aws:SourceAccount` or `aws:SourceArn`, so a trust policy that requires either refuses the role. The network connector ARN is checked for shape only.- **Resource policies** are checked the way AWS checks them before storing: the document grammar, a `lambda:` action, a `Resource` that names the function, a valid principal, and no public `Allow`. A wildcard principal needs a positive condition (`StringEquals`, `StringLike`, `ArnLike`, `Bool`) on a trusted key such as `aws:PrincipalAccount` or `aws:PrincipalArn`, with no wildcard in the account it names. An `IamAuth` endpoint evaluates it on requests, as below.- **Function names** are unique across the regions of an account, as on AWS. A name that another region already has is `409 ConflictException` "A function named <name> already exists in the account. Function names must be unique across all regions in an account.".diff --git a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.javaindex a2a3d15fb9..ca65416dd4 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java@@ -440,13 +440,14 @@ public class LambdaWebService implements Resettable {WebFunctionRevision revision = buildRevision(fn, request);keepCode(region, fn, revision);fn.revisions.put(revision.revisionId, revision);+ // Before the endpoints move, as AWS keeps the revision a LatestRevision endpoint leaves.+ List<String> cleanedUp = revision.failureReason == null ? cleanUpRevisions(fn, revision) : List.of();for (WebFunctionEndpoint endpoint : fn.endpoints.values()) {if ("LatestRevision".equals(endpoint.autoDeploymentMode) && revision.failureReason == null) {endpoint.revisionWeights = List.of(weight(revision.revisionId, 100));endpoint.updatedAt = now();}}- List<String> cleanedUp = revision.failureReason == null ? cleanUpRevisions(fn, revision) : List.of();fn.updatedAt = now();persist(region, fn);String accountId = AwsArnUtils.parse(fn.functionArn).accountId();diff --git a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebServiceTest.javaindex d8c3b8387a..a56852baa8 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebServiceTest.java@@ -1013,6 +1013,31 @@ class LambdaWebServiceTest {assertTrue(Files.exists(code.file(ACCOUNT_ID, REGION, "app", active.get(2))));}+ @Test+ void theRevisionALatestRevisionEndpointLeavesStaysUntilTheNextActiveOne() {+ S3Service s3 = mock(S3Service.class);+ LambdaWebService service = service(new MemoryStorageFactory(), "http://localhost:4566", s3);+ byte[] zip = LambdaWebZips.indexJs("");+ when(s3.openObject("code", "app.zip", null)).thenAnswer(invocation -> object(zip));+ String first = service.createFunction(REGION, createRequest("app")).revisions.keySet().iterator().next();+ when(s3.openObject("code", "app.zip", null)).thenAnswer(invocation -> object(NOT_A_ZIP));+ String failed = service.createRevision(REGION, "app", revisionConfig(30)).revisionId;+ for (int i = 0; i < 48; i++) {+ service.createRevision(REGION, "app", revisionConfig(30));+ }+ when(s3.openObject("code", "app.zip", null)).thenAnswer(invocation -> object(zip));++ String second = service.createRevision(REGION, "app", revisionConfig(30)).revisionId;+ Set<String> kept = service.getFunction(REGION, "app").revisions.keySet();+ assertTrue(kept.contains(first));+ assertFalse(kept.contains(failed));++ service.createRevision(REGION, "app", revisionConfig(30));+ kept = service.getFunction(REGION, "app").revisions.keySet();+ assertFalse(kept.contains(first));+ assertTrue(kept.contains(second));+ }+private ObjectNode stateFilter(String state) {ObjectNode request = mapper.createObjectNode();request.putArray("filters").addObject().put("name", "state").putArray("values").add(state);