mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 23:59:27 -05:00
fix(cloudformation): pass a function's LoggingConfig to Lambda (#5902)
* fix(cloudformation): pass a function's LoggingConfig to Lambda The AWS::Lambda::Function provisioner never read LoggingConfig, so every function a stack created logged in Text to /aws/lambda/<name>, whatever the template said, and an update to it was never applied. Measured on AWS (eu-west-1, 2026-10-16): the function reads back the LogGroup the template names; a change to JSON with an ApplicationLogLevel updates it in place, SystemLogLevel reading back INFO; and removing the property from the template puts the function back on Text and /aws/lambda/<name>, again in place. So the provisioner always sends a LoggingConfig, empty when the template has none, which Lambda resolves to the function's defaults (JSON for a durable function, which refuses Text). The update check compares what Lambda would store, through the same LambdaService.loggingSettings that UpdateFunctionConfiguration applies, so an unchanged LoggingConfig is still a no-op redeploy and the two cannot drift apart. Tests: the stack lifecycle in CloudFormationLambdaLoggingConfigIntegrationTest and, through the SDK clients, CloudFormationLambdaLoggingConfigTest, both failing on main; the normaliser in LambdaServiceTest. The compatibility suite gains TestFixtures.awaitStackTerminal for the new test rather than another private copy of the stack wait. * fix(cloudformation): refuse an invalid log level before comparing LoggingConfig The update check compared normalised logging settings, and the normaliser dropped both log levels under Text before anything validated them. An update whose only change was an invalid level, ApplicationLogLevel VERBOSE on a Text function, then looked unchanged, never reached UpdateFunctionConfiguration, and the stack went UPDATE_COMPLETE where AWS fails the update. LambdaService.loggingSettings now runs validateLoggingConfig before it normalises, so the comparison refuses what Lambda refuses, and applyLoggingConfig validates once, through it. The integration test registers its stacks' deletion with PartitionCleanup, and the compatibility test's cleanup no longer swallows a failed DeleteStack, which succeeds for a stack that is already gone.
This commit is contained in:
1 parent
0dd443478c
commit
63af5cd299
7 files changed
+374
-10
No files matched your search
@@ -930,6 +930,24 @@ public final class TestFixtures {
|
||||
}
|
||||
}
|
||||
|
||||
/** The stack's status once it is no longer {@code *_IN_PROGRESS}, or its last status at the timeout. */
|
||||
public static String awaitStackTerminal(CloudFormationClient cfn, String stackName, Duration timeout)
|
||||
throws InterruptedException {
|
||||
long deadline = System.nanoTime() + timeout.toNanos();
|
||||
while (true) {
|
||||
String status = cfn.describeStacks(request -> request.stackName(stackName))
|
||||
.stacks().get(0).stackStatusAsString();
|
||||
if (!status.endsWith("_IN_PROGRESS")) {
|
||||
return status;
|
||||
}
|
||||
long remainingNanos = deadline - System.nanoTime();
|
||||
if (remainingNanos <= 0) {
|
||||
return status;
|
||||
}
|
||||
Thread.sleep(Math.min(500, Math.max(1, Duration.ofNanos(remainingNanos).toMillis())));
|
||||
}
|
||||
}
|
||||
|
||||
public static GlueClient glueClient() {
|
||||
return GlueClient.builder()
|
||||
.endpointOverride(ENDPOINT)
|
||||
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
|
||||
import software.amazon.awssdk.services.lambda.LambdaClient;
|
||||
import software.amazon.awssdk.services.lambda.model.LogFormat;
|
||||
import software.amazon.awssdk.services.lambda.model.LoggingConfig;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* AWS::Lambda::Function.LoggingConfig through the CloudFormation and Lambda SDK clients. Measured on
|
||||
* AWS (eu-west-1, 2026-10-16): the function reads back the LogGroup the template names; a change to
|
||||
* JSON with an ApplicationLogLevel updates it in place, SystemLogLevel reading back INFO; removing
|
||||
* the property puts it back on Text and {@code /aws/lambda/<name>}, again in place.
|
||||
*/
|
||||
@DisplayName("CloudFormation Lambda LoggingConfig")
|
||||
class CloudFormationLambdaLoggingConfigTest {
|
||||
|
||||
private static final String STACK_NAME = TestFixtures.uniqueName("compat-cfn-logging");
|
||||
private static final String ROLE = "arn:aws:iam::000000000000:role/cfn-lambda-role";
|
||||
private static final Duration DEPLOY_TIMEOUT = Duration.ofSeconds(30);
|
||||
|
||||
private static CloudFormationClient cfn;
|
||||
private static LambdaClient lambda;
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
cfn = TestFixtures.cloudFormationClient();
|
||||
lambda = TestFixtures.lambdaClient();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (cfn != null) {
|
||||
// DeleteStack answers for a stack that is gone too, so a failure here is a real one.
|
||||
cfn.deleteStack(request -> request.stackName(STACK_NAME));
|
||||
cfn.close();
|
||||
}
|
||||
if (lambda != null) {
|
||||
lambda.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("LoggingConfig is created, updated and removed in place")
|
||||
void loggingConfigLifecycle() throws InterruptedException {
|
||||
cfn.createStack(request -> request.stackName(STACK_NAME)
|
||||
.templateBody(template(",\"LoggingConfig\": {\"LogGroup\": \"/compat/cfn-a\"}")));
|
||||
assertThat(TestFixtures.awaitStackTerminal(cfn, STACK_NAME, DEPLOY_TIMEOUT)).isEqualTo("CREATE_COMPLETE");
|
||||
String created = functionName();
|
||||
LoggingConfig named = loggingConfig(created);
|
||||
assertThat(named.logFormat()).isEqualTo(LogFormat.TEXT);
|
||||
assertThat(named.logGroup()).isEqualTo("/compat/cfn-a");
|
||||
assertThat(named.applicationLogLevel()).isNull();
|
||||
|
||||
cfn.updateStack(request -> request.stackName(STACK_NAME).templateBody(template(
|
||||
",\"LoggingConfig\": {\"LogGroup\": \"/compat/cfn-b\", \"LogFormat\": \"JSON\","
|
||||
+ " \"ApplicationLogLevel\": \"WARN\"}")));
|
||||
assertThat(TestFixtures.awaitStackTerminal(cfn, STACK_NAME, DEPLOY_TIMEOUT)).isEqualTo("UPDATE_COMPLETE");
|
||||
assertThat(functionName()).as("a changed LoggingConfig updates in place").isEqualTo(created);
|
||||
LoggingConfig json = loggingConfig(created);
|
||||
assertThat(json.logFormat()).isEqualTo(LogFormat.JSON);
|
||||
assertThat(json.applicationLogLevelAsString()).isEqualTo("WARN");
|
||||
assertThat(json.systemLogLevelAsString()).isEqualTo("INFO");
|
||||
assertThat(json.logGroup()).isEqualTo("/compat/cfn-b");
|
||||
|
||||
cfn.updateStack(request -> request.stackName(STACK_NAME).templateBody(template("")));
|
||||
assertThat(TestFixtures.awaitStackTerminal(cfn, STACK_NAME, DEPLOY_TIMEOUT)).isEqualTo("UPDATE_COMPLETE");
|
||||
assertThat(functionName()).as("removing LoggingConfig updates in place").isEqualTo(created);
|
||||
LoggingConfig defaults = loggingConfig(created);
|
||||
assertThat(defaults.logFormat()).isEqualTo(LogFormat.TEXT);
|
||||
assertThat(defaults.applicationLogLevel()).isNull();
|
||||
assertThat(defaults.logGroup()).isEqualTo("/aws/lambda/" + created);
|
||||
}
|
||||
|
||||
private static String template(String loggingConfig) {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"Fn": {
|
||||
"Type": "AWS::Lambda::Function",
|
||||
"Properties": {
|
||||
"Runtime": "python3.14",
|
||||
"Handler": "index.handler",
|
||||
"Role": "%s",
|
||||
"Code": {"ZipFile": "def handler(e, c): return 'ok'"}
|
||||
%s
|
||||
}
|
||||
}
|
||||
},
|
||||
"Outputs": {"Name": {"Value": {"Ref": "Fn"}}}
|
||||
}
|
||||
""".formatted(ROLE, loggingConfig);
|
||||
}
|
||||
|
||||
private static String functionName() {
|
||||
return cfn.describeStacks(request -> request.stackName(STACK_NAME)).stacks().get(0).outputs().get(0).outputValue();
|
||||
}
|
||||
|
||||
private static LoggingConfig loggingConfig(String functionName) {
|
||||
return lambda.getFunctionConfiguration(request -> request.functionName(functionName)).loggingConfig();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user