Skip to content

Commit 2c87522

Browse files
Add install, build and packages hooks.
* Add install, build and packages hooks. Issue: #6
1 parent cff2b3a commit 2c87522

File tree

15 files changed

+1114
-7
lines changed

15 files changed

+1114
-7
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
tests/test-func/test-out.log
2-
target
1+
tests/test-*/test-out.log
2+
target
3+
.DS_Store

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ $ docker run --rm \
5454
softprops/lambda-rust
5555
```
5656

57+
## ⚓ using hooks
58+
59+
If you want to customize certain parts of the build process, you can leverage hooks that this image provides.
60+
Hooks are just shell scripts that are invoked in a specific order, so you can customize the process as you wish. The following hooks exist:
61+
* `install`: run before `cargo build` - useful for installing native dependencies on the lambda environment
62+
* `build`: run after `cargo build`, but before packaging the executable into a zip - useful when modifying the executable after compilation
63+
* `package`: run after packaging the executable into a zip - useful for adding extra files into the zip file
64+
65+
The hooks' names are predefined and must be placed in a directory `.lambda-rust` in the project root.
66+
67+
You can take a look at an example [here](./tests/test-func-with-hooks).
68+
5769
## 🤸🤸 usage via cargo aws-lambda subcommand
5870

5971
If you want to set up ad hoc lambda functions or have another reason to not to go with full blown devops orchestration tools,

build.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# build and pack a rust lambda library
33
# https://aws.amazon.com/blogs/opensource/rust-runtime-for-aws-lambda/
44

5+
HOOKS_DIR="$PWD/.lambda-rust"
6+
INSTALL_HOOK="install"
7+
BUILD_HOOK="build"
8+
PACKAGE_HOOK="package"
9+
510
set -eo pipefail
611
mkdir -p target/lambda
712
export PROFILE=${PROFILE:-release}
@@ -18,6 +23,13 @@ export CARGO_TARGET_DIR=$PWD/target/lambda
1823
if [[ $# -gt 0 ]]; then
1924
yum install -y "$@"
2025
fi
26+
27+
if test -f "$HOOKS_DIR/$INSTALL_HOOK"; then
28+
echo "Running install hook"
29+
/bin/bash "$HOOKS_DIR/$INSTALL_HOOK"
30+
echo "Install hook ran successfully"
31+
fi
32+
2133
# source cargo
2234
. $HOME/.cargo/env
2335
# cargo only supports --release flag for release
@@ -27,6 +39,12 @@ export CARGO_TARGET_DIR=$PWD/target/lambda
2739
else
2840
cargo build ${CARGO_FLAGS:-}
2941
fi
42+
43+
if test -f "$HOOKS_DIR/$BUILD_HOOK"; then
44+
echo "Running build hook"
45+
/bin/bash "$HOOKS_DIR/$BUILD_HOOK"
46+
echo "Build hook ran successfully"
47+
fi
3048
) 1>&2
3149

3250
function package() {
@@ -45,6 +63,12 @@ function package() {
4563
fi
4664
zip "$file.zip" bootstrap
4765
rm bootstrap
66+
67+
if test -f "$HOOKS_DIR/$PACKAGE_HOOK"; then
68+
echo "Running package hook"
69+
/bin/bash "$HOOKS_DIR/$PACKAGE_HOOK" $file
70+
echo "Package hook ran successfully"
71+
fi
4872
}
4973

5074
cd "${CARGO_TARGET_DIR}/${TARGET_PROFILE}"

tests/test-func-with-hooks/.gitignore

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
3+
echo "build hook called"
4+
echo -n 'build,' >> /code/output.log
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
echo "install hook called"
4+
rm /code/output.log
5+
echo -n 'install,' >> /code/output.log
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
echo "package hook called"
4+
echo -n 'package' >> /code/output.log
5+
zip -j "$1.zip" /code/output.log

0 commit comments

Comments
 (0)