fix(cloudformation): reconcile AWS::Logs::LogGroup in place on UpdateStack (#2152)

* fix(cloudformation): reconcile AWS::Logs::LogGroup in place on UpdateStack

Fixes #1965

Root cause: provisionLogGroup called CreateLogGroup unconditionally on
every provision, including updates to a stack whose log group already
existed and was untouched. CloudWatchLogsService.createLogGroup throws
ResourceAlreadyExistsException in that case, so any UpdateStack on a
stack with a log group resource rolled back with "The specified log
group already exists", even when nothing about the resource changed.

Fix: on update, reconcile retention and tags in place instead of
recreating, matching the pattern already used for Lambda functions.
LogGroupName isn't updatable in place on real AWS, so a name change
still deletes the old group and creates a new one (replacement).

Also fixed a related gap: auto-generated LogGroupName (no explicit
name in the template) called generatePhysicalName, which mints a new
random name on every call, so an auto-named log group was replaced on
every single update even when unchanged. It now reuses the existing
physical name across updates, matching how auto-generated Lambda
function names are handled.

* fix(cloudformation): fix LogGroup replacement ordering and name-mode tracking

Addresses two review findings on the reconciliation fix:

- A LogGroupName rename deleted the old log group before confirming
  the new name could actually be created. If the new name collided
  with an existing group, CreateLogGroup failed and the update rolled
  back, but the old group was already gone and rollback does not
  restore it - permanent data loss. Now creates the new group first,
  matching the existing create-before-delete pattern already used for
  Lambda function replacement, and only deletes the old group once the
  new one exists.
- Removing an explicit LogGroupName from the template fell through to
  the "no explicit name" branch, which reused the prior physical id -
  the old explicit name - instead of generating a fresh name for the
  new auto-generated mode. Now tracks whether the current name is
  explicit or generated (mirroring FlociLambdaFunctionNameMode) and
  forces a fresh generated name when an explicit name is removed.

* fix(cloudformation): infer LogGroup name mode for pre-fix persisted stacks

A log group persisted by a floci version before FlociLogGroupNameMode
was tracked has no recorded mode, so explicitNameRemoved could never
be detected for it even when it should be.

Fixes it by backfilling the mode from the physical id's shape:
generatePhysicalName always produces <stackName>-<logicalId>-<12 hex
chars>, so anything else must have been an explicit LogGroupName.
Doesn't attempt to un-truncate the (practically unreachable at a
512-character limit) truncation path, conservatively treating that
edge case as explicit.

Added a dedicated unit test exercising both legacy cases directly
against the provisioner, since simulating "created by an older floci
version" isn't reachable through the black-box HTTP integration tests.

* refactor(cloudformation): rename shared name-mode constants off the Lambda prefix

NAME_MODE_EXPLICIT/NAME_MODE_GENERATED are generic mode-value labels
shared by both the Lambda and LogGroup name-mode tracking, not
Lambda-specific, so the LAMBDA_ prefix was misleading now that a
second resource type uses them. LAMBDA_NAME_MODE_ATTR is unchanged,
since that one is a genuinely Lambda-specific attribute key.
This commit is contained in:
Olamide Kosile authored and GitHub committed 2026-08-07 16:10:54 -04:00
1 parent f6faf0ffe5
commit 76b264f667
4 files changed
+570 -7

No files matched your search

