-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest-lambda.sh
More file actions
executable file
·88 lines (70 loc) · 2.12 KB
/
test-lambda.sh
File metadata and controls
executable file
·88 lines (70 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
set -e
# Configuration
LAMBDA_FUNCTION_NAME="pandoc-lambda-function"
AWS_REGION=${AWS_REGION:-"us-east-1"}
# Check if AWS CLI is installed
if ! command -v aws &> /dev/null; then
echo "AWS CLI not found. Please install it first."
exit 1
fi
# Create output directory
if [ ! -d "output" ]; then
mkdir -p output
fi
# Create a sample markdown file for testing
echo "Creating sample markdown file..."
cat > sample.md << 'EOL'
# Sample Markdown
This is a **sample** markdown file to test the Pandoc Lambda function.
## Features
- Document conversion
- Multiple formats
- AWS Lambda integration
## Code Example
```python
def hello_world():
print("Hello, World!")
```
EOL
# Encode the content as base64
CONTENT_BASE64=$(cat sample.md | base64)
# Create a test event file
echo "Creating test event file..."
cat > test-event.json << EOL
{
"input_format": "markdown",
"output_format": "html",
"content": "${CONTENT_BASE64}",
"options": ["--standalone", "--toc"]
}
EOL
# Invoke the Lambda function
echo "Invoking Lambda function ${LAMBDA_FUNCTION_NAME}..."
aws lambda invoke \
--function-name ${LAMBDA_FUNCTION_NAME} \
--region ${AWS_REGION} \
--payload file://test-event.json \
--cli-binary-format raw-in-base64-out \
output/lambda-response.json
# Check if the Lambda function was invoked successfully
if [ $? -ne 0 ]; then
echo "Failed to invoke Lambda function."
exit 1
fi
# Check the response
STATUS_CODE=$(cat output/lambda-response.json | grep -o '"statusCode":[0-9]*' | cut -d':' -f2)
if [ "$STATUS_CODE" == "200" ]; then
echo "Lambda function executed successfully!"
# Extract and decode the base64 content
RESPONSE_BODY=$(cat output/lambda-response.json | grep -o '"body":"[^"]*"' | cut -d':' -f2 | sed 's/"//g')
echo "$RESPONSE_BODY" | base64 --decode > output/lambda-output.html
echo "Output saved to output/lambda-output.html"
# Open the file if on macOS
if [[ "$OSTYPE" == "darwin"* ]]; then
open output/lambda-output.html
fi
else
echo "Lambda function execution failed. Status code: $STATUS_CODE"
cat output/lambda-response.json
fi