AppSpec Versioned Validate Service - AWS CodeDeploy Script

Validate AWS CodeDeploy service health with this versioned AppSpec script. Ensure your application is healthy before proceeding with deployments.

AppSpec Versioned Validate Service

AWS CodeDeploy AppSpec Script for Service Validation

This script is designed to be used within an AWS CodeDeploy AppSpec file to validate the health of a service before proceeding with a deployment. It ensures that the application is responsive and ready to accept traffic.

Script Overview

The script performs the following actions:

  • Sets the script to exit immediately if a command exits with a non-zero status.
  • Generates a timestamp for logging purposes.
  • Continuously checks the health endpoint of the application (assumed to be http://localhost:8000/health) until it receives a successful response.
  • Logs a message indicating that the application is passing health checks.
  • Writes the current deployment ID to a version file, which can be useful for tracking deployed versions.

Usage in AppSpec

This script is typically placed in the AfterInstall or ApplicationStart hooks of your AppSpec file. Here's a conceptual example:

hooks:
  AfterInstall:
    - location: scripts/validate_service.sh
      timeout: 300
      runas: root
  ApplicationStart:
    - location: scripts/start_application.sh
      timeout: 300
      runas: root

External Resources

Code Snippet

#!/usr/bin/env bash
set -ex
DATESTAMP="$(date +%FT%H:%m)"

# Verify if the service is healthy
while ! curl -sf http://localhost:8000/health; do sleep 5; done

# log
echo "[${DATESTAMP}] application passing health checks"

# write current version to disk
echo "${DEPLOYMENT_ID}" > /opt/codedeploy-agent/deployment-root/${DEPLOYMENT_GROUP_ID}/.version