mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 09:47:56 -05:00
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 <AndreKurait@gmail.com>
This commit is contained in:
1 parent
69c3af4571
commit
ec923a86bd
7 files changed
+131
-17
No files matched your search
@@ -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.
|
||||
|
||||
+19
-2
@@ -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<String, Object> configRequest,
|
||||
Map<String, String> tags,
|
||||
boolean reservedConcurrentExecutionsPresent,
|
||||
Integer reservedConcurrentExecutions) {}
|
||||
Integer reservedConcurrentExecutions,
|
||||
String recursiveLoop) {}
|
||||
|
||||
private record LambdaCodeSpec(Map<String, Object> request, String identity) {}
|
||||
}
|
||||
@@ -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<String> 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) {
|
||||
|
||||
@@ -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<String> 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());
|
||||
|
||||
+72
@@ -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);
|
||||
}
|
||||
}
|
||||
+22
@@ -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"));
|
||||
|
||||
@@ -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
|
||||
|
||||
|
Reference in new issue
Block a user