fix(cloudformation): clear EC2 network tags and properties the new template drops (#7124)

A property the previous template declared and the new one drops is now
cleared on update for the EC2 network types: Tags on Subnet,
InternetGateway, RouteTable, NatGateway, EIP and PrefixList, and
MapPublicIpOnLaunch on Subnet, which goes back to false. A property the
stack never declared keeps what was set outside it. A failed update puts
the cleared tags and the flag back from the existing in-place snapshot.

Refs #6606
This commit is contained in:
Oleksandr Nikolaichuk authored and GitHub committed 2026-10-31 16:46:29 -04:00
1 parent ad9de3022a
commit f55948cbcc
4 files changed
+199 -25

No files matched your search

@@ -305,10 +305,12 @@ public class Ec2NetworkCfnProvisioner implements CfnResourceProvisioner {
/**
* Reconciles the template's {@code Tags} onto an entity: keys the template dropped are removed,
* the rest written. A template without a Tags property leaves whatever the entity carries.
* the rest written. A template without a Tags property clears them when the previous template
* declared Tags, and leaves whatever the entity carries when no template ever did.
*/
private void applyTags(StackResource r, boolean reused, String resourceId, List<Tag> current, JsonNode props, ProvisionContext ctx) {
if (props == null || !props.has("Tags")) {
boolean declared = props != null && props.has("Tags");
if (!declared && !ctx.droppedProperties(props).contains("Tags")) {
return;
}
Map<String, String> desired = ctx.resolveTags(props, "Tags");
@@ -465,13 +467,16 @@ public class Ec2NetworkCfnProvisioner implements CfnResourceProvisioner {
CfnRollback.guarded(r, ctx, !reuse, () -> ec2Service.deleteSubnet(ctx.region(), subnet.getSubnetId()),
() -> undoInPlace(r), () -> {
applyTags(r, reuse, subnet.getSubnetId(), reuse ? existing.getTags() : List.of(), props, ctx);
if (mapPublicIpOnLaunch != null) {
if (reuse && ctx.isUpdate() && existing.isMapPublicIpOnLaunch() != Boolean.parseBoolean(mapPublicIpOnLaunch)) {
// A flag the previous template declared and this one drops goes back to false, the launch default.
String desiredMapPublicIp = mapPublicIpOnLaunch == null && reuse && ctx.droppedProperties(props).contains("MapPublicIpOnLaunch")
? "false" : mapPublicIpOnLaunch;
if (desiredMapPublicIp != null) {
if (reuse && ctx.isUpdate() && existing.isMapPublicIpOnLaunch() != Boolean.parseBoolean(desiredMapPublicIp)) {
ObjectNode prior = inPlacePrior(r, subnet.getSubnetId(), ctx);
prior.put("mapPublicIpOnLaunch", String.valueOf(existing.isMapPublicIpOnLaunch()));
writeInPlacePrior(r, prior);
}
ec2Service.modifySubnetAttribute(ctx.region(), subnet.getSubnetId(), "mapPublicIpOnLaunch", mapPublicIpOnLaunch);
ec2Service.modifySubnetAttribute(ctx.region(), subnet.getSubnetId(), "mapPublicIpOnLaunch", desiredMapPublicIp);
}
});
}
@@ -719,7 +724,7 @@ public class Ec2NetworkCfnProvisioner implements CfnResourceProvisioner {
*
* <p>Entries and Tags are each brought to the template only when it declares them, or the
* previous template did: a template that drops them clears them, and entries or tags no template
* ever declared are left alone. The other types here keep their tags when Tags is dropped.
* ever declared are left alone.
*/
private void updatePrefixList(StackResource r, ManagedPrefixList existing, String name, String family,
int maxEntries, List<PrefixListEntry> entries, JsonNode props, ProvisionContext ctx) {
@@ -755,20 +760,12 @@ public class Ec2NetworkCfnProvisioner implements CfnResourceProvisioner {
}
writeInPlacePrior(r, prior);
}
JsonNode desiredTags = !props.has("Tags") && ctx.previouslyDeclared("Tags") ? untagged(props) : props;
CfnRollback.guarded(r, ctx, false, () -> { }, () -> undoInPlace(r), () -> {
applyTags(r, true, prefixListId, existing.getTags(), desiredTags, ctx);
applyTags(r, true, prefixListId, existing.getTags(), props, ctx);
modifyPrefixList(ctx.region(), existing, name, maxEntries, changes);
});
}
/** {@code props} with an empty Tags list, which removes the tags a previous template declared. */
private static JsonNode untagged(JsonNode props) {
ObjectNode copy = props.deepCopy();
copy.putArray("Tags");
return copy;
}
/**
* Gives a prefix list {@code name} and {@code maxEntries} and applies {@code changes}, or does
* nothing when none of them changes it. EC2 refuses a request that removes and adds the same CIDR,