Skip to content

Commit e420a2e

Browse files
committed
Introduce SKIP_ZIPPING flag
This commit also changes the structure of `output/`. Since it is possible to build multiple binaries it is useful to have all of them in unzipped form. So now we put all binaries under `lambda/${PROFILE}/output/${BIN}`. Only `boostrap` and `boostrap.debug` files reside in this dir.
1 parent 5d7fbdc commit e420a2e

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 0.3.0-rust-1.44.1
2+
3+
* Put unzipped `boostrap` and `boostrap.debug` files under `target/lambda/${PROFILE}/output/${BIN}` dir
4+
to allow for using these artifacts without an intermediate `.zip` file creation step.
5+
* Introduce `$SKIP_ZIPPING` env var. Setting `-e SKIP_ZIPPING=1` prevents `.zip` archive from
6+
being created and `package` hook from running.
7+
18
# 0.2.7-rust-1.44.1
29

310
* Upgrade to Rust [`1.44.1`](https://blog.rust-lang.org/2020/06/18/Rust.1.44.1.html)
@@ -105,4 +112,4 @@
105112

106113
# 0.1.*
107114

108-
* Rust versions for `python 3.6` runtime targetting [rust crowbar](https://github.com/ilianaw/rust-crowbar) and [lando](https://github.com/softprops/lando) applications
115+
* Rust versions for `python 3.6` runtime targetting [rust crowbar](https://github.com/ilianaw/rust-crowbar) and [lando](https://github.com/softprops/lando) applications

README.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
This docker image extends [lambda ci `provided`](https://github.com/lambci/docker-lambda#documentation) builder docker image, a faithful reproduction of the actual AWS "**provided**" Lambda runtime environment,
77
and installs [rustup](https://rustup.rs/) and the *stable* rust toolchain.
88

9-
This provides a build environment, consistent with your target execution environment for predicable results.
9+
This provides a build environment, consistent with your target execution environment for predictable results.
1010

1111
## 📦 install
1212

@@ -21,7 +21,7 @@ You can also depend directly on `softprops/lambda-rust:latest` for the most rece
2121

2222
## 🤸 usage
2323

24-
The default docker entrypoint will build a packaged release optimized version your Rust artifact under `target/lambda/release` to
24+
The default docker entrypoint will build a packaged release optimized version of your Rust artifact under `target/lambda/release` to
2525
isolate the lambda specific build artifacts from your host-local build artifacts.
2626

2727
> **⚠️ Note:** you can switch from the `release` profile to a custom profile like `dev` by providing a `PROFILE` environment variable set to the name of the desired profile. i.e. `-e PROFILE=dev` in your docker run
@@ -30,7 +30,12 @@ isolate the lambda specific build artifacts from your host-local build artifacts
3030
3131
You will want to volume mount `/code` to the directory containing your cargo project.
3232

33-
You can pass additional flags to `cargo`, the Rust build tool, by setting the `CARGO_FLAGS` docker env variable
33+
You can pass additional flags to `cargo`, the Rust build tool, by setting the `CARGO_FLAGS` docker env variable.
34+
35+
Unzipped `boostrap` and `boostrap.debug` files are always available
36+
under `target/lambda/${PROFILE}/output/${BIN}` dir. If you want only them and don't
37+
need a `.zip` archive (e.g. for when running lambdas locally) pass `-e SKIP_ZIPPING=1`
38+
flag. More on that in [local testing](#-local-testing).
3439

3540
A typical docker run might look like the following.
3641

@@ -41,7 +46,6 @@ $ docker run --rm \
4146
-v ${HOME}/.cargo/git:/root/.cargo/git \
4247
softprops/lambda-rust
4348
```
44-
4549
> 💡 The -v (volume mount) flags for `/root/.cargo/{registry,git}` are optional but when supplied, provides a much faster turn around when doing iterative development
4650
4751
If you are using Windows, the command above may need to be modified to include
@@ -85,17 +89,32 @@ You can take a look at an example [here](./tests/test-func-with-hooks).
8589

8690
Once you've built a Rust lambda function artifact, the `provided` runtime expects
8791
deployments of that artifact to be named "**bootstrap**". The `lambda-rust` docker image
88-
builds a zip file, named after the binary, containing your binary files renamed to "bootstrap" for you.
92+
builds a zip file, named after the binary, containing your binary file renamed to "bootstrap" for you, but zip file creation is unnecessary for local development.
93+
94+
In order to prevent the creation of an intermediate `.zip` artifact when testing your lambdas locally, pass `-e SKIP_ZIPPING=1` during the build. After that the necessary
95+
output (not zipped) is available under `target/lambda/{profile}/output/{your-lambda-binary-name}` dir.
96+
You will see both `bootstrap` and `bootstrap.debug` files there.
97+
> **⚠️ Note:** `SKIP_ZIPPING=1` prevents `package` hook from running.
8998
90-
You can invoke this bootstap executable with the lambda-ci docker image for the `provided` AWS lambda runtime with a one off container.
99+
You can then invoke this bootstap executable with the lambda-ci docker image for the `provided` AWS lambda runtime with a one off container.
91100

92101
```sh
102+
# Build your function skipping the zip creation step
103+
# You may pass `-e PROFILE=dev` to build using dev profile, but here we use `release`
104+
docker run \
105+
-e SKIP_ZIPPING=1 \
106+
-e BIN={your-binary-name} \
107+
-v ${PWD}:/code \
108+
-v ${HOME}/.cargo/registry:/root/.cargo/registry \
109+
-v ${HOME}/.cargo/git:/root/.cargo/git \
110+
softprops/lambda-rust
111+
93112
# start a one-off docker container replicating the "provided" lambda runtime
94113
# awaiting an event to be provided via stdin
95114
$ docker run \
96115
-i -e DOCKER_LAMBDA_USE_STDIN=1 \
97116
--rm \
98-
-v ${PWD}/target/lambda/release:/var/task:ro,delegated \
117+
-v ${PWD}/target/lambda/release/output/{your-binary-name}:/var/task:ro,delegated \
99118
lambci/lambda:provided
100119

101120
# provide an event payload via stdin (typically a json blob)

build.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,25 @@ export CARGO_TARGET_DIR=$PWD/target/lambda
5252

5353
function package() {
5454
file="$1"
55-
OUTPUT_FOLDER="output"
55+
OUTPUT_FOLDER="output/${file}"
5656
if [[ "${PROFILE}" == "release" ]] && [[ -z "${DEBUGINFO}" ]]; then
5757
objcopy --only-keep-debug "$file" "$file.debug"
5858
objcopy --strip-debug --strip-unneeded "$file"
5959
objcopy --add-gnu-debuglink="$file.debug" "$file"
6060
fi
6161
rm "$file.zip" > 2&>/dev/null || true
6262
rm -r "${OUTPUT_FOLDER}" > 2&>/dev/null || true
63-
mkdir "${OUTPUT_FOLDER}"
63+
mkdir -p "${OUTPUT_FOLDER}"
6464
cp "${file}" "${OUTPUT_FOLDER}/bootstrap"
6565
cp "${file}.debug" "${OUTPUT_FOLDER}/bootstrap.debug" > 2&>/dev/null || true
66-
zip -j "$file.zip" "${OUTPUT_FOLDER}/bootstrap"
6766

68-
if test -f "$HOOKS_DIR/$PACKAGE_HOOK"; then
69-
echo "Running package hook"
70-
/bin/bash "$HOOKS_DIR/$PACKAGE_HOOK" $file
71-
echo "Package hook ran successfully"
67+
if [[ -z "$SKIP_ZIPPING" ]]; then
68+
zip -j "$file.zip" "${OUTPUT_FOLDER}/bootstrap"
69+
if test -f "$HOOKS_DIR/$PACKAGE_HOOK"; then
70+
echo "Running package hook"
71+
/bin/bash "$HOOKS_DIR/$PACKAGE_HOOK" $file
72+
echo "Package hook ran successfully"
73+
fi
7274
fi
7375
}
7476

tests/test.sh

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ IMAGE=${1:-softprops/lambda-rust}
88

99
source "${HERE}"/bashtest.sh
1010

11-
# test packaing with a single binary
11+
# test packaging with a single binary
1212
package_bin() {
1313
rm -rf target/lambda/release > /dev/null 2>&1
1414
docker run --rm \
@@ -17,7 +17,9 @@ package_bin() {
1717
-v "${HOME}"/.cargo/registry:/root/.cargo/registry \
1818
-v "${HOME}"/.cargo/git:/root/.cargo/git \
1919
${IMAGE} && \
20-
ls target/lambda/release/"$1".zip > /dev/null 2>&1
20+
ls target/lambda/release/"${1}".zip > /dev/null 2>&1 &&
21+
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
22+
ls target/lambda/release/output/"${1}"/bootstrap.debug 2>&1
2123
}
2224

2325
# test packaging all binaries
@@ -28,7 +30,23 @@ package_all() {
2830
-v "${HOME}"/.cargo/registry:/root/.cargo/registry \
2931
-v "${HOME}"/.cargo/git:/root/.cargo/git \
3032
${IMAGE} && \
31-
ls target/lambda/release/"${1}".zip > /dev/null 2>&1
33+
ls target/lambda/release/"${1}".zip > /dev/null 2>&1 &&
34+
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
35+
ls target/lambda/release/output/"${1}"/bootstrap.debug 2>&1
36+
}
37+
38+
# test SKIP_ZIPPING=1 flag
39+
compile_without_zipping() {
40+
rm -rf target/lambda/release > /dev/null 2>&1
41+
docker run --rm \
42+
-e SKIP_ZIPPING=1 \
43+
-v "${PWD}":/code \
44+
-v "${HOME}"/.cargo/registry:/root/.cargo/registry \
45+
-v "${HOME}"/.cargo/git:/root/.cargo/git \
46+
${IMAGE} &&
47+
!(ls target/lambda/release/"${1}".zip > /dev/null 2>&1) &&
48+
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
49+
ls target/lambda/release/output/"${1}"/bootstrap.debug 2>&1
3250
}
3351

3452
# test packaging with PROFILE=dev
@@ -40,7 +58,9 @@ package_all_dev_profile() {
4058
-v "${HOME}"/.cargo/registry:/root/.cargo/registry \
4159
-v "${HOME}"/.cargo/git:/root/.cargo/git \
4260
${IMAGE} && \
43-
ls target/lambda/debug/"${1}".zip > /dev/null 2>&1
61+
ls target/lambda/debug/"${1}".zip > /dev/null 2>&1 &&
62+
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
63+
ls target/lambda/release/output/"${1}"/bootstrap.debug 2>&1
4464
}
4565

4666
for project in test-func test-multi-func test-func-with-hooks; do
@@ -58,6 +78,8 @@ for project in test-func test-multi-func test-func-with-hooks; do
5878

5979
assert "it packages all bins with dev profile" package_all_dev_profile "${bin_name}"
6080

81+
assert "it compiles the binaries without zipping when SKIP_ZIPPING=1" compile_without_zipping "${bin_name}"
82+
6183
assert "it packages all bins" package_all "${bin_name}"
6284

6385
# verify packaged artifact by invoking it using the lambdaci "provided" docker image

0 commit comments

Comments
 (0)