fix(elbv2): default health checks by target group protocol (#9347)

CreateTargetGroup gave every target group an HTTP health check, so a
Network or Gateway Load Balancer target that does not answer HTTP with
the matcher's code stayed unhealthy. HealthCheckProtocol now defaults
to TCP, except for HTTP and HTTPS target groups and target type alb,
which keep HTTP, as the API reference states.

A health check uses HTTP, HTTPS or TCP. CreateTargetGroup and
ModifyTargetGroup refuse GENEVE, TLS, UDP, TCP_UDP, QUIC and TCP_QUIC,
and TCP for HTTP and HTTPS target groups and alb targets, naming the
protocols the target group takes as AWS does. A TCP check carries no
path or matcher, and moving one to HTTP or HTTPS takes both. A
CloudFormation rollback no longer sends a TCP check's stale path and
matcher back.

Closes #5824.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-24 08:40:07 -04:00
1 parent 3fbb0ced10
commit df8ccd0c52
7 files changed
+281 -23

No files matched your search

@@ -263,10 +263,7 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
}
TargetGroup prior = MAPPER.convertValue(saved, TargetGroup.class);
String arn = resource.getPhysicalId();
elbV2Service.modifyTargetGroup(AwsArnUtils.parse(arn).region(), arn, prior.getHealthCheckProtocol(),
prior.getHealthCheckPort(), prior.getHealthCheckEnabled(), prior.getHealthCheckPath(),
prior.getHealthCheckIntervalSeconds(), prior.getHealthCheckTimeoutSeconds(),
prior.getHealthyThresholdCount(), prior.getUnhealthyThresholdCount(), prior.getMatcher());
elbV2Service.restoreHealthCheck(AwsArnUtils.parse(arn).region(), arn, prior);
resource.getAttributes().remove(HEALTH_UPDATE_SNAPSHOT_ATTR);
}
@@ -860,7 +860,7 @@ public class ElbV2QueryHandler {
xml.elem("TargetGroupArn", tg.getTargetGroupArn());
xml.elem("TargetGroupName", tg.getTargetGroupName());
xml.elem("Protocol", safe(tg.getProtocol()));
if (isHttpProtocol(tg.getProtocol())) {
if (ElbV2Service.isHttpProtocol(tg.getProtocol())) {
xml.elem("ProtocolVersion", safe(tg.getProtocolVersion()));
}
if (tg.getPort() != null) xml.elem("Port", String.valueOf(tg.getPort()));
@@ -872,7 +872,7 @@ public class ElbV2QueryHandler {
if (tg.getHealthCheckTimeoutSeconds() != null) xml.elem("HealthCheckTimeoutSeconds", String.valueOf(tg.getHealthCheckTimeoutSeconds()));
if (tg.getHealthyThresholdCount() != null) xml.elem("HealthyThresholdCount", String.valueOf(tg.getHealthyThresholdCount()));
if (tg.getUnhealthyThresholdCount() != null) xml.elem("UnhealthyThresholdCount", String.valueOf(tg.getUnhealthyThresholdCount()));
if (isHttpProtocol(tg.getHealthCheckProtocol())) {
if (ElbV2Service.isHttpProtocol(tg.getHealthCheckProtocol())) {
xml.elem("HealthCheckPath", safe(tg.getHealthCheckPath()));
xml.start("Matcher").elem("HttpCode", safe(tg.getMatcher())).end("Matcher");
}
@@ -886,10 +886,6 @@ public class ElbV2QueryHandler {
return xml.build();
}
private static boolean isHttpProtocol(String protocol) {
return "HTTP".equals(protocol) || "HTTPS".equals(protocol);
}
private String listenerXml(Listener l) {
XmlBuilder xml = new XmlBuilder();
xml.elem("ListenerArn", l.getListenerArn());
@@ -583,6 +583,13 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
throw new AwsException("ValidationError", "A port must be specified", 400);
}
validateHealthCheckPort(healthCheckPort);
String groupProtocol = protocol != null ? protocol : "HTTP";
String groupTargetType = targetType != null ? targetType : "instance";
validateHealthCheckProtocol(healthCheckProtocol, groupProtocol, groupTargetType);
String checkProtocol = healthCheckProtocol != null ? healthCheckProtocol
: defaultHealthCheckProtocol(groupProtocol, groupTargetType);
validateLayer7Members(checkProtocol, healthCheckPath, matcher);
boolean layer7Check = isHttpProtocol(checkProtocol);
Map<String, TargetGroup> regionTgs = targetGroups.computeIfAbsent(region, k -> new ConcurrentHashMap<>());
boolean duplicate = regionTgs.values().stream()
.anyMatch(tg -> tg.getTargetGroupName().equals(name));
@@ -597,24 +604,24 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
TargetGroup tg = new TargetGroup();
tg.setTargetGroupArn(arn);
tg.setTargetGroupName(name);
tg.setProtocol(protocol != null ? protocol : "HTTP");
tg.setProtocol(groupProtocol);
tg.setProtocolVersion(protocolVersion != null ? protocolVersion : "HTTP1");
tg.setPort(port);
tg.setVpcId(vpcId);
tg.setTargetType(targetType != null ? targetType : "instance");
tg.setTargetType(groupTargetType);
tg.setIpAddressType(ipAddressType != null ? ipAddressType : "ipv4");
tg.setRegion(region);
// health check defaults
tg.setHealthCheckEnabled(healthCheckEnabled != null ? healthCheckEnabled : true);
tg.setHealthCheckProtocol(healthCheckProtocol != null ? healthCheckProtocol : "HTTP");
tg.setHealthCheckProtocol(checkProtocol);
tg.setHealthCheckPort(healthCheckPort != null ? healthCheckPort : "traffic-port");
tg.setHealthCheckPath(healthCheckPath != null ? healthCheckPath : "/");
tg.setHealthCheckPath(layer7Check ? (healthCheckPath != null ? healthCheckPath : "/") : null);
tg.setHealthCheckIntervalSeconds(healthCheckInterval != null ? healthCheckInterval : 30);
tg.setHealthCheckTimeoutSeconds(healthCheckTimeout != null ? healthCheckTimeout : 5);
tg.setHealthyThresholdCount(healthyThreshold != null ? healthyThreshold : 5);
tg.setUnhealthyThresholdCount(unhealthyThreshold != null ? unhealthyThreshold : 2);
tg.setMatcher(matcher != null ? matcher : "200");
tg.setMatcher(layer7Check ? (matcher != null ? matcher : "200") : null);
regionTgs.put(arn, tg);
targetGroups.put(region, regionTgs);
@@ -678,8 +685,20 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
validateHealthCheckTimeout(healthCheckTimeout);
TargetGroup tg = requireTargetGroup(region, arn);
validateHealthCheckPort(healthCheckPort);
validateHealthCheckProtocol(healthCheckProtocol, tg.getProtocol(), tg.getTargetType());
String checkProtocol = healthCheckProtocol != null ? healthCheckProtocol : tg.getHealthCheckProtocol();
validateLayer7Members(checkProtocol, healthCheckPath, matcher);
if (isHttpProtocol(healthCheckProtocol) && !isHttpProtocol(tg.getHealthCheckProtocol())
&& (healthCheckPath == null || matcher == null)) {
throw new AwsException("ValidationError", "Path and return code are required for layer 7 health checks.",
400);
}
Integer previousInterval = tg.getHealthCheckIntervalSeconds();
if (healthCheckProtocol != null) tg.setHealthCheckProtocol(healthCheckProtocol);
if (!isHttpProtocol(checkProtocol)) {
tg.setHealthCheckPath(null);
tg.setMatcher(null);
}
if (healthCheckPort != null) tg.setHealthCheckPort(healthCheckPort);
if (healthCheckEnabled != null) tg.setHealthCheckEnabled(healthCheckEnabled);
if (healthCheckPath != null) tg.setHealthCheckPath(healthCheckPath);
@@ -694,6 +713,71 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
}
}
/**
* Puts back the health check {@code prior} holds, as a CloudFormation rollback does. A TCP check has no path or
* matcher, so the ones a target group saved before TCP checks dropped them still holds are not put back.
*/
public void restoreHealthCheck(String region, String arn, TargetGroup prior) {
boolean layer7Check = isHttpProtocol(prior.getHealthCheckProtocol());
modifyTargetGroup(region, arn, prior.getHealthCheckProtocol(), prior.getHealthCheckPort(),
prior.getHealthCheckEnabled(), layer7Check ? prior.getHealthCheckPath() : null,
prior.getHealthCheckIntervalSeconds(), prior.getHealthCheckTimeoutSeconds(),
prior.getHealthyThresholdCount(), prior.getUnhealthyThresholdCount(),
layer7Check ? prior.getMatcher() : null);
}
/**
* The health check protocol a target group gets when the request names none: "For Application Load Balancers,
* the default is HTTP. For Network Load Balancers and Gateway Load Balancers, the default is TCP." An HTTP or
* HTTPS target group is an Application Load Balancer's, and one whose target is an Application Load Balancer
* supports only HTTP and HTTPS checks.
*/
static String defaultHealthCheckProtocol(String protocol, String targetType) {
return isHttpProtocol(protocol) || "alb".equals(targetType) ? "HTTP" : "TCP";
}
/**
* A health check uses HTTP, HTTPS or TCP: "The GENEVE, TLS, UDP, TCP_UDP, QUIC, and TCP_QUIC protocols are not
* supported for health checks", "the TCP protocol is not supported for health checks if the protocol of the
* target group is HTTP or HTTPS", and for a target type of {@code alb} "the supported health check protocols are
* HTTP and HTTPS". A protocol outside them is refused naming the ones the target group takes, as AWS refuses a
* TCP check of an HTTP target group: "Health check protocol 'TCP' must be one of 'HTTP, HTTPS'".
*/
private static void validateHealthCheckProtocol(String healthCheckProtocol, String protocol, String targetType) {
if (healthCheckProtocol == null) {
return;
}
List<String> supported = isHttpProtocol(protocol) || "alb".equals(targetType)
? List.of("HTTP", "HTTPS") : List.of("HTTP", "HTTPS", "TCP");
if (!supported.contains(healthCheckProtocol)) {
throw new AwsException("ValidationError", "Health check protocol '" + healthCheckProtocol
+ "' must be one of '" + String.join(", ", supported) + "'", 400);
}
}
/**
* {@code HealthCheckPath} and {@code Matcher} are "[HTTP/HTTPS health checks]" members, so a TCP check, which
* only opens a connection, carries neither. {@code protocol} is the protocol of the check the request leaves.
*/
private static void validateLayer7Members(String protocol, String healthCheckPath, String matcher) {
if (isHttpProtocol(protocol)) {
return;
}
if (healthCheckPath != null) {
throw new AwsException("InvalidConfigurationRequest",
"Health check paths are not supported for " + protocol + " health checks", 400);
}
if (matcher != null) {
throw new AwsException("InvalidConfigurationRequest",
"Health check matchers are not supported for " + protocol + " health checks", 400);
}
}
/** Whether {@code protocol} is HTTP or HTTPS, the protocols whose health checks have a path and a matcher. */
static boolean isHttpProtocol(String protocol) {
return "HTTP".equals(protocol) || "HTTPS".equals(protocol);
}
private static void validateHealthCheckTimeout(Integer timeout) {
if (timeout != null && (timeout < 2 || timeout > 120)) {
String constraint = timeout < 2 ? "greater than or equal to 2" : "less than or equal to 120";