From c611722658c962ec0057c740bd84094ada5f3d65 Mon Sep 17 00:00:00 2001 From: Nicolas Froidure Date: Sat, 25 Sep 2021 14:40:25 +0200 Subject: [PATCH] feat(lambda): add a layer for lambdas --- .gitignore | 1 + package.json | 3 +- packages/whook-example/README.md | 6 ++ packages/whook-example/bin/lambda_layer.sh | 14 +++++ packages/whook-example/package.json | 3 +- .../src/__snapshots__/cli.test.ts.snap | 61 ------------------- packages/whook-example/src/cli.test.ts | 3 +- packages/whook-example/terraform/lambdas.tf | 12 ++++ 8 files changed, 39 insertions(+), 64 deletions(-) create mode 100755 packages/whook-example/bin/lambda_layer.sh delete mode 100644 packages/whook-example/src/__snapshots__/cli.test.ts.snap diff --git a/.gitignore b/.gitignore index 96bca390..f9a159dc 100644 --- a/.gitignore +++ b/.gitignore @@ -138,3 +138,4 @@ lambdas *.tfstate.d *.credentials.json .nx/cache +lambda_layer.zip diff --git a/package.json b/package.json index 1ff8cddf..fe22a946 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "*.plan", "*.tfstate.d", "*.credentials.json", - ".nx/cache" + ".nx/cache", + "lambda_layer.zip" ], "rootPackage": true } diff --git a/packages/whook-example/README.md b/packages/whook-example/README.md index 9d552611..27cb0f4b 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..6e8e23f5 --- /dev/null +++ b/packages/whook-example/bin/lambda_layer.sh @@ -0,0 +1,14 @@ +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:20" /bin/sh -c " + dnf update; + dnf install -y gcc gcc-c++ make; + mkdir .npm; + HOME=$(pwd); + npm i --production; + rm -rf .npm; + exit"; +env --chdir "$PWD/layer" zip -r ../lambda_layer.zip .; +docker run --entrypoint "" -v "$PWD/layer/nodejs":/var/task "public.ecr.aws/lambda/nodejs:20" /bin/sh -c "rm -rf node_modules; exit"; +rm -rf layer/nodejs; diff --git a/packages/whook-example/package.json b/packages/whook-example/package.json index 5a15b0d2..aef051d2 100644 --- a/packages/whook-example/package.json +++ b/packages/whook-example/package.json @@ -20,7 +20,8 @@ ".terraform", "*.plan", "*.tfstate.d", - "*.credentials.json" + "*.credentials.json", + "lambda_layer.zip" ], "bundleFiles": [ "bin", diff --git a/packages/whook-example/src/__snapshots__/cli.test.ts.snap b/packages/whook-example/src/__snapshots__/cli.test.ts.snap deleted file mode 100644 index a91e7997..00000000 --- a/packages/whook-example/src/__snapshots__/cli.test.ts.snap +++ /dev/null @@ -1,61 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`commands should work with config 1`] = ` -{ - "stderr": "⚡ - Loading configurations from /whook/packages/whook-example/dist/config/local/config.js". -", - "stdout": ""localhost" -", -} -`; - -exports[`commands should work with env 1`] = ` -{ - "stderr": "⚡ - Loading configurations from /whook/packages/whook-example/dist/config/local/config.js". -🔂 - Running with "test" node environment. -🔂 - Running with "local" application environment. -", - "stdout": "test -", -} -`; - -exports[`commands should work with ls 1`] = ` -{ - "stderr": "⚡ - Loading configurations from /whook/packages/whook-example/dist/config/local/config.js". -", - "stdout": " - -# Provided by "@whook/example": 2 commands -- printEnv: A command printing every env values -- terraformValues: A command printing lambdas informations for Terraform - - -# Provided by "@whook/whook": 8 commands -- config: A simple program that returns the queryed config value -- create: A command helping to create new Whook files easily -- env: A command printing env values -- generateOpenAPISchema: Write openAPI schema to stdout -- generateOpenAPITypes: Write openAPI types to stdout -- handler: Runs the given server handler for testing purpose -- inspect: A simple program that returns the result of the injected service -- ls: Print available commands - - -# Provided by "@whook/cors": none - - -# Provided by "@whook/authorization": none - - -# Provided by "@whook/aws-lambda": 7 commands -- testConsumerLambda: A command for testing AWS consumer lambda -- testCronLambda: A command for testing AWS cron lambda -- testHTTPLambda: A command for testing AWS HTTP lambda -- testKafkaConsumerLambda: A command for testing AWS lambda Kafka consumers -- testS3Lambda: A command for testing AWS consumer lambda -- testS3Lambda: A command for testing AWS consumer lambda -- testTransformerLambda: A command for testing AWS lambda transformers -", -} -`; diff --git a/packages/whook-example/src/cli.test.ts b/packages/whook-example/src/cli.test.ts index bc1e1048..992a5d42 100644 --- a/packages/whook-example/src/cli.test.ts +++ b/packages/whook-example/src/cli.test.ts @@ -17,8 +17,9 @@ describe('commands should work', () => { ", "stdout": " -# Provided by "@whook/example": 1 commands +# Provided by "@whook/example": 2 commands - printEnv: A command printing every env values +- terraformValues: A command printing lambdas informations for Terraform # Provided by "@whook/whook": 8 commands diff --git a/packages/whook-example/terraform/lambdas.tf b/packages/whook-example/terraform/lambdas.tf index b289a119..56e549ac 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 = ["nodejs20.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),