engineering-handbook/scripts/health-check-template.sh

51 lines
1.7 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Post-deploy health check template.
# Copy to your project as scripts/health-check.sh and customize.
# Called automatically by CD workflows after deploy.
# Args: $1 = stack name, $2 = AWS region
set -euo pipefail
STACK_NAME="${1:?Stack name required}"
REGION="${2:-us-east-1}"
echo "Running health checks for ${STACK_NAME}..."
# Example: verify a Lambda function is invocable
# FUNCTION_NAME="${STACK_NAME}-process-order"
# aws lambda invoke \
# --function-name "$FUNCTION_NAME" \
# --payload '{"healthCheck": true}' \
# --region "$REGION" \
# /tmp/health-check-response.json
# cat /tmp/health-check-response.json
# Example: verify an EC2 instance is running
# INSTANCE_ID=$(aws cloudformation describe-stacks \
# --stack-name "$STACK_NAME" \
# --query 'Stacks[0].Outputs[?OutputKey==`InstanceId`].OutputValue' \
# --output text --region "$REGION")
# STATE=$(aws ec2 describe-instances \
# --instance-ids "$INSTANCE_ID" \
# --query 'Reservations[0].Instances[0].State.Name' \
# --output text --region "$REGION")
# if [[ "$STATE" != "running" ]]; then
# echo "ERROR: Instance $INSTANCE_ID is $STATE, expected running"
# exit 1
# fi
# echo "Instance $INSTANCE_ID is running."
# Example: verify an API Gateway endpoint responds
# API_URL=$(aws cloudformation describe-stacks \
# --stack-name "$STACK_NAME" \
# --query 'Stacks[0].Outputs[?OutputKey==`ApiUrl`].OutputValue' \
# --output text --region "$REGION")
# HTTP_CODE=$(curl -sf -o /dev/null -w '%{http_code}' "$API_URL/health")
# if [[ "$HTTP_CODE" != "200" ]]; then
# echo "ERROR: API health endpoint returned $HTTP_CODE"
# exit 1
# fi
# echo "API endpoint healthy."
echo "All health checks passed."