Skip to content

Commit

Permalink
feat(lambda): add a layer for lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Dec 20, 2022
1 parent bcee3dc commit 5512b55
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,4 @@ lambdas
*.plan
*.tfstate.d
*.credentials.json
lambda_layer.zip
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
".terraform",
"*.plan",
"*.tfstate.d",
"*.credentials.json"
"*.credentials.json",
"lambda_layer.zip"
],
"rootPackage": true
}
Expand Down
6 changes: 6 additions & 0 deletions packages/whook-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ Build the lambdas and the commands Terraform depends on:
NODE_ENV=staging npm run build
```

Build the lambdas layer:

```sh
NODE_ENV=staging bin/lambda_layer.sh
```

Plan the deployment:

```sh
Expand Down
7 changes: 7 additions & 0 deletions packages/whook-example/bin/lambda_layer.sh
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 2 additions & 1 deletion packages/whook-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
".terraform",
"*.plan",
"*.tfstate.d",
"*.credentials.json"
"*.credentials.json",
"lambda_layer.zip"
],
"bundleFiles": [
"bin",
Expand Down
19 changes: 15 additions & 4 deletions packages/whook-example/src/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,22 @@ exports[`commands should work with ls 1`] = `
",
"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/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
# Provided by "@whook/cors": none
# Provided by "@whook/whook": 8 commands
Expand All @@ -41,9 +55,6 @@ exports[`commands should work with ls 1`] = `
- 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
",
}
`;
12 changes: 12 additions & 0 deletions packages/whook-example/terraform/lambdas.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
Expand Down

0 comments on commit 5512b55

Please sign in to comment.