-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdemo.sh
More file actions
executable file
·94 lines (79 loc) · 3.06 KB
/
Copy pathdemo.sh
File metadata and controls
executable file
·94 lines (79 loc) · 3.06 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
89
90
91
92
93
94
#!/bin/sh
set -eu
# We use the ENDPOINT provided by docker-compose, or localhost.
ENDPOINT="${ENDPOINT:-http://localhost:8000}"
export AWS_ACCESS_KEY_ID=test
export AWS_SECRET_ACCESS_KEY=test
export AWS_DEFAULT_REGION=us-east-1
AWS="aws --endpoint-url $ENDPOINT --no-cli-pager --output json"
WORK="$(mktemp -d)"
LAMBDA_SRC="${LAMBDA_SRC:-/lambda}"
if [ ! -d "$LAMBDA_SRC" ]; then
LAMBDA_SRC="$(cd "$(dirname "$0")" && pwd)/lambda"
fi
cleanup() {
rm -rf "$WORK"
}
trap cleanup EXIT
echo "=== Building Go zip Lambda ==="
cp -r "$LAMBDA_SRC"/* "$WORK"/
( cd "$WORK" && go mod tidy && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags lambda.norpc -o bootstrap . )
( cd "$WORK" && zip -q function.zip bootstrap )
# Copy the compiled binary back to our workspace so Terraform can zip it
cp "$WORK/bootstrap" ./bootstrap
echo ""
echo "=== Initializing and Applying Terraform/Tofu ==="
# Determine if we have tofu or terraform
TF=""
if command -v tofu >/dev/null 2>&1; then
TF="tofu"
elif command -v terraform >/dev/null 2>&1; then
TF="terraform"
else
echo "Neither tofu nor terraform found! Please install one to run this demo."
exit 1
fi
$TF init
$TF apply -auto-approve -var="provider_endpoint=$ENDPOINT" -var="lambda_endpoint=${LAMBDA_ENDPOINT:-http://host.docker.internal:8000}"
echo ""
echo "=== Uploading object to S3 ==="
$AWS s3api get-bucket-notification-configuration --bucket incoming-uploads
# Upload a file to the bucket. The endpoint handles s3:// directly, but we use path style.
echo "Hello from S3 Event Notifications!" > "$WORK/testfile.txt"
$AWS s3 cp "$WORK/testfile.txt" s3://incoming-uploads/testfile.txt
echo ""
echo "=== Waiting for Lambda execution ==="
# It can take a moment for the event to fire, the container to launch, and logs to flush.
# We will poll for up to 30 seconds.
SUCCESS=0
for i in $(seq 1 15); do
sleep 2
if $AWS logs filter-log-events --log-group-name /aws/lambda/s3-processor --query 'events[*].message' --output text 2>/dev/null | grep -q "Successfully read object!"; then
SUCCESS=1
break
fi
done
sleep 15
echo "=== Lambda Container Logs ==="
# Find all containers matching gopherstack-lambda-s3-processor
for container in $(curl -s --unix-socket /var/run/docker.sock http://localhost/containers/json?all=1 | grep -o 'gopherstack-lambda-s3-processor-[^"]*'); do
echo "Logs for $container:"
curl -s --unix-socket /var/run/docker.sock "http://localhost/containers/$container/logs?stdout=1&stderr=1"
done
echo "=== Lambda Logs ==="
for i in {1..10}; do
aws logs filter-log-events --log-group-name /aws/lambda/s3-processor --endpoint-url "$ENDPOINT" 2>/dev/null && break
sleep 2
done
if ! aws logs filter-log-events --log-group-name /aws/lambda/s3-processor --endpoint-url "$ENDPOINT" | grep -q "Successfully read object"; then
echo "FAIL: Could not find success message in Lambda logs."
exit 1
fi
if [ "$SUCCESS" -eq 1 ]; then
echo ""
echo "SUCCESS: Lambda correctly processed the S3 event notification!"
else
echo ""
echo "FAIL: Could not find success message in Lambda logs."
exit 1
fi