Skip to content

Commit

Permalink
Add install, build and packages hooks.
Browse files Browse the repository at this point in the history
* Add install, build and packages hooks.

Issue: #6
  • Loading branch information
bernardobelchior committed Apr 12, 2020
1 parent cff2b3a commit 2c87522
Show file tree
Hide file tree
Showing 15 changed files with 1,114 additions and 7 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests/test-func/test-out.log
target
tests/test-*/test-out.log
target
.DS_Store
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ $ docker run --rm \
softprops/lambda-rust
```

## ⚓ using hooks

If you want to customize certain parts of the build process, you can leverage hooks that this image provides.
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:
* `install`: run before `cargo build` - useful for installing native dependencies on the lambda environment
* `build`: run after `cargo build`, but before packaging the executable into a zip - useful when modifying the executable after compilation
* `package`: run after packaging the executable into a zip - useful for adding extra files into the zip file

The hooks' names are predefined and must be placed in a directory `.lambda-rust` in the project root.

You can take a look at an example [here](./tests/test-func-with-hooks).

## 🤸🤸 usage via cargo aws-lambda subcommand

If you want to set up ad hoc lambda functions or have another reason to not to go with full blown devops orchestration tools,
Expand Down
24 changes: 24 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
# build and pack a rust lambda library
# https://aws.amazon.com/blogs/opensource/rust-runtime-for-aws-lambda/

HOOKS_DIR="$PWD/.lambda-rust"
INSTALL_HOOK="install"
BUILD_HOOK="build"
PACKAGE_HOOK="package"

set -eo pipefail
mkdir -p target/lambda
export PROFILE=${PROFILE:-release}
Expand All @@ -18,6 +23,13 @@ export CARGO_TARGET_DIR=$PWD/target/lambda
if [[ $# -gt 0 ]]; then
yum install -y "$@"
fi

if test -f "$HOOKS_DIR/$INSTALL_HOOK"; then
echo "Running install hook"
/bin/bash "$HOOKS_DIR/$INSTALL_HOOK"
echo "Install hook ran successfully"
fi

# source cargo
. $HOME/.cargo/env
# cargo only supports --release flag for release
Expand All @@ -27,6 +39,12 @@ export CARGO_TARGET_DIR=$PWD/target/lambda
else
cargo build ${CARGO_FLAGS:-}
fi

if test -f "$HOOKS_DIR/$BUILD_HOOK"; then
echo "Running build hook"
/bin/bash "$HOOKS_DIR/$BUILD_HOOK"
echo "Build hook ran successfully"
fi
) 1>&2

function package() {
Expand All @@ -45,6 +63,12 @@ function package() {
fi
zip "$file.zip" bootstrap
rm bootstrap

if test -f "$HOOKS_DIR/$PACKAGE_HOOK"; then
echo "Running package hook"
/bin/bash "$HOOKS_DIR/$PACKAGE_HOOK" $file
echo "Package hook ran successfully"
fi
}

cd "${CARGO_TARGET_DIR}/${TARGET_PROFILE}"
Expand Down
Empty file.
4 changes: 4 additions & 0 deletions tests/test-func-with-hooks/.lambda-rust/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

echo "build hook called"
echo -n 'build,' >> /code/output.log
5 changes: 5 additions & 0 deletions tests/test-func-with-hooks/.lambda-rust/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

echo "install hook called"
rm /code/output.log
echo -n 'install,' >> /code/output.log
5 changes: 5 additions & 0 deletions tests/test-func-with-hooks/.lambda-rust/package
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

echo "package hook called"
echo -n 'package' >> /code/output.log
zip -j "$1.zip" /code/output.log
Loading

0 comments on commit 2c87522

Please sign in to comment.