From ec923a86bdce9ef2c2536f48aef1fdc8396e0c85 Mon Sep 17 00:00:00 2001 From: Jackson Price <59149601+jprice8@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:41:38 -0600 Subject: [PATCH] feat(cloudformation): apply RecursiveLoop to Lambda functions (#9935) AWS::Lambda::Function accepted RecursiveLoop but ignored it. The provisioner now validates the value before changing the function and applies it through PutFunctionRecursionConfig, on create and when an update changes it, keeping the same physical function. Removing a value the template declared restores Terminate, and a stack that never declared it leaves a setting made through the Lambda API in place. Co-authored-by: Andre Kurait --- docs/services/lambda.md | 4 ++ .../provisioners/LambdaCfnProvisioner.java | 21 +++++- .../LambdaRecursionConfigController.java | 14 +--- .../aws/services/lambda/LambdaService.java | 14 +++- ...nLambdaRecursionConfigIntegrationTest.java | 72 +++++++++++++++++++ .../LambdaCfnProvisionerTest.java | 22 ++++++ .../cloudformation/writable-property-gaps.tsv | 1 - 7 files changed, 131 insertions(+), 17 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/cloudformation/CloudFormationLambdaRecursionConfigIntegrationTest.java diff --git a/docs/services/lambda.md b/docs/services/lambda.md index 7c65c69d48..92d7b667ee 100644 --- a/docs/services/lambda.md +++ b/docs/services/lambda.md @@ -200,6 +200,10 @@ subsequent unqualified `GetFunction` responses. `GetFunctionRecursionConfig` reads it back, `Terminate` until one is put. The setting is function-wide: it is read from `$LATEST` whichever version or alias is invoked. +CloudFormation applies `AWS::Lambda::Function.RecursiveLoop` on creation and updates the same +function when the value changes. In Floci, removing the property restores `Terminate`. If neither +template declares it, a stack update preserves the setting from `PutFunctionRecursionConfig`. + Under `Terminate`, a chain of Lambda destinations stops at 16 hops (see Asynchronous Invocation Destinations below). Under `Allow`, the function is not stopped at that bound, as on AWS, so a function that continues itself through a destination runs on until it stops invoking itself. diff --git a/src/main/java/io/floci/aws/services/cloudformation/provisioners/LambdaCfnProvisioner.java b/src/main/java/io/floci/aws/services/cloudformation/provisioners/LambdaCfnProvisioner.java index ec56f71670..10a5fe3eea 100644 --- a/src/main/java/io/floci/aws/services/cloudformation/provisioners/LambdaCfnProvisioner.java +++ b/src/main/java/io/floci/aws/services/cloudformation/provisioners/LambdaCfnProvisioner.java @@ -142,6 +142,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner { applyLambdaReservedConcurrency(region, func, desired); applyLambdaCodeSigningConfig(ctx, func, desired); + applyLambdaRecursionConfig(ctx, func, desired.recursiveLoop()); r.setPhysicalId(desired.functionName()); r.getAttributes().put("Arn", func.getFunctionArn()); @@ -286,6 +287,11 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner { if (codeSigningConfigArn != null) { createRequest.put("CodeSigningConfigArn", codeSigningConfigArn); } + JsonNode recursiveLoopNode = types.readProperty(FUNCTION, props, "RecursiveLoop"); + String recursiveLoop = recursiveLoopNode == null ? null : recursiveLoopNode.textValue(); + if (recursiveLoop != null) { + LambdaService.validateRecursiveLoop(recursiveLoop); + } Integer reservedConcurrentExecutions = null; String reserved = ctx.resolveOptional(props, "ReservedConcurrentExecutions"); if (reserved != null) { @@ -300,7 +306,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner { types.finish(); return new LambdaDesiredState(functionName, hasExplicitName, packageType, durableReplacement, createRequest, code, configRequest, tags, props != null && props.has("ReservedConcurrentExecutions"), - reservedConcurrentExecutions); + reservedConcurrentExecutions, recursiveLoop); } /** @@ -484,6 +490,16 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner { } } + private void applyLambdaRecursionConfig(ProvisionContext ctx, LambdaFunction function, String recursiveLoop) { + if (recursiveLoop == null && ctx.previouslyDeclared("RecursiveLoop")) { + recursiveLoop = LambdaService.DEFAULT_RECURSIVE_LOOP; + } + String current = Objects.requireNonNullElse(function.getRecursiveLoop(), LambdaService.DEFAULT_RECURSIVE_LOOP); + if (recursiveLoop != null && !recursiveLoop.equals(current)) { + lambdaService.putFunctionRecursionConfig(ctx.region(), function.getFunctionName(), recursiveLoop); + } + } + /** * The form LambdaService stores a hot-reload host path in: normalized, so {@code /a/../b/} * and {@code /b} are the same mount, and rendered as the POSIX path Docker receives @@ -1020,7 +1036,8 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner { Map configRequest, Map tags, boolean reservedConcurrentExecutionsPresent, - Integer reservedConcurrentExecutions) {} + Integer reservedConcurrentExecutions, + String recursiveLoop) {} private record LambdaCodeSpec(Map request, String identity) {} } diff --git a/src/main/java/io/floci/aws/services/lambda/LambdaRecursionConfigController.java b/src/main/java/io/floci/aws/services/lambda/LambdaRecursionConfigController.java index 258d798483..5b0d189c02 100644 --- a/src/main/java/io/floci/aws/services/lambda/LambdaRecursionConfigController.java +++ b/src/main/java/io/floci/aws/services/lambda/LambdaRecursionConfigController.java @@ -6,7 +6,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import io.floci.aws.core.common.AwsException; import io.floci.aws.core.common.RegionResolver; -import io.floci.aws.core.common.SmithyValidationErrors; import jakarta.inject.Inject; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.GET; @@ -19,8 +18,6 @@ import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; -import java.util.List; - /** * Lambda recursion config endpoints. * @@ -36,7 +33,6 @@ import java.util.List; public class LambdaRecursionConfigController { private static final String RECURSIVE_LOOP = "RecursiveLoop"; - private static final List RECURSIVE_LOOP_VALUES = List.of("Allow", "Terminate"); private final LambdaService lambdaService; private final RegionResolver regionResolver; @@ -76,15 +72,7 @@ public class LambdaRecursionConfigController { "Could not parse request body into json: " + e.getOriginalMessage(), 400); } JsonNode value = request == null ? null : request.get(RECURSIVE_LOOP); - if (value == null || value.isNull()) { - throw SmithyValidationErrors.validationException(SmithyValidationErrors.notNull("recursiveLoop")); - } - String recursiveLoop = value.asText(); - if (!value.isTextual() || !RECURSIVE_LOOP_VALUES.contains(recursiveLoop)) { - throw SmithyValidationErrors.validationException( - SmithyValidationErrors.notInEnum(recursiveLoop, "recursiveLoop", RECURSIVE_LOOP_VALUES)); - } - return recursiveLoop; + return value == null || value.isNull() ? null : value.asText(); } private Response response(String recursiveLoop) { diff --git a/src/main/java/io/floci/aws/services/lambda/LambdaService.java b/src/main/java/io/floci/aws/services/lambda/LambdaService.java index 7080bba717..16f726aa5f 100644 --- a/src/main/java/io/floci/aws/services/lambda/LambdaService.java +++ b/src/main/java/io/floci/aws/services/lambda/LambdaService.java @@ -99,7 +99,8 @@ public class LambdaService implements ResourceProvider, VpcDependencyProvider { private static final Logger LOG = Logger.getLogger(LambdaService.class); static final String RECURSIVE_LOOP_ALLOW = "Allow"; - private static final String DEFAULT_RECURSIVE_LOOP = "Terminate"; + public static final String DEFAULT_RECURSIVE_LOOP = "Terminate"; + private static final List RECURSIVE_LOOP_VALUES = List.of(RECURSIVE_LOOP_ALLOW, DEFAULT_RECURSIVE_LOOP); /** * Qualifier constraint the live service enforces, taken from its own ValidationException @@ -3849,6 +3850,16 @@ public class LambdaService implements ResourceProvider, VpcDependencyProvider { return fn.getReservedConcurrentExecutions(); } + public static void validateRecursiveLoop(String recursiveLoop) { + if (recursiveLoop == null) { + throw SmithyValidationErrors.validationException(SmithyValidationErrors.notNull("recursiveLoop")); + } + if (!RECURSIVE_LOOP_VALUES.contains(recursiveLoop)) { + throw SmithyValidationErrors.validationException( + SmithyValidationErrors.notInEnum(recursiveLoop, "recursiveLoop", RECURSIVE_LOOP_VALUES)); + } + } + /** * Stores the function's {@code RecursiveLoop} setting. {@code Allow} lifts the bound * {@link LambdaInvocationChain} puts on a chain of asynchronous invocations. The record is read @@ -3856,6 +3867,7 @@ public class LambdaService implements ResourceProvider, VpcDependencyProvider { * DeleteFunction removed. */ public String putFunctionRecursionConfig(String region, String functionName, String recursiveLoop) { + validateRecursiveLoop(recursiveLoop); LambdaFunction observed = getOwnFunction(region, functionName); synchronized (lockForConcurrencyOp(observed.getFunctionArn())) { LambdaFunction fn = getFunction(region, observed.getFunctionName()); diff --git a/src/test/java/io/floci/aws/services/cloudformation/CloudFormationLambdaRecursionConfigIntegrationTest.java b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationLambdaRecursionConfigIntegrationTest.java new file mode 100644 index 0000000000..2c7498e58c --- /dev/null +++ b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationLambdaRecursionConfigIntegrationTest.java @@ -0,0 +1,72 @@ +package io.floci.aws.services.cloudformation; + +import io.floci.aws.testing.AwsTestClients; +import io.floci.aws.testing.CfnStackWaits; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.cloudformation.CloudFormationClient; +import software.amazon.awssdk.services.cloudformation.model.Stack; +import software.amazon.awssdk.services.lambda.LambdaClient; +import software.amazon.awssdk.services.lambda.model.RecursiveLoop; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@QuarkusTest +class CloudFormationLambdaRecursionConfigIntegrationTest { + + @Test + void updatesInPlaceAndResetsOnlyASettingTheTemplateDeclared() { + String stackName = "recursion-cfn-" + UUID.randomUUID(); + String account = "000000000000"; + try (CloudFormationClient cfn = AwsTestClients.create(CloudFormationClient.builder(), account, Region.US_EAST_1); + LambdaClient lambda = AwsTestClients.create(LambdaClient.builder(), account, Region.US_EAST_1)) { + cfn.createStack(r -> r.stackName(stackName).templateBody(template("Allow", "created"))); + try { + Stack created = CfnStackWaits.awaitStatus(cfn, stackName, "CREATE_COMPLETE"); + String function = created.outputs().getFirst().outputValue(); + assertSetting(lambda, function, RecursiveLoop.ALLOW); + Stack updated = CfnStackWaits.updateStack(cfn, stackName, + template("Terminate", "updated"), "UPDATE_COMPLETE"); + assertEquals(function, updated.outputs().getFirst().outputValue()); + assertSetting(lambda, function, RecursiveLoop.TERMINATE); + + CfnStackWaits.updateStack(cfn, stackName, template("Allow", "before-removal"), "UPDATE_COMPLETE"); + CfnStackWaits.updateStack(cfn, stackName, template(null, "removed"), "UPDATE_COMPLETE"); + assertSetting(lambda, function, RecursiveLoop.TERMINATE); + + lambda.putFunctionRecursionConfig(r -> r.functionName(function).recursiveLoop(RecursiveLoop.ALLOW)); + CfnStackWaits.updateStack(cfn, stackName, template(null, "description-only"), "UPDATE_COMPLETE"); + assertSetting(lambda, function, RecursiveLoop.ALLOW); + } finally { + CfnStackWaits.deleteStack(cfn, stackName); + } + } + } + + private static void assertSetting(LambdaClient client, String function, RecursiveLoop expected) { + assertEquals(expected, client.getFunctionRecursionConfig(r -> r.functionName(function)).recursiveLoop()); + } + + private static String template(String mode, String description) { + String setting = mode == null ? "" : "RecursiveLoop: " + mode; + return """ + Resources: + Function: + Type: AWS::Lambda::Function + Properties: + Runtime: nodejs22.x + Handler: index.handler + Role: arn:aws:iam::000000000000:role/recursion-cfn + Code: + ZipFile: exports.handler = async () => ({}); + Description: %s + %s + Outputs: + Name: + Value: !Ref Function + """.formatted(description, setting); + } +} diff --git a/src/test/java/io/floci/aws/services/cloudformation/provisioners/LambdaCfnProvisionerTest.java b/src/test/java/io/floci/aws/services/cloudformation/provisioners/LambdaCfnProvisionerTest.java index 86a599b925..7f38b5a466 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/provisioners/LambdaCfnProvisionerTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/provisioners/LambdaCfnProvisionerTest.java @@ -115,6 +115,28 @@ class LambdaCfnProvisionerTest { return existing; } + @Test + void createAppliesRecursiveLoopThroughTheLambdaRecursionApi() { + when(lambda.createFunction(eq(REGION), anyMap())).thenReturn(lambdaFunction("my-fn")); + provisioner.provision(function(null), props("my-fn").put("RecursiveLoop", "Allow"), ctx()); + + assertFalse(capturedCreateRequest().containsKey("RecursiveLoop")); + verify(lambda).putFunctionRecursionConfig(REGION, "my-fn", "Allow"); + } + + @Test + void invalidRecursiveLoopLeavesTheExistingFunctionInPlace() { + stubInPlaceUpdate("my-fn", Map.of()); + ObjectNode replacement = props("replacement").put("RecursiveLoop", "allow"); + AwsException error = assertThrows(AwsException.class, + () -> provisioner.provision(function("my-fn"), replacement, updateCtx("my-fn"))); + + assertEquals("ValidationException", error.getErrorCode()); + verify(lambda, never()).createFunction(anyString(), anyMap()); + verify(lambda, never()).deleteFunction(anyString(), anyString()); + verify(lambda, never()).putFunctionRecursionConfig(anyString(), anyString(), anyString()); + } + @Test void functionTagsFromTheTemplateReachCreateFunction() { when(lambda.createFunction(eq(REGION), anyMap())).thenReturn(lambdaFunction("my-fn")); diff --git a/src/test/resources/cloudformation/writable-property-gaps.tsv b/src/test/resources/cloudformation/writable-property-gaps.tsv index 4718b58420..3280a077b6 100644 --- a/src/test/resources/cloudformation/writable-property-gaps.tsv +++ b/src/test/resources/cloudformation/writable-property-gaps.tsv @@ -395,7 +395,6 @@ AWS::Lambda::Function CapacityProviderConfig accepted and ignored AWS::Lambda::Function FunctionScalingConfig accepted and ignored AWS::Lambda::Function KmsKeyArn accepted and ignored AWS::Lambda::Function PublishToLatestPublished accepted and ignored -AWS::Lambda::Function RecursiveLoop accepted and ignored AWS::Lambda::Function RuntimeConfig accepted and ignored AWS::Lambda::Function RuntimeManagementConfig accepted and ignored AWS::Lambda::Function SnapStart accepted and ignored