Skip to content

Commit 1b0e890

Browse files
authored
chore(aws-lambda): fixed and improved zip utility (#1608)
Extracted from #1315 - fixed bugs, cleaned up and clarified
1 parent da5bc43 commit 1b0e890

File tree

9 files changed

+215
-146
lines changed

9 files changed

+215
-146
lines changed

packages/aws-lambda/lambdas/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ Example Lambdas
33

44
Contains a few trivial example lambdas, mainly used to set up quick experiments.
55

6+
Manually creating the empty Lambda function
7+
-------------------------------------------
8+
9+
First, you need to manually create the lambda function in the target region.
10+
The name of the new function and the name of the zip file needs to match.
11+
612

713
Building The Lambda Zip Files
814
-----------------------------
915

16+
Add `.dont-add-npm-instana` file to the target folder to avoid adding the Instana npm packages and to use the layer instead.
17+
18+
Go to `cd packages/aws-lambda`.
1019
Run `bin/create-zip.sh <lambda-folder-name>` to create deployment zip files for one of Lambda functions in this folder. The resulting zip files can be found in the `zip` subfolder. They can be uploaded to AWS as Lambda functions. There is also a script to deploy the resulting zip file via the `aws` command line tool.
1120

1221
The environment variable `BUILD_LAMBDAS_WITH` controls how the Lambda zip files are being built:
@@ -21,11 +30,11 @@ Deploying Lambda Zip Files
2130

2231
Before you deploy zip files, you need to actually build them, see above.
2332

24-
Use `bin/deploy-zip.sh <lambda-folder-name>` to deploy a Lambda zip files. They will be deployed to region `us-east-2` by default. You can repeat that step as often as you like if the Lambda code has changed or you want to deploy zip files with a more recent npm package/local package/Lambda layer.
33+
Use `bin/deploy-zip.sh <zip-file>` to deploy a Lambda zip files. They will be deployed to region `us-east-2` by default. You can repeat that step as often as you like if the Lambda code has changed or you want to deploy zip files with a more recent npm package/local package/Lambda layer.
2534

2635
If you have built the zip files with `BUILD_LAMBDAS_WITH=layer`, the script will try to add the Lambda layer "instana-nodejs" to the deployed Lambda function. The script will try to figure out the latest version of the Instana Node.js Lambda layer. Alternatively, you can also use `LAYER_VERSION` and `LAYER_ARN` to specifiy which layer you want to have added. Checkout the latest layers here: https://www.ibm.com/docs/en/instana-observability/current?topic=lambda-aws-native-tracing-nodejs
2736

28-
E.g. run something like `LAYER_VERSION=167 LAYER_ARN=arn:aws:lambda:ap-southeast-1:767398002385:layer:instana-nodejs:167 bin/deploy-zip.sh`.
37+
E.g. run something like `LAYER_VERSION=167 LAYER_ARN=arn:aws:lambda:ap-southeast-1:767398002385:layer:instana-nodejs:167 bin/deploy-zip.sh <zip-file>`.
2938

3039
Note that if you have use `BUILD_LAMBDAS_WITH=npm` or `BUILD_LAMBDAS_WITH=local` and the function already has the Instana Lambda layer, the deploy script will try to remove it and revert the handler back to `index.handler`.
3140

packages/aws-lambda/lambdas/bin/create-zip-util

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function createZip {
22
name=${PWD##*/}
3-
echo "creating $name.zip"
3+
echo "create-zip-util: $name.zip with build option '$BUILD_LAMBDAS_WITH'"
44
rm -f $name.zip
55
mkdir -p ../zip
66
rm -f ../zip/$name.zip
@@ -16,7 +16,12 @@ function createZip {
1616
cp package-lock.json package-lock.json.backup
1717
fi
1818

19-
if [[ "${BUILD_LAMBDAS_WITH-}" == "layer" ]]; then
19+
# if package-lock.json.backup exists, the script crashed. Bring it back
20+
if [[ -f package-lock.json.backup ]]; then
21+
cp package-lock.json.backup package-lock.json
22+
fi
23+
24+
if [[ "${BUILD_LAMBDAS_WITH}" == "layer" ]]; then
2025
# We want to use the lambda layer and not the locally build npm package tar file.
2126
# If the lambda's package.json currently contains a dependency to the local tar files, like:
2227
# "dependencies": {
@@ -30,7 +35,7 @@ function createZip {
3035
npm uninstall --production -S @instana/serverless
3136
npm uninstall --production -S instana-aws-lambda-auto-wrap
3237

33-
elif [[ "${BUILD_LAMBDAS_WITH-}" == "local" ]]; then
38+
elif [[ "${BUILD_LAMBDAS_WITH}" == "local" ]]; then
3439
# The lambda's package.json might or might not already have a dependency to the local tar files:
3540
# "dependencies": {
3641
# "@instana/aws-lambda": "file:../../instana-aws-lambda.tgz",
@@ -44,12 +49,14 @@ function createZip {
4449
# We have to remove the lock file otherwise the integrity will fail.
4550
rm package-lock.json
4651

52+
echo "Installing local instana packages in folder $PWD"
53+
4754
npm --loglevel=warn --production install -S "../../instana-core.tgz"
4855
npm --loglevel=warn --production install -S "../../instana-aws-lambda-auto-wrap.tgz"
4956
npm --loglevel=warn --production install -S "../../instana-aws-lambda.tgz"
5057
npm --loglevel=warn --production install -S "../../instana-serverless.tgz"
5158

52-
elif [[ "${BUILD_LAMBDAS_WITH-npm}" == "npm" ]]; then
59+
elif [[ "${BUILD_LAMBDAS_WITH}" == "npm" ]]; then
5360
# The lambda's package.json might or might not already have a dependency to the local tar files:
5461
# "dependencies": {
5562
# "@instana/serverless": "file:../../instana-serverless.tgz",

packages/aws-lambda/lambdas/bin/create-zip.sh

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
set -eEuo pipefail
99

10-
cd `dirname $BASH_SOURCE`/..
10+
cd `dirname $BASH_SOURCE`/.. && pwd
1111

1212
pushd .. > /dev/null
1313

@@ -27,47 +27,48 @@ rm -rf instana-aws-lambda*.tgz
2727
# - BUILD_LAMBDAS_WITH=layer
2828
# See ../README.md for details.
2929

30-
# Note: ${BUILD_LAMBDAS_WITH-xyz} -> default expansion with default xyz, to avoid bash error "unbound variable"
31-
# when set -u is active.
32-
3330
echo
3431
echo NOTE: When switching between BUILD_LAMBDAS_WITH=layer on one hand and BUILD_LAMBDAS_WITH=npm or BUILD_LAMBDAS_WITH=local on the other hand, you might need to add \(or remove\) instana.wrap\(\) to \(from\) the individual Lambda handler functions!
3532
echo
3633

3734
echo Building all local tar.gz files.
3835

39-
if [[ -z "${BUILD_LAMBDAS_WITH-}" ]]; then
40-
echo "Environmant variable BUILD_LAMBDAS_WITH has not been provided, assuming \"npm\" (build with latest npm package)."
36+
if [[ -z "${BUILD_LAMBDAS_WITH}" ]]; then
37+
echo "Environment variable BUILD_LAMBDAS_WITH has not been provided, assuming \"local\"."
38+
BUILD_LAMBDAS_WITH=local
4139
fi
4240

4341
# We will want to install/uninstall local npm package tar files, which means we need to build them first. Note: Even for
4442
# options BUILD_LAMBDAS_WITH=layer or BUILD_LAMBDAS_WITH=npm, we need to build the packages, because we for all three
4543
# packages we will call "npm uninstall -S $package-name" and if the package.json points to the tar file it needs
4644
# to exist so npm can uninstall it and all its transitive dependencies.
45+
46+
version=$(jq -r '.version' package.json)
47+
4748
echo "Building local tar.gz for @instana/serverless."
4849
cd ../serverless
4950
npm --loglevel=warn pack
50-
mv instana-serverless-*.tgz ../aws-lambda/instana-serverless.tgz
51+
mv instana-serverless-${version}.tgz ../aws-lambda/instana-serverless.tgz
5152

5253
echo "Building local tar.gz for @instana/core."
5354
cd ../core
5455
npm --loglevel=warn pack
55-
mv instana-core-*.tgz ../aws-lambda/instana-core.tgz
56+
mv instana-core-${version}.tgz ../aws-lambda/instana-core.tgz
5657

5758
echo "Building local tar.gz for @instana/aws-lambda."
5859
cd ../aws-lambda
5960
npm --loglevel=warn pack
60-
mv instana-aws-lambda-*.tgz instana-aws-lambda.tgz
61+
mv instana-aws-lambda-${version}.tgz instana-aws-lambda.tgz
6162

6263
echo "Building local tar.gz for instana-aws-lambda-auto-wrap."
6364
cd ../aws-lambda-auto-wrap
6465
npm --loglevel=warn pack
65-
mv instana-aws-lambda-auto-wrap*.tgz ../aws-lambda/instana-aws-lambda-auto-wrap.tgz
66+
mv instana-aws-lambda-auto-wrap-${version}.tgz ../aws-lambda/instana-aws-lambda-auto-wrap.tgz
6667
cd ../aws-lambda
6768

68-
if [[ "${BUILD_LAMBDAS_WITH-npm}" == "npm" ]]; then
69+
if [[ "${BUILD_LAMBDAS_WITH}" == "npm" ]]; then
6970
echo "Building Lambda zip file(s) with the latest npm packages."
70-
elif [[ "${BUILD_LAMBDAS_WITH-}" == "local" ]]; then
71+
elif [[ "${BUILD_LAMBDAS_WITH}" == "local" ]]; then
7172
echo "Building Lambda zip file(s) with the local tar.gz files."
7273
elif [[ "$BUILD_LAMBDAS_WITH" == "layer" ]]; then
7374
echo "Building Lambda zip file(s) without @instana/aws-lambda, assuming the AWS Lambda layer \"instana-nodejs\" is (or will be) configured."
@@ -79,10 +80,11 @@ fi
7980

8081
popd > /dev/null
8182

82-
echo "Creating $1.zip"
83+
echo "Creating $1.zip with build option $BUILD_LAMBDAS_WITH."
84+
8385
lambda_directory=$1
8486
if [[ -d "$lambda_directory" && ! -L "$lambda_directory" && -e "$lambda_directory/bin/create-zip.sh" ]]; then
85-
$lambda_directory/bin/create-zip.sh
87+
BUILD_LAMBDAS_WITH=$BUILD_LAMBDAS_WITH $lambda_directory/bin/create-zip.sh
8688
else
8789
echo "Cannot create zip file for $lambda_directory, either the directory does not exist or is a symlink or it has no bin/create-zip.sh script."
8890
fi

0 commit comments

Comments
 (0)