fix(codedeploy): fail closed on unreachable, erroring, or timed-out lifecycle hooks (#3563)

* fix(codedeploy): fail closed on unreachable, erroring, or timed-out lifecycle hooks

CodeDeploy lifecycle hooks failed open in several cases: a missing SSM
instance, an SSM execution exception, a Lambda invoke() error, and a hook
callback timeout were all silently treated as a successful hook. This
makes all of those cases finish the lifecycle event as Failed with an
AWS-compatible Diagnostics.errorCode and ErrorInformation.code
(HEALTH_CONSTRAINTS for Server, HOOK_EXECUTION_FAILURE for Lambda/ECS),
instead of swallowing the error and continuing.

invokeHook now polls a stop flag while waiting on a hook callback, so
StopDeployment interrupts that wait promptly instead of only after the
one-hour callback timeout elapses. A hook function that returns cleanly
without ever calling PutLifecycleEventHookExecutionStatus no longer
succeeds implicitly; it must explicitly call back, matching AWS's
documented AppSpec hooks contract.

* fix(codedeploy): poll SSM hook scripts for their declared timeout

* chore: rerun CI
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-14 15:54:49 -04:00
1 parent 5b4d1aff60
commit d167cc5bf3
3 files changed
+649 -33

No files matched your search

@@ -28,6 +28,8 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
@@ -36,7 +38,9 @@ import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
@@ -45,6 +49,10 @@ public class CodeDeployService {
private static final Logger LOG = Logger.getLogger(CodeDeployService.class);
// Lambda/ECS validation hooks must call PutLifecycleEventHookExecutionStatus within one hour.
private static final Duration DEFAULT_HOOK_CALLBACK_TIMEOUT = Duration.ofHours(1);
private static final Duration STOP_POLL_INTERVAL = Duration.ofMillis(200);
private final LambdaService lambdaService;
private final EcsService ecsService;
private final ElbV2Service elbV2Service;
@@ -54,12 +62,22 @@ public class CodeDeployService {
private final ObjectMapper yamlMapper;
private final RegionResolver regionResolver;
private final StorageFactory storageFactory;
private final Duration hookCallbackTimeout;
private final Clock clock;
@Inject
public CodeDeployService(LambdaService lambdaService, EcsService ecsService,
ElbV2Service elbV2Service, SsmCommandService ssmCommandService,
Ec2Service ec2Service, ObjectMapper mapper, RegionResolver regionResolver,
StorageFactory storageFactory) {
this(lambdaService, ecsService, elbV2Service, ssmCommandService, ec2Service, mapper,
regionResolver, storageFactory, DEFAULT_HOOK_CALLBACK_TIMEOUT, Clock.systemUTC());
}
CodeDeployService(LambdaService lambdaService, EcsService ecsService,
ElbV2Service elbV2Service, SsmCommandService ssmCommandService,
Ec2Service ec2Service, ObjectMapper mapper, RegionResolver regionResolver,
StorageFactory storageFactory, Duration hookCallbackTimeout, Clock clock) {
this.lambdaService = lambdaService;
this.ecsService = ecsService;
this.elbV2Service = elbV2Service;
@@ -69,6 +87,8 @@ public class CodeDeployService {
this.yamlMapper = new ObjectMapper(new YAMLFactory());
this.regionResolver = regionResolver;
this.storageFactory = storageFactory;
this.hookCallbackTimeout = hookCallbackTimeout;
this.clock = clock;
}
// ---- Durable (persisted) ----
@@ -845,8 +865,10 @@ public class CodeDeployService {
if (anyFailed) {
deployment.setStatus("Failed");
deployment.setErrorInformation(Map.of("code", "DeploymentFailed",
"message", "One or more instances failed deployment"));
deployment.setErrorInformation(Map.of("code", "HEALTH_CONSTRAINTS",
"message", "The overall deployment failed because too many individual instances failed "
+ "deployment, too few healthy instances are available for deployment, or some "
+ "instances in your deployment group are experiencing problems."));
} else {
deployment.setStatus("Succeeded");
}
@@ -925,8 +947,12 @@ public class CodeDeployService {
// Check if instance is registered with SSM
boolean hasSsm = ssmCommandService.isInstanceRegistered(instanceId, region);
if (!hasSsm) {
LOG.debugv("Instance {0} not in SSM, marking hook {1} as Succeeded", instanceId, location);
continue;
LOG.warnv("Instance {0} is not registered with SSM; cannot run hook script {1}",
instanceId, location);
finishLifecycleEvent(event, "Failed", "UnknownError",
"CodeDeploy agent was not able to receive the lifecycle event. Check the CodeDeploy agent "
+ "logs on your host and make sure the agent is running and can connect to the CodeDeploy server.");
return false;
}
try {
@@ -938,21 +964,31 @@ public class CodeDeployService {
instanceId, "AWS-RunShellScript", Map.of("commands", List.of(script)),
timeout, region);
// Poll until done (max timeout seconds, capped at 30s for emulator)
long deadline = System.currentTimeMillis() + Math.min(timeout * 1000L, 30_000L);
long deadline = clock.millis() + timeout * 1000L;
String invocationStatus = "InProgress";
while (System.currentTimeMillis() < deadline && "InProgress".equals(invocationStatus)) {
while (clock.millis() < deadline && "InProgress".equals(invocationStatus)) {
Thread.sleep(500);
invocationStatus = ssmCommandService.getCommandInvocationStatus(commandId, instanceId, region);
}
if (!"Success".equals(invocationStatus) && !"InProgress".equals(invocationStatus)) {
finishLifecycleEvent(event, "Failed");
if ("InProgress".equals(invocationStatus)) {
LOG.warnv("Hook script {0} on instance {1} did not finish within {2}s",
location, instanceId, timeout);
finishLifecycleEvent(event, "Failed", "ScriptTimedOut",
"Script at specified location: " + location + " failed to complete in " + timeout + " seconds");
return false;
}
if (!"Success".equals(invocationStatus)) {
finishLifecycleEvent(event, "Failed", "ScriptFailed",
"Script at specified location: " + location + " failed with SSM command status "
+ invocationStatus);
return false;
}
} catch (Exception e) {
LOG.debugv("SSM execution failed for {0} on {1}: {2}", location, instanceId, e.getMessage());
// Graceful degradation: if SSM fails, treat as succeeded
LOG.warnv(e, "SSM execution failed for {0} on {1}: {2}", location, instanceId, e.getMessage());
finishLifecycleEvent(event, "Failed", "UnknownError",
"Failed to execute script " + location + ": " + e.getMessage());
return false;
}
}
finishLifecycleEvent(event, "Succeeded");
@@ -1308,8 +1344,9 @@ public class CodeDeployService {
if (appSpec.beforeInstall != null) {
boolean ok = invokeHook(region, deployment, appSpec.beforeInstall,
"BeforeInstall", ecsTargetMap, stopFlag);
if (stopFlag.get()) { finishEcsStopped(deployment, ecsTargetMap); return; }
if (!ok) {
finishEcsFailed(deployment, ecsTargetMap, "BeforeInstallHookFailed",
finishEcsFailed(deployment, ecsTargetMap, "HOOK_EXECUTION_FAILURE",
"Hook function reported failure: BeforeInstall");
return;
}
@@ -1335,8 +1372,9 @@ public class CodeDeployService {
if (appSpec.afterInstall != null) {
boolean ok = invokeHook(region, deployment, appSpec.afterInstall,
"AfterInstall", ecsTargetMap, stopFlag);
if (stopFlag.get()) { finishEcsStopped(deployment, ecsTargetMap); return; }
if (!ok) {
finishEcsFailed(deployment, ecsTargetMap, "AfterInstallHookFailed",
finishEcsFailed(deployment, ecsTargetMap, "HOOK_EXECUTION_FAILURE",
"Hook function reported failure: AfterInstall");
return;
}
@@ -1347,8 +1385,9 @@ public class CodeDeployService {
if (appSpec.beforeAllowTraffic != null) {
boolean ok = invokeHook(region, deployment, appSpec.beforeAllowTraffic,
"BeforeAllowTraffic", ecsTargetMap, stopFlag);
if (stopFlag.get()) { finishEcsStopped(deployment, ecsTargetMap); return; }
if (!ok) {
finishEcsFailed(deployment, ecsTargetMap, "BeforeAllowTrafficHookFailed",
finishEcsFailed(deployment, ecsTargetMap, "HOOK_EXECUTION_FAILURE",
"Hook function reported failure: BeforeAllowTraffic");
return;
}
@@ -1370,8 +1409,9 @@ public class CodeDeployService {
if (appSpec.afterAllowTraffic != null) {
boolean ok = invokeHook(region, deployment, appSpec.afterAllowTraffic,
"AfterAllowTraffic", ecsTargetMap, stopFlag);
if (stopFlag.get()) { finishEcsStopped(deployment, ecsTargetMap); return; }
if (!ok) {
finishEcsFailed(deployment, ecsTargetMap, "AfterAllowTrafficHookFailed",
finishEcsFailed(deployment, ecsTargetMap, "HOOK_EXECUTION_FAILURE",
"Hook function reported failure: AfterAllowTraffic");
return;
}
@@ -1549,7 +1589,8 @@ public class CodeDeployService {
if (appSpec.beforeAllowTraffic != null) {
boolean ok = invokeHook(region, deployment, appSpec.beforeAllowTraffic,
"BeforeAllowTraffic", lambdaTargetMap, stopFlag);
if (!ok) { finishFailed(deployment, lambdaTargetMap, "BeforeAllowTrafficHookFailed",
if (stopFlag.get()) { finishStopped(deployment, lambdaTargetMap); return; }
if (!ok) { finishFailed(deployment, lambdaTargetMap, "HOOK_EXECUTION_FAILURE",
"Hook function reported failure: BeforeAllowTraffic"); return; }
}
@@ -1562,7 +1603,8 @@ public class CodeDeployService {
if (appSpec.afterAllowTraffic != null) {
boolean ok = invokeHook(region, deployment, appSpec.afterAllowTraffic,
"AfterAllowTraffic", lambdaTargetMap, stopFlag);
if (!ok) { finishFailed(deployment, lambdaTargetMap, "AfterAllowTrafficHookFailed",
if (stopFlag.get()) { finishStopped(deployment, lambdaTargetMap); return; }
if (!ok) { finishFailed(deployment, lambdaTargetMap, "HOOK_EXECUTION_FAILURE",
"Hook function reported failure: AfterAllowTraffic"); return; }
}
@@ -1651,26 +1693,59 @@ public class CodeDeployService {
String payload = "{\"DeploymentId\":\"" + deployment.getDeploymentId()
+ "\",\"LifecycleEventHookExecutionId\":\"" + executionId + "\"}";
String failureMessage = null;
try {
InvokeResult result = lambdaService.invoke(region, hookFunctionName,
payload.getBytes(), InvocationType.RequestResponse);
if (!future.isDone()) {
// Lambda didn't call PutLifecycleEventHookExecutionStatus; decide from invocation result
future.complete(result.getFunctionError() == null ? "Succeeded" : "Failed");
if (result.getFunctionError() != null && !future.isDone()) {
failureMessage = "Lambda function " + hookFunctionName
+ " returned a function error: " + result.getFunctionError();
future.complete("Failed");
}
} catch (Exception e) {
LOG.debugv("Hook Lambda {0} not invokable: {1}", hookFunctionName, e.getMessage());
future.complete("Succeeded");
LOG.warnv(e, "Hook Lambda {0} for lifecycle event {1} could not be invoked: {2}",
hookFunctionName, lifecycleEventName, e.getMessage());
failureMessage = "Lambda function " + hookFunctionName + " could not be invoked: " + e.getMessage();
if (!future.isDone()) {
future.complete("Failed");
}
}
String status = "Succeeded";
try {
status = future.get(30, TimeUnit.SECONDS);
} catch (Exception e) {
status = "Succeeded";
String status = null;
long deadlineNanos = System.nanoTime() + hookCallbackTimeout.toNanos();
while (status == null) {
if (stopFlag.get()) {
finishLifecycleEvent(event, "Skipped");
return false;
}
long remainingNanos = deadlineNanos - System.nanoTime();
if (remainingNanos <= 0) {
LOG.warnv("Hook {0} for lifecycle event {1} did not call PutLifecycleEventHookExecutionStatus within {2}",
hookFunctionName, lifecycleEventName, hookCallbackTimeout);
status = "Failed";
failureMessage = "Lambda function " + hookFunctionName
+ " did not call PutLifecycleEventHookExecutionStatus within "
+ hookCallbackTimeout.toSeconds() + " seconds";
break;
}
long sliceNanos = Math.min(remainingNanos, STOP_POLL_INTERVAL.toNanos());
try {
status = future.get(sliceNanos, TimeUnit.NANOSECONDS);
} catch (TimeoutException e) {
continue;
} catch (ExecutionException e) {
throw new IllegalStateException(e);
}
}
finishLifecycleEvent(event, status);
if ("Failed".equals(status)) {
finishLifecycleEvent(event, "Failed", "UnknownError",
failureMessage != null ? failureMessage
: "Lambda function " + hookFunctionName + " reported that lifecycle event "
+ lifecycleEventName + " failed");
} else {
finishLifecycleEvent(event, status);
}
return "Succeeded".equals(status);
} finally {
hookFutures.remove(executionId);
@@ -1696,6 +1771,12 @@ public class CodeDeployService {
event.put("status", status);
}
private void finishLifecycleEvent(Map<String, Object> event, String status,
String diagnosticsErrorCode, String diagnosticsMessage) {
finishLifecycleEvent(event, status);
event.put("diagnostics", Map.of("errorCode", diagnosticsErrorCode, "message", diagnosticsMessage));
}
private void updateTargetStatus(Map<String, Object> lambdaTargetMap, String status) {
lambdaTargetMap.put("status", status);
lambdaTargetMap.put("lastUpdatedAt", Instant.now().toEpochMilli() / 1000.0);