@@ -119,8 +119,9 @@ public class CloudFormationResourceProvisioner {
private static final String INLINE_CLEANUP_ROLE_TARGETS_ATTR = "__FlociInlineCleanupRoleTargets";
private static final String INLINE_CLEANUP_USER_TARGETS_ATTR = "__FlociInlineCleanupUserTargets";
private static final String INLINE_CLEANUP_GROUP_TARGETS_ATTR = "__FlociInlineCleanupGroupTargets";
private static final String LAMBDA_NAME_MODE_EXPLICIT = "explicit";
private static final String LAMBDA_NAME_MODE_GENERATED = "generated";
private static final String NAME_MODE_EXPLICIT = "explicit";
private static final String NAME_MODE_GENERATED = "generated";
private static final String LOG_GROUP_NAME_MODE_ATTR = "FlociLogGroupNameMode";
private static final String SECRET_TARGET_MANAGED_KEYS_ATTR = "__FlociSecretTargetManagedKeys";
private static final String SECRET_TARGET_OWNER_ATTR = "__FlociSecretTargetOwner";
private static final List<String> SECRET_TARGET_CONNECTION_KEYS = List.of(
@@ -707,8 +708,32 @@ public class CloudFormationResourceProvisioner {
private void provisionLogGroup(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String region, String accountId, String stackName) {
String name = resolveOptional(props, "LogGroupName", engine);
if (name == null || name.isBlank()) {
String explicitName = resolveOptional(props, "LogGroupName", engine);
boolean hasExplicitName = explicitName != null && !explicitName.isBlank();
String previousNameMode = r.getAttributes().get(LOG_GROUP_NAME_MODE_ATTR);
if (previousNameMode == null && r.getPhysicalId() != null) {
// Stacks persisted before FlociLogGroupNameMode existed have no recorded mode, but an
// auto-generated name always has the deterministic <stackName>-<logicalId>-<12 hex chars>
// shape generatePhysicalName produces, so anything else must have been explicit.
previousNameMode = isGeneratedLogGroupName(r.getPhysicalId(), stackName, r.getLogicalId())
? NAME_MODE_GENERATED
: NAME_MODE_EXPLICIT;
}
// Going from an explicit name to none is itself a replacement-worthy change on real AWS, not
// something to silently keep reconciling under the old explicit name (mirrors the same check
// for Lambda's FunctionName above).
boolean explicitNameRemoved = r.getPhysicalId() != null && !hasExplicitName
&& NAME_MODE_EXPLICIT.equals(previousNameMode);
String name;
if (hasExplicitName) {
name = explicitName;
} else if (r.getPhysicalId() != null && !explicitNameRemoved) {
// No explicit name and the prior name was itself auto-generated: keep it across updates
// instead of generating a fresh random one each time, so the log group is reconciled in
// place rather than replaced on every no-op update.
name = r.getPhysicalId();
} else {
name = generatePhysicalName(stackName, r.getLogicalId(), 512, false);
}
Integer retentionInDays = null;
@@ -729,11 +754,74 @@ public class CloudFormationResourceProvisioner {
}
}
}
logsService.createLogGroup(name, retentionInDays, tags, region);
// LogGroupName isn't updatable in place on real AWS (a change replaces the resource), so only
// reconcile in place when the name is unchanged and the group is still there; otherwise this is
// either a first create or a rename, both of which need a fresh createLogGroup call. On a rename,
// create the new group before deleting the old one: if the new name collides with something else
// and createLogGroup throws, the update rolls back without touching the old group, since rollback
// does not restore a resource this method already deleted.
String priorPhysicalId = r.getPhysicalId();
if (priorPhysicalId != null && priorPhysicalId.equals(name) && logsService.logGroupExists(name, region)) {
reconcileLogGroup(name, retentionInDays, tags, region);
} else {
logsService.createLogGroup(name, retentionInDays, tags, region);
if (priorPhysicalId != null && !priorPhysicalId.equals(name)
&& logsService.logGroupExists(priorPhysicalId, region)) {
logsService.deleteLogGroup(priorPhysicalId, region);
}
}
// Ref returns the log group name; GetAtt Arn is arn:aws:logs:<region>:<account>:log-group:<name>:*
r.setPhysicalId(name);
r.getAttributes().put("Arn",
AwsArnUtils.Arn.of("logs", region, accountId, "log-group:" + name + ":*").toString());
r.getAttributes().put(LOG_GROUP_NAME_MODE_ATTR,
hasExplicitName ? NAME_MODE_EXPLICIT : NAME_MODE_GENERATED);
}
/**
* Whether {@code physicalId} matches the exact shape {@link #generatePhysicalName} produces for
* this stack/logical id: {@code <stackName>-<logicalId>-} followed by exactly 12 lowercase hex
* characters. Doesn't account for the (practically unreachable at a 512-character limit)
* truncation path in {@link #generatePhysicalName}, so a truncated legacy name is conservatively
* treated as explicit rather than misdetected as generated.
*/
private boolean isGeneratedLogGroupName(String physicalId, String stackName, String logicalId) {
String prefix = stackName + "-" + logicalId + "-";
if (!physicalId.startsWith(prefix)) {
return false;
}
String suffix = physicalId.substring(prefix.length());
if (suffix.length() != 12) {
return false;
}
for (int i = 0; i < suffix.length(); i++) {
char c = suffix.charAt(i);
boolean hex = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
if (!hex) {
return false;
}
}
return true;
}
private void reconcileLogGroup(String name, Integer retentionInDays, Map<String, String> tags, String region) {
if (retentionInDays != null) {
logsService.putRetentionPolicy(name, retentionInDays, region);
} else {
logsService.deleteRetentionPolicy(name, region);
}
Map<String, String> existingTags = logsService.listTagsLogGroup(name, region);
List<String> tagsToRemove = existingTags.keySet().stream()
.filter(key -> !tags.containsKey(key))
.toList();
if (!tagsToRemove.isEmpty()) {
logsService.untagLogGroup(name, tagsToRemove, region);
}
if (!tags.isEmpty()) {
logsService.tagLogGroup(name, tags, region);
}
}
// ── Kinesis ─────────────────────────────────────────────────────────────────
@@ -1418,7 +1506,7 @@ public class CloudFormationResourceProvisioner {
r.getAttributes().put("Arn", func.getFunctionArn());
r.getAttributes().put(LAMBDA_CODE_IDENTITY_ATTR, desired.code().identity());
r.getAttributes().put(LAMBDA_NAME_MODE_ATTR,
desired.explicitFunctionName() ? LAMBDA_NAME_MODE_EXPLICIT : LAMBDA_NAME_MODE_GENERATED);
desired.explicitFunctionName() ? NAME_MODE_EXPLICIT : NAME_MODE_GENERATED);
r.getAttributes().put(LAMBDA_PACKAGE_TYPE_ATTR, desired.packageType());
}
@@ -1436,7 +1524,7 @@ public class CloudFormationResourceProvisioner {
&& !Objects.equals(oldPackageType, packageType);
boolean explicitRemoved = r.getPhysicalId() != null
&& !hasExplicitName
&& LAMBDA_NAME_MODE_EXPLICIT.equals(previousNameMode);
&& NAME_MODE_EXPLICIT.equals(previousNameMode);
String functionName;
if (hasExplicitName) {
@@ -167,6 +167,10 @@ public class CloudWatchLogsService {
LOG.infov("Deleted log group: {0}", name);
}
public boolean logGroupExists(String name, String region) {
return groupStore.get(groupKey(region, name)).isPresent();
}
public List<LogGroup> describeLogGroups(String prefix, String region) {
String storagePrefix = groupKeyPrefix(region);
List<LogGroup> result = groupStore.scan(k -> {