Skip to content

Commit 2ec10b1

Browse files
committed
feat: 🎸 specify environment variables at build time
use variable `build_environment_variables` to specify environment variables to be used by the build process at build time in the worker lambda
1 parent 47a3f37 commit 2ec10b1

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ module "my_lambda" {
8686
| Name | Description | Type | Default | Required |
8787
| --------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------- | --------------------------------------------------- | :------: |
8888
| <a name="input_build_commands"></a> [build_commands](#input_build_commands) | Commands to run remotely on the worker lambda during build phase, in the directory root of `${var.package_source_path}` | `list(string)` | <pre>[<br> "npm ci",<br> "npm run build"<br>]</pre> | no |
89+
| <a name="input_build_environment_variables"></a> [build_environment_variables](#input_build_environment_variables) | A map that defines environment variables to use in the build process while building the lambda in the worker. | `map(string)` | `{}` | no |
8990
| <a name="input_cloudwatch_logs_enable"></a> [cloudwatch_logs_enable](#input_cloudwatch_logs_enable) | Enable logging to cloudwatch logs for the built lambda | `bool` | `true` | no |
9091
| <a name="input_cloudwatch_logs_retention"></a> [cloudwatch_logs_retention](#input_cloudwatch_logs_retention) | Log retention in days | `number` | `7` | no |
9192
| <a name="input_lambda_environment_variables"></a> [lambda_environment_variables](#input_lambda_environment_variables) | A map that defines environment variables for the built lambda. | `map(string)` | `{}` | no |

integration-test/lambda_source/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"main": "main.js",
1313
"scripts": {
14+
"prebuild": "mkdir -p dist && echo $RUNNER > dist/build.txt",
1415
"build": "tsc && cp *.json ./dist"
1516
},
1617
"devDependencies": {

integration-test/main.tf

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ module "integration_test" {
6060
"npm run build"
6161
]
6262

63-
package_target_include = ["*.js", "*.json"]
63+
build_environment_variables = {
64+
RUNNER = "integration_test"
65+
}
66+
67+
package_target_include = ["*.js", "*.json", "build.txt"]
6468
package_target_exclude = ["tsconfig.json", "*-lock.json"]
6569
}
6670

modules/lambda-worker/sources/worker/main.js

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const eventSource = "io.sellalong.lambda-cd-worker"
2525
/**
2626
* @typedef {object} WorkerBuildEvent
2727
* @property {string[]} commands
28+
* @property {Record<string, string>} environment
2829
* @property {WorkerBuildEventS3} s3
2930
*/
3031

@@ -45,6 +46,7 @@ function isBuildEvent(event) {
4546
e.source === eventSource &&
4647
typeof e.detail === "object" &&
4748
Array.isArray(e.detail.commands) &&
49+
typeof e.detail.environment === "object" &&
4850
typeof e.detail.s3 === "object" &&
4951
typeof e.detail.s3.sources === "object" &&
5052
typeof e.detail.s3.sources.bucket === "string" &&
@@ -280,6 +282,8 @@ module.exports.handler = async function handler(event) {
280282

281283
// Prioritise additional node cli tools when added as a layer(s)
282284
PATH: `/opt/nodejs/node_modules/.bin:${process.env.PATH}`,
285+
286+
...event.detail.environment,
283287
},
284288
})
285289

variables.tf

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ variable "build_commands" {
44
default = ["npm ci", "npm run build"]
55
}
66

7+
variable "build_environment_variables" {
8+
description = "A map that defines environment variables to use in the build process while building the lambda in the worker."
9+
type = map(string)
10+
default = {}
11+
}
712

813
variable "cloudwatch_logs_enable" {
914
description = "Enable logging to cloudwatch logs for the built lambda"

worker-invoke.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ data "aws_lambda_invocation" "worker_invoke_build" {
2020
source = "io.sellalong.lambda-cd-worker"
2121
"detail-type" = "BUILD"
2222
detail = {
23-
commands = var.build_commands
23+
commands = var.build_commands
24+
environment = var.build_environment_variables
2425
s3 = {
2526
sources = {
2627
bucket = local.package_sources.bucket

0 commit comments

Comments
 (0)