As you might infer from their names, the pre-hook runs before the deployment and the post-hook runs once the canary deployment is complete.
If you return a "Failure" from either hook, Code Deploy will automatically rollback your deployment.
The missing piece from the mentioned blog is writing a hook in Java. The only example is in Node.js.
I experimented for a day and now share my results. This should be enough to get you started writing AWS Code Deploy hooks in Java.
public class PreHook implements RequestHandler<Map<String, String>, PutLifecycleEventHookExecutionStatusResult> {
@Override
public PutLifecycleEventHookExecutionStatusResult handleRequest(Map<string, string> event, Context context) {
String deploymentId = event.get("DeploymentId");
String lifecycleEventHookExecutionId = event.get("LifecycleEventHookExecutionId");
PutLifecycleEventHookExecutionStatusRequest request = new PutLifecycleEventHookExecutionStatusRequest();
request.setDeploymentId(deploymentId);
request.setLifecycleEventHookExecutionId(lifecycleEventHookExecutionId);
// Do your integration tests here. If they pass, set the status to "Succeeded" otherwise "Failed"
// I set it to "Failed" to test rollback
request.setStatus("Failed");
AmazonCodeDeploy codeDeploy = AmazonCodeDeployClientBuilder.defaultClient();
return codeDeploy.putLifecycleEventHookExecutionStatus(request);
}
Like most Java Lambdas, you extend RequestHandler and parameterize it.
The input parameter is a Map of Strings. The map contains two very important pieces of information. The DeploymentId and the LifeCycleEventHookExecutionId.
Both are required to create a PuLifecycleEventHookExecutionStatusRequest.
PuLifecycleEventHookExecutionStatusRequest is required to create the output parameter PutLifecycleEventHookExecutionStatusResult by way of a AmazonCodeDeploy object.
Hopefully the code is enough to get you started. If not, please leave a comment or come find TheDarkSavant at the Serverless forums.
