Skip to content

Commit aa56f8f

Browse files
author
sai-praveen-os
committed
feat(connectivity_check): implement pre-built Lambda package approach
Switch to pre-built Lambda package to avoid npm authentication issues during terraform apply. This allows the module to work across all consuming repos without requiring npm credentials. Changes: - Renamed handler.ts to index.ts (standard convention) - Updated main.tf to use pre-built lambda.zip instead of building from source - Created build-lambda.sh script to build Lambda package with dependencies - Added GitHub Actions workflow to auto-rebuild lambda.zip on code changes - Updated .gitignore to allow committing lambda.zip - Added README with usage and build instructions The lambda.zip will be built by GitHub Actions when Lambda code changes, bundling @janus.team/janus-core and other dependencies. PLATEXP-11106
1 parent 2e0b910 commit aa56f8f

6 files changed

Lines changed: 150 additions & 7 deletions

File tree

.github/workflows/build-lambda.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Build Connectivity Check Lambda
2+
3+
on:
4+
push:
5+
paths:
6+
- 'modules/connectivity_check/lambda/**'
7+
- 'modules/connectivity_check/scripts/build-lambda.sh'
8+
workflow_dispatch:
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Setup Node.js
17+
uses: actions/setup-node@v4
18+
with:
19+
node-version: '22'
20+
21+
- name: Configure npm authentication
22+
run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc
23+
24+
- name: Build Lambda package
25+
run: |
26+
chmod +x modules/connectivity_check/scripts/build-lambda.sh
27+
modules/connectivity_check/scripts/build-lambda.sh
28+
29+
- name: Commit lambda.zip if changed
30+
run: |
31+
git config user.name "github-actions[bot]"
32+
git config user.email "github-actions[bot]@users.noreply.github.com"
33+
git add modules/connectivity_check/lambda.zip
34+
git diff --staged --quiet || git commit -m "chore(connectivity_check): rebuild lambda.zip [skip ci]"
35+
git push

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ modules/lambda-layer-deps/examples/*/builds
88
modules/lambda-layer-deps/examples/*/node_modules
99
modules/lambda-layer-deps/examples/*/package.log
1010
.DS_Store
11+
12+
# Allow pre-built Lambda packages
13+
!modules/connectivity_check/lambda.zip
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Connectivity Check Module
2+
3+
Terraform module for deploying a Lambda function that tests TCP and HTTPS connectivity to specified endpoints and publishes metrics to Datadog.
4+
5+
## Lambda Package
6+
7+
The Lambda function uses a pre-built package (`lambda.zip`) that includes the `@janus.team/janus-core` dependency. This approach ensures the module works across all consuming repositories without requiring npm authentication during terraform apply.
8+
9+
### Rebuilding the Lambda Package
10+
11+
If you modify the Lambda code or dependencies:
12+
13+
```bash
14+
cd modules/connectivity_check
15+
./scripts/build-lambda.sh
16+
```
17+
18+
The GitHub Actions workflow will automatically rebuild the package when changes are pushed to the `lambda/` directory.
19+
20+
## Usage
21+
22+
```hcl
23+
module "connectivity_check" {
24+
source = "git@github.com:RSS-Engineering/terraform//modules/connectivity_check?ref=<commit-sha>"
25+
26+
function_name = "connectivity-check-primary"
27+
subnet_ids = ["subnet-xxx", "subnet-yyy"]
28+
security_group_ids = ["sg-xxx"]
29+
30+
enable_monitoring = true
31+
monitoring_schedule = "rate(1 minute)"
32+
monitoring_targets = [
33+
{
34+
host = "example.com"
35+
port = 443
36+
protocol = "https"
37+
critical = true
38+
}
39+
]
40+
41+
datadog_api_key = var.datadog_api_key
42+
janus_environment = var.environment
43+
}
44+
```
45+
46+
## Requirements
47+
48+
- Terraform >= 1.0
49+
- AWS Provider >= 5.0
50+
51+
## Inputs
52+
53+
| Name | Description | Type | Default | Required |
54+
|------|-------------|------|---------|----------|
55+
| function_name | Name of the Lambda function | string | - | yes |
56+
| subnet_ids | List of subnet IDs for Lambda | list(string) | - | yes |
57+
| security_group_ids | List of security group IDs | list(string) | - | yes |
58+
| enable_monitoring | Enable scheduled monitoring | bool | false | no |
59+
| monitoring_schedule | EventBridge schedule expression | string | "rate(1 minute)" | no |
60+
| monitoring_targets | List of endpoints to monitor | list(object) | [] | no |
61+
| datadog_api_key | Datadog API key | string | "" | no |
62+
| janus_environment | Environment name | string | "unknown" | no |
63+
| timeout | Lambda timeout in seconds | number | 10 | no |
64+
| memory_size | Lambda memory in MB | number | 128 | no |
65+
| log_retention_days | CloudWatch log retention | number | 30 | no |
66+
67+
## Outputs
68+
69+
| Name | Description |
70+
|------|-------------|
71+
| lambda_function_arn | ARN of the Lambda function |
72+
| lambda_function_name | Name of the Lambda function |
73+
| lambda_role_arn | ARN of the Lambda IAM role |
File renamed without changes.

modules/connectivity_check/main.tf

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ module "lambda" {
1717
handler = "index.handler"
1818
runtime = "nodejs22.x"
1919

20+
# Use pre-built Lambda package
21+
create_package = false
22+
local_existing_package = "${path.module}/lambda.zip"
23+
2024
layers = var.enable_monitoring ? [
2125
"arn:aws:lambda:${data.aws_region.current.name}:901920570463:layer:aws-otel-nodejs-amd64-ver-1-7-0:2"
2226
] : []
@@ -32,13 +36,6 @@ module "lambda" {
3236
} : {}
3337
)
3438

35-
source_path = [
36-
{
37-
path = "${path.module}/lambda"
38-
npm_requirements = true
39-
}
40-
]
41-
4239
timeout = var.timeout
4340
memory_size = var.memory_size
4441

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
set -e
3+
4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
MODULE_DIR="$(dirname "$SCRIPT_DIR")"
6+
LAMBDA_DIR="$MODULE_DIR/lambda"
7+
BUILD_DIR="$MODULE_DIR/lambda-build"
8+
OUTPUT_ZIP="$MODULE_DIR/lambda.zip"
9+
10+
echo "Building Lambda package with dependencies..."
11+
12+
# Clean previous build
13+
rm -rf "$BUILD_DIR"
14+
rm -f "$OUTPUT_ZIP"
15+
16+
# Create build directory
17+
mkdir -p "$BUILD_DIR"
18+
19+
# Copy Lambda source files
20+
cp "$LAMBDA_DIR/index.ts" "$BUILD_DIR/"
21+
cp "$LAMBDA_DIR/package.json" "$BUILD_DIR/"
22+
23+
# Install production dependencies
24+
cd "$BUILD_DIR"
25+
npm install --production --no-package-lock
26+
27+
# Create zip with handler and node_modules
28+
cd "$BUILD_DIR"
29+
zip -r "$OUTPUT_ZIP" index.ts node_modules/
30+
31+
echo "Lambda package created: $OUTPUT_ZIP"
32+
echo "Size: $(du -h "$OUTPUT_ZIP" | cut -f1)"
33+
34+
# Clean up build directory
35+
rm -rf "$BUILD_DIR"

0 commit comments

Comments
 (0)