Skip to content

Commit 75cc057

Browse files
committed
Add utility scripts and SQL setup file
1 parent ecf4cb7 commit 75cc057

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
-- SQL script to set up PostgreSQL to call Lambda
2+
-- Replace <AWS_REGION> with your AWS region (e.g., us-east-1)
3+
-- Replace <POSTGRES_FUNCTION_NAME> with the name of your Lambda function
4+
5+
-- Create the aws_lambda extension
6+
CREATE EXTENSION IF NOT EXISTS aws_lambda CASCADE;
7+
8+
-- Create a function to process data using Lambda
9+
CREATE OR REPLACE FUNCTION process_data(data JSONB)
10+
RETURNS JSONB AS $$
11+
SELECT payload FROM aws_lambda.invoke(
12+
aws_commons.create_lambda_function_arn('<POSTGRES_FUNCTION_NAME>', '<AWS_REGION>'),
13+
json_build_object('action', 'process', 'data', data)::text,
14+
'Event'
15+
);
16+
$$ LANGUAGE SQL;
17+
18+
-- Create a function to transform data using Lambda
19+
CREATE OR REPLACE FUNCTION transform_data(data JSONB)
20+
RETURNS JSONB AS $$
21+
SELECT payload FROM aws_lambda.invoke(
22+
aws_commons.create_lambda_function_arn('<POSTGRES_FUNCTION_NAME>', '<AWS_REGION>'),
23+
json_build_object('action', 'transform', 'data', data)::text,
24+
'Event'
25+
);
26+
$$ LANGUAGE SQL;
27+
28+
-- Create a function to validate data using Lambda
29+
CREATE OR REPLACE FUNCTION validate_data(data JSONB)
30+
RETURNS JSONB AS $$
31+
SELECT payload FROM aws_lambda.invoke(
32+
aws_commons.create_lambda_function_arn('<POSTGRES_FUNCTION_NAME>', '<AWS_REGION>'),
33+
json_build_object('action', 'validate', 'data', data)::text,
34+
'Event'
35+
);
36+
$$ LANGUAGE SQL;
37+
38+
-- Test the functions
39+
SELECT process_data('{"id": 123, "value": "test"}'::JSONB);
40+
SELECT transform_data('{"id": 456, "value": "hello world"}'::JSONB);
41+
SELECT validate_data('{"id": 789, "value": "valid data"}'::JSONB);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
3+
# Test script for PostgreSQL Lambda integration
4+
5+
# Check if jq is installed
6+
if ! command -v jq &> /dev/null; then
7+
echo "Error: jq is required but not installed. Please install jq first."
8+
exit 1
9+
fi
10+
11+
# Function to display usage
12+
function display_usage {
13+
echo "Usage: $0 [options]"
14+
echo "Options:"
15+
echo " -f, --function-name NAME Lambda function name to test"
16+
echo " -m, --message MESSAGE Message to send to Lambda (default: 'Test message')"
17+
echo " -h, --help Display this help message"
18+
exit 1
19+
}
20+
21+
# Default values
22+
MESSAGE="Test message"
23+
24+
# Parse command line arguments
25+
while [[ $# -gt 0 ]]; do
26+
key="$1"
27+
case $key in
28+
-f|--function-name)
29+
FUNCTION_NAME="$2"
30+
shift
31+
shift
32+
;;
33+
-m|--message)
34+
MESSAGE="$2"
35+
shift
36+
shift
37+
;;
38+
-h|--help)
39+
display_usage
40+
;;
41+
*)
42+
echo "Unknown option: $1"
43+
display_usage
44+
;;
45+
esac
46+
done
47+
48+
# Check if function name is provided
49+
if [ -z "$FUNCTION_NAME" ]; then
50+
echo "Error: Lambda function name is required."
51+
display_usage
52+
fi
53+
54+
# Create payload
55+
PAYLOAD=$(echo "{\"message\": \"$MESSAGE\"}" | jq -c .)
56+
57+
# Create temporary file for response
58+
RESPONSE_FILE=$(mktemp)
59+
60+
echo "Invoking Lambda function: $FUNCTION_NAME"
61+
echo "Payload: $PAYLOAD"
62+
63+
# Invoke Lambda function
64+
aws lambda invoke \
65+
--function-name "$FUNCTION_NAME" \
66+
--payload "$PAYLOAD" \
67+
--cli-binary-format raw-in-base64-out \
68+
"$RESPONSE_FILE"
69+
70+
# Display response
71+
echo "Response:"
72+
cat "$RESPONSE_FILE" | jq .
73+
74+
# Clean up
75+
rm "$RESPONSE_FILE"

0 commit comments

Comments
 (0)