11#! /bin/bash
22
33set -ex
4- arch=$( uname -a)
5- cd ../bottlecap
6- # build bottlecap in debug mode
7- if (echo $arch | grep -q " Darwin" ); then
8- PATH=/usr/bin:$PATH cargo zigbuild --target=aarch64-unknown-linux-gnu
9- build_path=../bottlecap/target/aarch64-unknown-linux-gnu/debug/bottlecap
4+
5+ # Setup cleanup trap to ensure docker container is stopped and removed even if script is interrupted
6+ cleanup () {
7+ if [ -n " ${docker_name} " ]; then
8+ echo " Cleaning up Docker container..."
9+ docker stop " ${docker_name} " 2> /dev/null || true
10+ docker rm " ${docker_name} " 2> /dev/null || true
11+ fi
12+ }
13+
14+ # Register trap for EXIT, INT (Ctrl+C), TERM, and ERR
15+ trap cleanup EXIT INT TERM ERR
16+
17+ if [ -z " $PREBUILT_BUILD_PATH " ]; then
18+ cd ../bottlecap
19+ arch=$( uname -a)
20+ # build bottlecap in debug mode
21+ if (echo $arch | grep -q " Darwin" ); then
22+ PATH=/usr/bin:$PATH cargo zigbuild --target=aarch64-unknown-linux-gnu
23+ build_path=../bottlecap/target/aarch64-unknown-linux-gnu/debug/bottlecap
24+ else
25+ cargo build
26+ build_path=../bottlecap/target/debug/bottlecap
27+ fi
28+ cd -
29+
1030else
11- cargo build
12- build_path=../bottlecap/target/debug/bottlecap
31+ echo " using a prebuilt bottlecap from $PREBUILT_BUILD_PATH "
32+ build_path=" $PREBUILT_BUILD_PATH "
1333fi
14- cd -
1534
1635# run a hello world function in Lambda RIE (https://github.com/aws/aws-lambda-runtime-interface-emulator)
1736# the lambda_extension binary is copied to /opt/extensions
1837docker_name=$( docker create \
1938 --publish 9000:8080 \
2039 -e DD_API_KEY=XXX \
40+ -e DD_SERVERLESS_FLUSH_STRATEGY=' periodically,1' \
41+ -e DD_LOG_LEVEL=debug \
42+ -e RUST_BACKTRACE=full \
43+ -e DD_ENV=dev \
44+ -e DD_VERSION=1 \
2145 " public.ecr.aws/lambda/nodejs:20" " index.handler" )
2246echo -e ' export const handler = async () => {\n\tconsole.log("Hello world!");\n};' > /tmp/index.mjs
2347docker cp " /tmp/index.mjs" " ${docker_name} :/var/task/index.mjs"
@@ -26,5 +50,3 @@ docker exec "${docker_name}" mkdir -p /opt/extensions
2650docker cp " ${build_path} " " ${docker_name} :/opt/extensions/datadog-agent"
2751curl -XPOST " http://localhost:9000/2015-03-31/functions/function/invocations" -d ' {}'
2852docker logs " ${docker_name} "
29- docker stop " ${docker_name} "
30- docker rm " ${docker_name} "
0 commit comments