Check HTTP Service is running after CodeDeploy

1 minute, 8 seconds Read

I have been using AWS CodeDeploy to deploy code from a private GitHub repository to a EC2 server. As part of the deployment, I wanted to check that the deployment was sucessful, by validating there was a HTTP 200 OK response from the server. As part of the deployment, the web server and application server are both stopped and restarted.

To do this, we can use an AppSpec hook to validate everything is working as expected. The image describes the order in which each AppSpec hook is executed. In my case, we want to check the status of the HTTP response after the ApplicationStart hook, therefore we’ll use the ValidateService hook.

To do this, simply update your AppSpec.yml file to include the following.

  ValidateService:
    - location: scripts/validate_service.sh
      timeout: 900
      runas: root

This will execute the validate_service.sh script located in your scripts folder as the root user upon deployment. If this script times (set to 15 mins) out the deployment will fail.

Your validate_service.sh should look something like this;validate_service.sh

#!/bin/sh
fetchstatus() {
  curl \
    -o /dev/null \
    --silent \
    --head \
    --write-out '%{http_code}' \
    "https://[your URL].com/"
}

urlstatus=$(fetchstatus)          # get the current HTTP response status
until [ "$urlstatus" = 200 ]; do  # until our result is success (200)...
  sleep 1                         # wait a second...
  urlstatus=$(fetchstatus)        # then check again.
done

Copyright 2003 – 2021

Similar Posts