Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cloudformation): pass Lambda hot-reload code through to the Lambda service (#4093)
stodan34f4f72e8257
diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.mdindex 3449277d89..da6712dbbe 100644--- a/docs/services/cloudformation.md+++ b/docs/services/cloudformation.md@@ -128,7 +128,7 @@ cross-resource references.| SQS | `Queue`, `QueuePolicy` (accepted; policy not enforced) || SNS | `Topic`, `Subscription`, `TopicPolicy` || DynamoDB | `Table`, `GlobalTable` |-| Lambda | `Function` (Zip via S3/inline `ZipFile`, and Image), `LayerVersion`, `EventSourceMapping` (SQS, Kinesis, DynamoDB Streams), `Version`, `Alias` (also what SAM's `AutoPublishAlias` expands into), `Permission`, `EventInvokeConfig`, `MicrovmImage`, `NetworkConnector`, `Url`. Inline `ZipFile` packages include the `cfn-response` (Node.js) / `cfnresponse` (Python) module AWS injects for that code path, so Solutions-style custom-resource handlers work. |+| Lambda | `Function` (Zip via S3/inline `ZipFile`, Image, and the `hot-reload` bind-mount bucket), `LayerVersion`, `EventSourceMapping` (SQS, Kinesis, DynamoDB Streams), `Version`, `Alias` (also what SAM's `AutoPublishAlias` expands into), `Permission`, `EventInvokeConfig`, `MicrovmImage`, `NetworkConnector`, `Url`. Inline `ZipFile` packages include the `cfn-response` (Node.js) / `cfnresponse` (Python) module AWS injects for that code path, so Solutions-style custom-resource handlers work. || IAM | `Role`, `User` (template `LoginProfile` and `PermissionsBoundary` are ignored; an API-created login profile is removed on delete), `AccessKey`, `Policy`, `ManagedPolicy`, `InstanceProfile` || Organizations | `Organization`, `OrganizationalUnit`, `Account`, `Policy`, `ResourcePolicy` || SSM | `Parameter` |@@ -268,6 +268,7 @@ accepts, not only by name:- Code and mutable configuration changes update the existing function in place.- Replacement-only changes such as `FunctionName` or `PackageType` changes create a replacement function and remove the old one.- S3-backed code stays linked through `S3Bucket` / `S3Key`, so Lambda's reactive S3 sync continues to work for functions created by CloudFormation or CDK.+- Hot-reload code (`S3Bucket: hot-reload`) is compared by host path: the same path is a no-op, a different path updates the bind mount in place.## RDS Credential Dynamic Referencesdiff --git a/docs/services/lambda.md b/docs/services/lambda.mdindex ab797054b4..acfee960a9 100644--- a/docs/services/lambda.md+++ b/docs/services/lambda.md@@ -194,6 +194,18 @@ services:FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ENABLED: "true"```+### CloudFormation and SAM++`AWS::Lambda::Function` accepts the same pair. A template whose `Code` is+`{"S3Bucket": "hot-reload", "S3Key": "/absolute/path"}` is handed to Lambda unchanged instead of+being resolved against S3, so the stack creates a hot-reload function and reaches+`CREATE_COMPLETE`. A SAM `CodeUri` of `s3://hot-reload//absolute/path` expands to the same `Code`:+the double slash keeps the key absolute, because SAM takes everything after the first slash as the key.+Redeploying the stack with an unchanged path is a no-op for the code; a changed path updates the+function's bind mount through `UpdateFunctionCode`. Lambda's own hot-reload checks still apply, so+a stack whose hot-reload path is relative, outside the allow-list, or used while hot-reload is+disabled fails with Lambda's error as the resource status reason.+### Limitations- The `S3Key` path is interpreted by the **Docker daemon**, not by Floci. When Floci itself runs inside Docker, the path must exist on the Docker host machine, not inside the Floci container.diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaCfnProvisioner.javaindex 2d829732a3..6d0e54488a 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaCfnProvisioner.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaCfnProvisioner.java@@ -19,6 +19,8 @@ import org.jboss.logging.Logger;import java.io.ByteArrayOutputStream;import java.nio.charset.StandardCharsets;+import java.nio.file.InvalidPathException;+import java.nio.file.Path;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.ArrayList;@@ -48,6 +50,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {private static final String LAYER_VERSION = "AWS::Lambda::LayerVersion";private static final String LAMBDA_CODE_IDENTITY_ATTR = "FlociLambdaCodeIdentity";+ private static final String HOT_RELOAD_BUCKET = "hot-reload";private static final String LAMBDA_NAME_MODE_ATTR = "FlociLambdaFunctionNameMode";private static final String LAMBDA_PACKAGE_TYPE_ATTR = "FlociLambdaPackageType";private static final String NAME_MODE_EXPLICIT = "explicit";@@ -265,6 +268,14 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {String s3Bucket = codeNode.path("S3Bucket").asText(null);String s3Key = codeNode.path("S3Key").asText(null);+ if (HOT_RELOAD_BUCKET.equals(s3Bucket) && s3Key != null) {+ // Lambda's bind-mount hot-reload convention: the bucket is a marker and the key+ // is a directory on the Docker host, so there is no S3 object to probe. Hand the+ // pair to LambdaService unchanged; it enforces the hot-reload enablement and+ // path allow-list and reports its own errors, which fail the resource.+ return new LambdaCodeSpec(Map.of("S3Bucket", s3Bucket, "S3Key", s3Key),+ "hot-reload:" + canonicalHotReloadPath(s3Key));+ }if (s3Bucket != null && s3Key != null) {// A template that names its code explicitly must fail if that code cannot be// read, the way real CloudFormation does. Substituting the stub handler here@@ -394,6 +405,21 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {}}+ /**+ * The form LambdaService stores a hot-reload host path in: normalized, so {@code /a/../b/}+ * and {@code /b} are the same mount. Identities and change detection use it so a redeploy+ * that only rewrites the path spelling, or a function adopted from a direct CreateFunction,+ * is not mistaken for a code change. The raw key still goes to Lambda, which validates it and+ * reports its own error for a path it cannot use.+ */+ private static String canonicalHotReloadPath(String s3Key) {+ try {+ return Path.of(s3Key).normalize().toString();+ } catch (InvalidPathException e) {+ return s3Key;+ }+ }+private boolean lambdaCodeChanged(LambdaFunction fn,LambdaCodeSpec code, String previousIdentity) {if (previousIdentity != null) {@@ -403,6 +429,10 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {if (request.containsKey("ImageUri")) {return !Objects.equals(fn.getImageUri(), request.get("ImageUri"));}+ if (HOT_RELOAD_BUCKET.equals(request.get("S3Bucket"))) {+ return !Objects.equals(fn.getHotReloadHostPath(),+ canonicalHotReloadPath((String) request.get("S3Key")));+ }if (request.containsKey("S3Bucket") && request.containsKey("S3Key")) {return !Objects.equals(fn.getS3Bucket(), request.get("S3Bucket"))|| !Objects.equals(fn.getS3Key(), request.get("S3Key"));diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaHotReloadCodeTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaHotReloadCodeTest.javanew file mode 100644index 0000000000..8700a269a4--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaHotReloadCodeTest.java@@ -0,0 +1,184 @@+package io.github.hectorvent.floci.services.cloudformation;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import io.github.hectorvent.floci.services.cloudformation.model.StackResource;+import io.github.hectorvent.floci.services.lambda.LambdaService;+import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;+import io.github.hectorvent.floci.services.s3.S3Service;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;+import org.mockito.ArgumentCaptor;++import java.util.Map;+import java.util.function.Function;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.mockito.ArgumentMatchers.anyMap;+import static org.mockito.ArgumentMatchers.anyString;+import static org.mockito.ArgumentMatchers.eq;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.never;+import static org.mockito.Mockito.verify;+import static org.mockito.Mockito.when;++/**+ * A template whose Lambda code is {@code {S3Bucket: hot-reload, S3Key: /host/path}} uses the+ * Lambda service's bind-mount hot-reload convention. There is no S3 object behind that pair, so+ * the provisioner must hand it to LambdaService unchanged instead of probing S3 and failing the+ * resource (or, with allow-stub-lambda-code, quietly deploying the stub handler).+ */+class CloudFormationLambdaHotReloadCodeTest {++ private static final String ACCOUNT_ID = "000000000000";+ private static final String REGION = "us-east-1";+ private static final String STACK_NAME = "test-stack";+ private static final String FUNCTION_NAME = "hot-fn";+ private static final String HOST_PATH = "/home/dev/project/dist";++ private final ObjectMapper mapper = new ObjectMapper();+ private S3Service s3Service;+ private LambdaService lambdaService;+ private CloudFormationResourceProvisioner provisioner;++ @BeforeEach+ void setUp() {+ s3Service = mock(S3Service.class);+ lambdaService = mock(LambdaService.class);+ when(lambdaService.createFunction(anyString(), anyMap()))+ .thenAnswer(inv -> hotReloadFunction(HOST_PATH));+ when(lambdaService.updateFunctionConfiguration(anyString(), anyString(), anyMap()))+ .thenAnswer(inv -> hotReloadFunction(HOST_PATH));+ when(lambdaService.updateFunctionCode(anyString(), anyString(), anyMap()))+ .thenAnswer(inv -> hotReloadFunction((String) ((Map<?, ?>) inv.getArgument(2)).get("S3Key")));+ provisioner = CfnProvisionerFixture.builder()+ .s3(s3Service)+ .lambda(lambdaService)+ .objectMapper(mapper)+ .build();+ }++ @Test+ void hotReloadCodeIsPassedThroughWithoutProbingS3() {+ StackResource result = provision(HOST_PATH, null, Map.of());++ assertEquals("CREATE_COMPLETE", result.getStatus());+ verify(s3Service, never()).headObject(anyString(), anyString());+ verify(s3Service, never()).getObject(anyString(), anyString());++ ArgumentCaptor<Map<String, Object>> request = ArgumentCaptor.captor();+ verify(lambdaService).createFunction(eq(REGION), request.capture());+ assertEquals(Map.of("S3Bucket", "hot-reload", "S3Key", HOST_PATH), request.getValue().get("Code"));+ assertEquals("hot-reload:" + HOST_PATH, result.getAttributes().get("FlociLambdaCodeIdentity"));+ }++ @Test+ void redeployingTheSameHostPathDoesNotTouchTheCode() {+ when(lambdaService.getFunction(REGION, FUNCTION_NAME)).thenReturn(hotReloadFunction(HOST_PATH));++ StackResource result = provision(HOST_PATH, FUNCTION_NAME,+ Map.of("FlociLambdaCodeIdentity", "hot-reload:" + HOST_PATH));++ assertEquals(FUNCTION_NAME, result.getPhysicalId());+ verify(lambdaService, never()).createFunction(anyString(), anyMap());+ verify(lambdaService, never()).updateFunctionCode(anyString(), anyString(), anyMap());+ verify(s3Service, never()).headObject(anyString(), anyString());+ }++ @Test+ void anAdoptedHotReloadFunctionWithoutARecordedIdentityIsComparedByHostPath() {+ when(lambdaService.getFunction(REGION, FUNCTION_NAME)).thenReturn(hotReloadFunction(HOST_PATH));++ provision(HOST_PATH, FUNCTION_NAME, Map.of());++ verify(lambdaService, never()).updateFunctionCode(anyString(), anyString(), anyMap());+ }++ @Test+ void aRespelledHostPathIsNotACodeChange() {+ // LambdaService stores the normalized path (#4053); the template may spell the same+ // directory with a trailing slash or a ".." segment. Neither is a new mount.+ when(lambdaService.getFunction(REGION, FUNCTION_NAME)).thenReturn(hotReloadFunction(HOST_PATH));++ StackResource result = provision("/home/dev/project/build/../dist/", FUNCTION_NAME,+ Map.of("FlociLambdaCodeIdentity", "hot-reload:" + HOST_PATH));++ verify(lambdaService, never()).updateFunctionCode(anyString(), anyString(), anyMap());+ assertEquals("hot-reload:" + HOST_PATH, result.getAttributes().get("FlociLambdaCodeIdentity"));+ }++ @Test+ void anAdoptedFunctionIsComparedAgainstTheCanonicalTemplatePath() {+ when(lambdaService.getFunction(REGION, FUNCTION_NAME)).thenReturn(hotReloadFunction(HOST_PATH));++ provision(HOST_PATH + "/", FUNCTION_NAME, Map.of());++ verify(lambdaService, never()).updateFunctionCode(anyString(), anyString(), anyMap());+ }++ @Test+ void theRawKeyStillReachesLambdaForValidation() {+ StackResource result = provision("relative/dist", null, Map.of());++ ArgumentCaptor<Map<String, Object>> request = ArgumentCaptor.captor();+ verify(lambdaService).createFunction(eq(REGION), request.capture());+ assertEquals("relative/dist", ((Map<?, ?>) request.getValue().get("Code")).get("S3Key"));+ assertEquals("CREATE_COMPLETE", result.getStatus());+ }++ @Test+ void aChangedHostPathUpdatesTheFunctionCode() {+ when(lambdaService.getFunction(REGION, FUNCTION_NAME)).thenReturn(hotReloadFunction(HOST_PATH));++ StackResource result = provision("/home/dev/other/dist", FUNCTION_NAME,+ Map.of("FlociLambdaCodeIdentity", "hot-reload:" + HOST_PATH));++ verify(lambdaService, never()).createFunction(anyString(), anyMap());+ verify(lambdaService).updateFunctionCode(eq(REGION), eq(FUNCTION_NAME),+ eq(Map.of("S3Bucket", "hot-reload", "S3Key", "/home/dev/other/dist")));+ assertEquals("hot-reload:/home/dev/other/dist", result.getAttributes().get("FlociLambdaCodeIdentity"));+ }++ private StackResource provision(String hostPath, String existingPhysicalId,+ Map<String, String> existingAttributes) {+ String props = """+ {+ "FunctionName": "%s",+ "Runtime": "nodejs20.x",+ "Handler": "index.handler",+ "Role": "arn:aws:iam::000000000000:role/r",+ "Code": {"S3Bucket": "hot-reload", "S3Key": "%s"}+ }+ """.formatted(FUNCTION_NAME, hostPath);+ return provisioner.provision("Function", "AWS::Lambda::Function", props(props), engine(),+ REGION, ACCOUNT_ID, STACK_NAME, existingPhysicalId, existingAttributes);+ }++ private CloudFormationTemplateEngine engine() {+ return new CloudFormationTemplateEngine(+ ACCOUNT_ID, REGION, STACK_NAME, "stack/id",+ Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), mapper,+ (Function<String, String>) name -> null);+ }++ private JsonNode props(String json) {+ try {+ return mapper.readTree(json);+ } catch (Exception e) {+ throw new RuntimeException(e);+ }+ }++ private static LambdaFunction hotReloadFunction(String hostPath) {+ LambdaFunction fn = new LambdaFunction();+ fn.setFunctionName(FUNCTION_NAME);+ fn.setFunctionArn("arn:aws:lambda:" + REGION + ":" + ACCOUNT_ID + ":function:" + FUNCTION_NAME);+ fn.setPackageType("Zip");+ fn.setRuntime("nodejs20.x");+ fn.setHandler("index.handler");+ fn.setRole("arn:aws:iam::" + ACCOUNT_ID + ":role/r");+ fn.setRevisionId("1");+ fn.setHotReloadHostPath(hostPath);+ return fn;+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaHotReloadIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaHotReloadIntegrationTest.javanew file mode 100644index 0000000000..907baec325--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaHotReloadIntegrationTest.java@@ -0,0 +1,116 @@+package io.github.hectorvent.floci.services.cloudformation;++import io.quarkus.test.junit.QuarkusTest;+import io.restassured.response.ValidatableResponse;+import org.junit.jupiter.api.Test;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.equalTo;+import static org.hamcrest.Matchers.not;++/**+ * A stack whose Lambda code is {@code {S3Bucket: hot-reload, S3Key: /host/path}} must reach+ * CREATE_COMPLETE with the function bound to that host path, exactly as CreateFunction with the+ * same Code does. The provisioner used to probe S3 for the pair and fail the stack because no+ * such object exists. Control plane only: the bind mount is only consulted at invoke time, so+ * the test is Docker-free. The test profile enables hot-reload.+ */+@QuarkusTest+class CloudFormationLambdaHotReloadIntegrationTest {++ private static final String CFN_AUTH =+ "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";++ @Test+ void hotReloadCodeReachesLambdaAndCompletesTheStack() {+ String suffix = Long.toString(System.nanoTime(), 36);+ String stackName = "cfn-hot-reload-" + suffix;+ String fnName = "hot-reload-fn-" + suffix;+ String hostPath = "/tmp/floci-cfn-hot-reload-" + suffix;++ createStack(stackName, template(fnName, hostPath));++ describeStacks(stackName)+ .body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));++ given()+ .when().get("/2015-03-31/functions/" + fnName)+ .then()+ .statusCode(200)+ .body("Configuration.FunctionName", equalTo(fnName))+ .body("Configuration.Handler", equalTo("index.handler"));++ given()+ .contentType("application/x-www-form-urlencoded")+ .header("Authorization", CFN_AUTH)+ .formParam("Action", "DeleteStack")+ .formParam("StackName", stackName)+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }++ @Test+ void aRelativeHostPathFailsTheStackWithLambdasOwnMessage() {+ String suffix = Long.toString(System.nanoTime(), 36);+ String stackName = "cfn-hot-reload-rel-" + suffix;+ String fnName = "hot-reload-rel-fn-" + suffix;++ createStack(stackName, template(fnName, "relative/dist"));++ describeStacks(stackName)+ .body(not(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>")))+ .body(containsString("Hot-reload S3Key must be an absolute path"));++ given()+ .when().get("/2015-03-31/functions/" + fnName)+ .then()+ .statusCode(404);+ }++ private static String template(String fnName, String hostPath) {+ return """+ {+ "Resources": {+ "Fn": {+ "Type": "AWS::Lambda::Function",+ "Properties": {+ "FunctionName": "%s",+ "Runtime": "nodejs20.x",+ "Handler": "index.handler",+ "Role": "arn:aws:iam::000000000000:role/r",+ "Code": {"S3Bucket": "hot-reload", "S3Key": "%s"}+ }+ }+ }+ }+ """.formatted(fnName, hostPath);+ }++ private static void createStack(String stackName, String template) {+ given()+ .contentType("application/x-www-form-urlencoded")+ .header("Authorization", CFN_AUTH)+ .formParam("Action", "CreateStack")+ .formParam("StackName", stackName)+ .formParam("TemplateBody", template)+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }++ private static ValidatableResponse describeStacks(String stackName) {+ return given()+ .contentType("application/x-www-form-urlencoded")+ .header("Authorization", CFN_AUTH)+ .formParam("Action", "DescribeStacks")+ .formParam("StackName", stackName)+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.javaindex 0b993c5461..b9c29de3ca 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java@@ -411,6 +411,33 @@ class SamTransformProcessorTest {assertEquals("code.zip", code.path("S3Key").asText());}+ @Test+ void expandSamTemplate_functionWithHotReloadCodeUri() throws Exception {+ // Everything after the first slash is the key, so the documented double slash is what+ // keeps a hot-reload host path absolute; the pair must reach AWS::Lambda::Function intact.+ JsonNode template = objectMapper.readTree("""+ {+ "Transform": "AWS::Serverless-2016-10-31",+ "Resources": {+ "HotFunc": {+ "Type": "AWS::Serverless::Function",+ "Properties": {+ "Handler": "index.handler",+ "Runtime": "nodejs20.x",+ "CodeUri": "s3://hot-reload//home/dev/project/dist"+ }+ }+ }+ }+ """);++ JsonNode code = processor.expandSamTemplate(template)+ .path("Resources").path("HotFunc").path("Properties").path("Code");++ assertEquals("hot-reload", code.path("S3Bucket").asText());+ assertEquals("/home/dev/project/dist", code.path("S3Key").asText());+ }+@Testvoid expandSamTemplate_functionPreservesMetadata() throws Exception {JsonNode template = objectMapper.readTree("""diff --git a/tools/docs/cfn_resource_types.yaml b/tools/docs/cfn_resource_types.yamlindex ba37eefb9d..4cfcb52c31 100644--- a/tools/docs/cfn_resource_types.yaml+++ b/tools/docs/cfn_resource_types.yaml@@ -151,7 +151,7 @@ service_labels:notes:AWS::S3::BucketPolicy: document stored on the bucket; S3 does not evaluate itAWS::SQS::QueuePolicy: accepted; policy not enforced- AWS::Lambda::Function: Zip via S3/inline `ZipFile`, and Image+ AWS::Lambda::Function: Zip via S3/inline `ZipFile`, Image, and the `hot-reload` bind-mount bucketAWS::Lambda::EventSourceMapping: SQS, Kinesis, DynamoDB StreamsAWS::Lambda::Alias: also what SAM's `AutoPublishAlias` expands intoAWS::EC2::SecurityGroup: inline `SecurityGroupIngress`/`SecurityGroupEgress` supported