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:
Alessandro Lattao authored and GitHub committed 2026-10-18 00:47:18 -04:00
1 parent 0dd443478c
commit 63af5cd299
7 files changed
+374 -10

No files matched your search

@@ -261,6 +261,9 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
configRequest.put("DeadLetterConfig", resolveMapOrDefault(props, "DeadLetterConfig", engine,
mapWithNullValue("TargetArn")));
configRequest.put("VpcConfig", resolveMapOrDefault(props, "VpcConfig", engine, Map.of()));
// Empty when the template has none, so that removing it puts the function back on its
// defaults (Text, or JSON for a durable function, and /aws/lambda/<name>), as on AWS.
configRequest.put("LoggingConfig", resolveMapOrDefault(props, "LoggingConfig", engine, Map.of()));
configRequest.put("FileSystemConfigs",
resolveObjectListOrEmpty(props, "FileSystemConfigs", engine));
putResolvedMapIfPresent(configRequest, props, "ImageConfig", "ImageConfig", engine);
@@ -573,6 +576,11 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
return true;
}
}
case "LoggingConfig" -> {
if (loggingConfigurationChanged(fn, desired)) {
return true;
}
}
case "FileSystemConfigs" -> {
if (!Objects.equals(normalizeForCompare(fileSystemConfigs(fn)),
normalizeForCompare(desired))) {
@@ -601,6 +609,12 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
return false;
}
/** Compares what Lambda would store for {@code desired} with what the function has. */
private static boolean loggingConfigurationChanged(LambdaFunction fn, Object desired) {
Map<?, ?> logging = desired instanceof Map<?, ?> map ? map : Map.of();
return !LambdaService.loggingSettings(fn).equals(LambdaService.loggingSettings(logging, fn.isDurable()));
}
private boolean imageConfigurationChanged(
LambdaFunction fn,
Object desired) {
@@ -2924,17 +2924,45 @@ public class LambdaService implements ResourceProvider {
if (!(value instanceof Map<?, ?> logging)) {
return;
}
validateLoggingConfig(value, fn.isDurable());
String defaultFormat = fn.isDurable() ? "JSON" : "Text";
String format = logging.get("LogFormat") instanceof String f && !f.isBlank() ? f : defaultFormat;
LoggingSettings settings = loggingSettings(logging, fn.isDurable());
fn.setLogFormat(settings.logFormat());
fn.setApplicationLogLevel(settings.applicationLogLevel());
fn.setSystemLogLevel(settings.systemLogLevel());
fn.setLogGroup(settings.logGroup());
}
/** A function's logging settings as Lambda stores them; see {@link #loggingSettings(Map, boolean)}. */
public record LoggingSettings(String logFormat, String applicationLogLevel, String systemLogLevel,
String logGroup) {
}
/**
* What Lambda stores for a LoggingConfig, once it is valid: an omitted LogFormat is the
* function's default, JSON for a durable function and Text otherwise, the two log levels count
* only in JSON, and a blank value is an omitted one, an omitted LogGroup meaning
* {@code /aws/lambda/<name>}. An invalid value is refused even where it would not be stored,
* a log level under Text included, so a comparison of these settings cannot pass it as
* unchanged.
*/
public static LoggingSettings loggingSettings(Map<?, ?> logging, boolean durable) {
validateLoggingConfig(logging, durable);
String format = logging.get("LogFormat") instanceof String f && !f.isBlank() ? f : durable ? "JSON" : "Text";
boolean json = "JSON".equals(format);
fn.setLogFormat(format);
fn.setApplicationLogLevel(json && logging.get("ApplicationLogLevel") instanceof String level
&& !level.isBlank() ? level : null);
fn.setSystemLogLevel(json && logging.get("SystemLogLevel") instanceof String level
&& !level.isBlank() ? level : null);
fn.setLogGroup(logging.get("LogGroup") instanceof String group && !group.isBlank()
? group : null);
return new LoggingSettings(format,
json ? nonBlank(logging.get("ApplicationLogLevel")) : null,
json ? nonBlank(logging.get("SystemLogLevel")) : null,
nonBlank(logging.get("LogGroup")));
}
/** The function's stored logging settings, its default LogFormat when it has none stored. */
public static LoggingSettings loggingSettings(LambdaFunction fn) {
return new LoggingSettings(
fn.getLogFormat() != null ? fn.getLogFormat() : loggingSettings(Map.of(), fn.isDurable()).logFormat(),
fn.getApplicationLogLevel(), fn.getSystemLogLevel(), fn.getLogGroup());
}
private static String nonBlank(Object value) {
return value instanceof String s && !s.isBlank() ? s : null;
}
private static boolean hasValues(Map<String, Object> config, String key) {