Skip to content

Commit ad036fd

Browse files
authored
feat: make helpers function available in the LAMBDA_TASK_ROOT folder (#6)
1 parent a8eaa8e commit ad036fd

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ RUN chmod +x /var/runtime/bootstrap
4747

4848
WORKDIR /var/task
4949

50-
COPY task/handler.sh handler.sh
50+
COPY task/handler.sh handler.sh
51+
COPY task/helpers.sh helpers.sh

micro.Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ RUN chmod +x /var/runtime/bootstrap
5353
WORKDIR /var/task
5454

5555
COPY task/handler.sh handler.sh
56+
COPY task/helpers.sh helpers.sh

task/helpers.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+

tiny.Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ RUN chmod +x /var/runtime/bootstrap
3434
WORKDIR /var/task
3535

3636
COPY task/handler.sh handler.sh
37+
COPY task/helpers.sh helpers.sh

0 commit comments

Comments
 (0)