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 Sep 28, 2021
1 parent d3a1ee5 commit 891ca4e
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 7 deletions.

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions packages/whook-create/src/services/createWhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ Mr Bean
"axios": "^0.21.4",
"babel-plugin-knifecycle": "^5.0.1",
"chokidar": "^3.5.1",
"esbuild": "^0.12.5",
"esbuild-node-externals": "^1.3.0",
"eslint": "^7.23.0",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
Expand Down Expand Up @@ -483,6 +485,8 @@ Mr Bean
"axios": "^0.21.4",
"babel-plugin-knifecycle": "^5.0.1",
"chokidar": "^3.5.1",
"esbuild": "^0.12.5",
"esbuild-node-externals": "^1.3.0",
"eslint": "^7.23.0",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
Expand Down Expand Up @@ -729,6 +733,8 @@ Mr Bean
"axios": "^0.21.4",
"babel-plugin-knifecycle": "^5.0.1",
"chokidar": "^3.5.1",
"esbuild": "^0.12.5",
"esbuild-node-externals": "^1.3.0",
"eslint": "^7.23.0",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
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 @@ -109,6 +109,12 @@ Build the lambdas:
NODE_ENV=staging npm run build
```

Build the lambdas layer:

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

Build the commands Terraform depends on:

```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;
5 changes: 4 additions & 1 deletion packages/whook-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"lambdas",
".terraform",
"*.plan",
"*.tfstate.d"
"*.tfstate.d",
"lambda_layer.zip"
],
"bundleFiles": [
"bin",
Expand Down Expand Up @@ -121,6 +122,8 @@
"axios": "^0.21.4",
"babel-plugin-knifecycle": "^5.0.1",
"chokidar": "^3.5.1",
"esbuild": "^0.12.5",
"esbuild-node-externals": "^1.3.0",
"eslint": "^7.23.0",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
Expand Down
5 changes: 5 additions & 0 deletions packages/whook-example/src/config/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ const CONFIG: AppConfigs = {
'User-Agent',
].join(','),
},
COMPILER_OPTIONS: {
externalModules: [],
ignoredModules: [],
excludeNodeModules: true,
},
};

export default CONFIG;
13 changes: 11 additions & 2 deletions packages/whook-example/terraform/lambdas.tf
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ 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("../package-lock.json")
compatible_runtimes = ["nodejs14.x"]
}

data "archive_file" "lambdas" {
for_each = data.external.lambdas.result
type = "zip"
Expand All @@ -88,6 +96,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 All @@ -98,7 +107,7 @@ resource "aws_lambda_function" "lambda_function" {
}

resource "aws_cloudwatch_log_group" "cloudwatch_log_group" {
for_each = data.external.lambdas.result
name = "/aws/lambda/${terraform.workspace}_${each.key}"
for_each = data.external.lambdas.result
name = "/aws/lambda/${terraform.workspace}_${each.key}"
retention_in_days = terraform.workspace == "production" ? 90 : 3
}

0 comments on commit 891ca4e

Please sign in to comment.