diff --git a/.gitignore b/.gitignore index 108c1625..7ef20f06 100644 --- a/.gitignore +++ b/.gitignore @@ -137,3 +137,4 @@ lambdas *.plan *.tfstate.d *.credentials.json +lambda_layer.zip diff --git a/package.json b/package.json index 9554bcfa..0349ef77 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ ".terraform", "*.plan", "*.tfstate.d", - "*.credentials.json" + "*.credentials.json", + "lambda_layer.zip" ], "rootPackage": true } diff --git a/packages/whook-example/README.md b/packages/whook-example/README.md index 0c12a65c..bbd031f0 100644 --- a/packages/whook-example/README.md +++ b/packages/whook-example/README.md @@ -104,6 +104,12 @@ Create a new workspace for each `APP_ENV`: ../.bin/terraform workspace new staging ``` +Build the lambdas layer: + +```sh +NODE_ENV=production bin/lambda_layer.sh +``` + Plan the deployment: ```sh diff --git a/packages/whook-example/bin/lambda_layer.sh b/packages/whook-example/bin/lambda_layer.sh new file mode 100755 index 00000000..36738745 --- /dev/null +++ b/packages/whook-example/bin/lambda_layer.sh @@ -0,0 +1,7 @@ +mkdir -p layer/nodejs; +cp package.json layer/nodejs/package.json; +cp package-lock.json layer/nodejs/package-lock.json; +docker run --entrypoint "" -v "$PWD/layer/nodejs":/var/task "public.ecr.aws/lambda/nodejs:14" /bin/sh -c "yum install -y gcc gcc-c++ make; npm i --production; sudo chown -R $USER /var/task; exit"; +env --chdir "$PWD/layer" zip -r ../lambda_layer.zip .; +docker run --entrypoint "" -v "$PWD/layer/nodejs":/var/task "public.ecr.aws/lambda/nodejs:14" /bin/sh -c "rm -rf node_modules; exit"; +rm -rf layer/nodejs; diff --git a/packages/whook-example/bin/lambdas_zip.sh b/packages/whook-example/bin/lambdas_zip.sh index 5a85e596..f75ccff7 100755 --- a/packages/whook-example/bin/lambdas_zip.sh +++ b/packages/whook-example/bin/lambdas_zip.sh @@ -1,7 +1,7 @@ #!/bin/bash set -e -o pipefail -u -APP_ENV=${APP_ENV:-development} +APP_ENV=${APP_ENV:-local} FILES=$(ls "builds/$APP_ENV") rm -rf "lambdas/$APP_ENV" diff --git a/packages/whook-example/package.json b/packages/whook-example/package.json index 87501b52..dd3ca234 100644 --- a/packages/whook-example/package.json +++ b/packages/whook-example/package.json @@ -21,7 +21,8 @@ ".terraform", "*.plan", "*.tfstate.d", - "*.credentials.json" + "*.credentials.json", + "lambda_layer.zip" ], "bundleFiles": [ "bin", diff --git a/packages/whook-example/terraform/lambdas.tf b/packages/whook-example/terraform/lambdas.tf index 7c1b2715..b67aa521 100644 --- a/packages/whook-example/terraform/lambdas.tf +++ b/packages/whook-example/terraform/lambdas.tf @@ -71,6 +71,17 @@ data "external" "envvars" { working_dir = ".." } +resource "aws_lambda_layer_version" "lambda_layer" { + filename = "../lambda_layer.zip" + layer_name = "api-lambda-layer" + description = "A layer with all lambdas node modules" + source_code_hash = filebase64sha256("../lambda_layer.zip") + # You may replace the above by the following to avoid pushing + # a new layer when the package lock did not change + # source_code_hash = filebase64sha256("../package-lock.json") + compatible_runtimes = ["nodejs14.x"] +} + data "archive_file" "lambdas" { for_each = data.external.lambdas.result type = "zip" @@ -88,6 +99,7 @@ resource "aws_lambda_function" "lambda_function" { source_code_hash = data.archive_file.lambdas[each.key].output_base64sha256 memory_size = split("|", each.value)[3] timeout = split("|", each.value)[2] + layers = [aws_lambda_layer_version.lambda_layer.arn] environment { variables = zipmap( keys(data.external.envvars.result),