|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html |
| 4 | + |
| 5 | + |
| 6 | +# https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html#apigateway-example-event |
| 7 | +# https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format |
| 8 | +# https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format |
| 9 | + |
| 10 | +lambda_parse_event () { |
| 11 | + local EVENT="$1" |
| 12 | + # Extract commonly used fields |
| 13 | + if echo "$EVENT" | jq -e '.requestContext' >/dev/null; then |
| 14 | + export EVENT_BODY=$(echo "$EVENT" | jq -r '.body // empty') |
| 15 | + export EVENT_QUERY=$(echo "$EVENT" | jq -c '.queryStringParameters // {}') |
| 16 | + export EVENT_HEADERS=$(echo "$EVENT" | jq -c '.headers // {}') |
| 17 | + export EVENT_PATH=$(echo "$EVENT" | jq -r '.path // empty') |
| 18 | + export EVENT_HTTPMETHOD=$(echo "$EVENT" | jq -r '.httpMethod // empty') |
| 19 | + else |
| 20 | + # Handle other event types |
| 21 | + export EVENT_TYPE=$(echo "$EVENT" | jq -r 'keys[0]' 2>/dev/null || echo "unknown") |
| 22 | + fi |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +lambda_require_http_event () { |
| 27 | + if [ -z "$EVENT_HTTPMETHOD" ]; then |
| 28 | + lambda_error_response "Not an HTTP event" 400 |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | +} |
| 32 | + |
| 33 | +# JSON response with CORS |
| 34 | +lambda_json_response() { |
| 35 | + local data="$1" |
| 36 | + local status="${2:-200}" |
| 37 | + |
| 38 | + jq -n \ |
| 39 | + --arg data "$data" \ |
| 40 | + --arg status "$status" \ |
| 41 | + '{ |
| 42 | + statusCode: ($status|tonumber), |
| 43 | + body: $data, |
| 44 | + headers: { |
| 45 | + "Content-Type": "application/json", |
| 46 | + "Access-Control-Allow-Origin": "*" |
| 47 | + } |
| 48 | + }' |
| 49 | +} |
| 50 | + |
| 51 | +# Error response |
| 52 | +lambda_error_response() { |
| 53 | + local message="$1" |
| 54 | + local status="${2:-400}" |
| 55 | + lambda_json_response "$(jq -n --arg msg "$message" '{error: $msg}')" "$status" |
| 56 | +} |
| 57 | + |
| 58 | +# Success with data |
| 59 | +lambda_ok_response() { |
| 60 | + local data="$1" |
| 61 | + lambda_json_response "$data" 200 |
| 62 | +} |
| 63 | + |
| 64 | +# Not found |
| 65 | +lambda_not_found_response() { |
| 66 | + lambda_ok_response "Not found" 404 |
| 67 | +} |
| 68 | + |
| 69 | +# Method not allowed |
| 70 | +lambda_method_not_allowed_response() { |
| 71 | + lambda_ok_response "Method not allowed" 405 |
| 72 | +} |
| 73 | + |
| 74 | +# lambda_log function |
| 75 | +lambda_log() { |
| 76 | + local message="$1" |
| 77 | + local level="${2:-INFO}" |
| 78 | + local timestamp=$(date +"%Y-%m-%d %H:%M:%S") |
| 79 | + echo "$timestamp [$level] $message" >&2 |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +# lambda_log_error |
| 84 | +lambda_log_error() { |
| 85 | + lambda_log "$1" "ERROR" |
| 86 | +} |
| 87 | + |
| 88 | +# lambda_log_info |
| 89 | +lambda_log_info() { |
| 90 | + lambda_log "$1" "INFO" |
| 91 | +} |
| 92 | + |
| 93 | +# lambda_log_debug |
| 94 | +lambda_log_debug() { |
| 95 | + lambda_log "$1" "DEBUG" |
| 96 | +} |
| 97 | + |
| 98 | +# lambda_log_warn |
| 99 | +lambda_log_warn() { |
| 100 | + lambda_log "$1" "WARN" |
| 101 | +} |
| 102 | + |
0 commit comments