#!/usr/bin/env bash # Synchronous post-deploy smoke test for the PO and WO email-processor Lambdas. # # Invokes both functions with a {"healthcheck": true} payload using # RequestResponse (synchronous) invocation and asserts: # 1. The invoke response has no FunctionError field. A Lambda init failure # (e.g. an ImportError from a broken bundle) still returns HTTP 200 from # the Invoke API with FunctionError=Unhandled, so an exit-code-only check # would false-pass -- this script inspects the response JSON explicitly. # 2. The returned payload is exactly {"healthcheck": "ok"}. # # Exits non-zero on any failure (bad invoke, FunctionError set, wrong payload). set -euo pipefail REGION="us-east-1" FUNCTION_NAMES=("po-email-processor" "workorder-email-processor") HEALTHCHECK_PAYLOAD='{"healthcheck": true}' work_dir="$(mktemp -d)" trap 'rm -rf "${work_dir}"' EXIT overall_status=0 for function_name in "${FUNCTION_NAMES[@]}"; do echo "==> Smoke-testing ${function_name} (region ${REGION})" response_meta_file="${work_dir}/${function_name}.meta.json" response_payload_file="${work_dir}/${function_name}.payload.json" if ! aws lambda invoke \ --function-name "${function_name}" \ --invocation-type RequestResponse \ --payload "${HEALTHCHECK_PAYLOAD}" \ --cli-binary-format raw-in-base64-out \ --region "${REGION}" \ "${response_payload_file}" \ >"${response_meta_file}"; then echo "FAIL: ${function_name}: aws lambda invoke command failed" >&2 overall_status=1 continue fi function_error="$(python3 -c ' import json, sys with open(sys.argv[1]) as f: meta = json.load(f) print(meta.get("FunctionError", "")) ' "${response_meta_file}")" if [[ -n "${function_error}" ]]; then echo "FAIL: ${function_name}: invoke returned FunctionError=${function_error}" >&2 echo "----- response payload -----" >&2 cat "${response_payload_file}" >&2 echo >&2 overall_status=1 continue fi payload_check="$(python3 -c ' import json, sys try: with open(sys.argv[1]) as f: payload = json.load(f) except (json.JSONDecodeError, UnicodeDecodeError): print("unparseable") sys.exit(0) print("ok" if payload == {"healthcheck": "ok"} else "mismatch") ' "${response_payload_file}")" if [[ "${payload_check}" == "unparseable" ]]; then echo "FAIL: ${function_name}: response payload is not valid JSON:" >&2 cat "${response_payload_file}" >&2 echo >&2 overall_status=1 continue fi if [[ "${payload_check}" != "ok" ]]; then echo "FAIL: ${function_name}: unexpected healthcheck payload:" >&2 cat "${response_payload_file}" >&2 echo >&2 overall_status=1 continue fi echo "OK: ${function_name} healthcheck passed" done if [[ "${overall_status}" -ne 0 ]]; then echo "post-deploy smoke: one or more functions failed the healthcheck" >&2 exit 1 fi echo "post-deploy smoke: all functions healthy"