Skip to content

Instantly share code, notes, and snippets.

@ajaxray
Last active December 11, 2025 10:45
Show Gist options
  • Select an option

  • Save ajaxray/46999d811e67bb4635442a68d69213c0 to your computer and use it in GitHub Desktop.

Select an option

Save ajaxray/46999d811e67bb4635442a68d69213c0 to your computer and use it in GitHub Desktop.
Check health status of (all or filtered) AWS Lambda Functions

AWS Lambda Health Monitoring Bash Script

This is a robust but simple cli script to check Lambda health using most efficient way. It verifies Lambda health using two distinct signals:

  • Configuration State: Is the function Active and Successful? (Deployability check)
  • Runtime Health: Has it generated Errors in CloudWatch recently? (Operational check)

How it works

This script retrieves all functions (or filters by a pattern), checks their deployment state, and queries CloudWatch for error metrics over the last 10 minutes.

How to use

Save the script as lambda_healthcheck.sh and make it executable.

chmod +x lambda_healthcheck.sh

Then run it directly or filter by function name pattern:

# Check all functions
./lambda_healthcheck.sh

# Check only functions containing "payment"
./lambda_healthcheck.sh payment
#!/bin/bash
# Configuration
REGION=${AWS_REGION:-"us-east-1"}
PERIOD=600 # Check last 10 minutes (600 seconds)
PROFILE=${AWS_PROFILE:-"default"}
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo "Checking Lambda Health in region: $REGION..."
printf "%-30s %-15s %-15s %-10s\n" "FUNCTION NAME" "STATE" "UPDATE STATUS" "ERRORS (10m)"
echo "---------------------------------------------------------------------------"
# Get list of functions (optionally filtered by argument $1)
if [ -z "$1" ]; then
FUNCTIONS=$(aws lambda list-functions --region "$REGION" --profile "$PROFILE" --query 'Functions[*].FunctionName' --output text)
else
# Filter functions if an argument is provided (e.g. "prod-")
FUNCTIONS=$(aws lambda list-functions --region "$REGION" --profile "$PROFILE" --query "Functions[?contains(FunctionName, '$1')].FunctionName" --output text)
fi
# Time window for CloudWatch
START_TIME=$(date -u -v-${PERIOD}S +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -u -d "-${PERIOD} seconds" +%Y-%m-%dT%H:%M:%SZ)
END_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ)
for func in $FUNCTIONS; do
# 1. Get Configuration State
CONFIG=$(aws lambda get-function-configuration --function-name "$func" --region "$REGION" --profile "$PROFILE" --query '{State:State, LastUpdateStatus:LastUpdateStatus}' --output json 2>/dev/null)
STATE=$(echo "$CONFIG" | grep '"State":' | awk -F'"' '{print $4}')
UPDATE_STATUS=$(echo "$CONFIG" | grep '"LastUpdateStatus":' | awk -F'"' '{print $4}')
# 2. Get Error Metrics (Sum of Errors)
ERRORS=$(aws cloudwatch get-metric-statistics \
--namespace AWS/Lambda \
--metric-name Errors \
--dimensions Name=FunctionName,Value="$func" \
--start-time "$START_TIME" \
--end-time "$END_TIME" \
--period $PERIOD \
--statistics Sum \
--region "$REGION" \
--profile "$PROFILE" \
--query 'Datapoints[0].Sum' \
--output text)
# Handle "None" result from CloudWatch (means 0 data points)
if [ "$ERRORS" == "None" ]; then
ERRORS=0
fi
# Determine Color
COLOR=$GREEN
if [ "$STATE" != "Active" ] || [ "$UPDATE_STATUS" == "Failed" ]; then
COLOR=$YELLOW
fi
if [ $(echo "$ERRORS > 0" | bc -l) -eq 1 ]; then
COLOR=$RED
fi
printf "${COLOR}%-30s %-15s %-15s %-10s${NC}\n" "$func" "$STATE" "$UPDATE_STATUS" "$ERRORS"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment