Skip to content

Commit 11963a6

Browse files
feat!: use cloud-front function alongside with lamda-at-edge
1 parent 4e1b4ed commit 11963a6

30 files changed

Lines changed: 669 additions & 62 deletions

.github/dependabot.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ version: 2
77
updates:
88
- package-ecosystem: "npm" # See documentation for possible values
99
directories:
10-
- "modules/lambda-at-edge/edge-functions"
11-
- "modules/lambda-at-edge/edge-functions/packages/**" # Location of package manifests
10+
- "lib/lambda-at-edge"
11+
- "lib/lambda-at-edge/packages/**"
12+
- "lib/cloudfront-functions"
13+
- "lib/cloudfront-functions/packages/**" # Location of package manifests
14+
schedule:
15+
interval: "weekly"
16+
- package-ecosystem: "github-actions"
17+
directory: "/"
1218
schedule:
1319
interval: "weekly"

.github/workflows/release-cloudfront-functions.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ jobs:
2323
runs-on: ubuntu-latest
2424
steps:
2525
- name: Checkout
26-
uses: actions/checkout@v4
26+
uses: actions/checkout@v7
2727
with:
2828
fetch-depth: 0
2929
persist-credentials: false
3030

3131
- name: Setup Node.js
32-
uses: actions/setup-node@v4
32+
uses: actions/setup-node@v7
3333
with:
3434
node-version-file: lib/cloudfront-functions/.nvmrc
3535
registry-url: https://npm.pkg.github.com

.github/workflows/release-lambda-at-edge.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ jobs:
3232
- response-handler
3333
steps:
3434
- name: Checkout
35-
uses: actions/checkout@v4
35+
uses: actions/checkout@v7
3636
with:
3737
fetch-depth: 0
3838
persist-credentials: false
3939

4040
- name: Setup Node.js
41-
uses: actions/setup-node@v4
41+
uses: actions/setup-node@v7
4242
with:
4343
node-version-file: lib/lambda-at-edge/.nvmrc
4444
registry-url: https://npm.pkg.github.com
@@ -52,6 +52,28 @@ jobs:
5252
run: yarn build
5353
working-directory: lib/lambda-at-edge
5454

55+
# --- S3 mirror publish (DISABLED) -------------------------------------
56+
# This repo does not deploy the Lambda@Edge functions, so a release only
57+
# publishes the npm tarball to GitHub Packages + a GitHub Release. The
58+
# optional S3 mirror step -- which copied build/index.js to
59+
# s3://$EDGE_ARTIFACTS_BUCKET/lambda-at-edge/<fn>/<version>.zip for the
60+
# Terraform edge-functions module to deploy from -- is turned off in two
61+
# coupled places; re-enable BOTH together:
62+
# 1. this "Configure AWS credentials" step, and
63+
# 2. the "@semantic-release/exec" plugin block in every
64+
# lib/lambda-at-edge/packages/*/.releaserc.json (currently removed).
65+
# Keeping the exec block without these credentials makes `aws s3 cp` fail,
66+
# which fails the whole semantic-release run (no npm publish either).
67+
# Also needs repo secrets EDGE_PUBLISHER_AWS_ACCESS_KEY_ID /
68+
# EDGE_PUBLISHER_AWS_SECRET_ACCESS_KEY and repo var EDGE_ARTIFACTS_BUCKET.
69+
# - name: Configure AWS credentials
70+
# uses: aws-actions/configure-aws-credentials@v6
71+
# with:
72+
# aws-access-key-id: ${{ secrets.EDGE_PUBLISHER_AWS_ACCESS_KEY_ID }}
73+
# aws-secret-access-key: ${{ secrets.EDGE_PUBLISHER_AWS_SECRET_ACCESS_KEY }}
74+
# aws-region: us-east-1
75+
# --------------------------------------------------------------------
76+
5577
- name: Release
5678
run: npx --no-install semantic-release
5779
working-directory: lib/lambda-at-edge/packages/${{ matrix.package }}

.github/workflows/test.yml

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,39 @@ name: Test
33
on:
44
push:
55
branches:
6-
- master
6+
- main
77
pull_request:
88

99
jobs:
1010
test:
11-
name: Run Tests
11+
name: Run Tests (${{ matrix.workspace }})
1212
runs-on: ubuntu-latest
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
workspace:
17+
- lib/lambda-at-edge
18+
- lib/cloudfront-functions
1319

1420
steps:
1521
- name: Checkout
16-
uses: actions/checkout@v4
22+
uses: actions/checkout@v7
1723

1824
- name: Setup Node.js
19-
uses: actions/setup-node@v4
25+
uses: actions/setup-node@v7
2026
with:
21-
node-version: "20"
27+
node-version-file: ${{ matrix.workspace }}/.nvmrc
2228
cache: yarn
23-
cache-dependency-path: modules/lambda-at-edge/edge-functions/yarn.lock
29+
cache-dependency-path: ${{ matrix.workspace }}/yarn.lock
2430

2531
- name: Install dependencies
26-
run: yarn install
27-
working-directory: modules/lambda-at-edge/edge-functions
32+
run: yarn install --frozen-lockfile
33+
working-directory: ${{ matrix.workspace }}
34+
35+
- name: Build
36+
run: yarn build
37+
working-directory: ${{ matrix.workspace }}
2838

2939
- name: Run tests
3040
run: yarn test
31-
working-directory: modules/lambda-at-edge/edge-functions
41+
working-directory: ${{ matrix.workspace }}

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ terraform.rc
4545
**/*.tgz
4646
**/.eslintcache
4747

48+
# Exception: the built uri-rewrite CloudFront Function bundle is committed and
49+
# consumed directly by the edge-functions Terraform module (kept fresh by a
50+
# pre-commit hook).
51+
!lib/cloudfront-functions/packages/uri-rewrite/build/
52+
!lib/cloudfront-functions/packages/uri-rewrite/build/index.js
53+
4854
# ---- OS / editor ----
4955
**/.DS_Store
5056
Thumbs.db

.pre-commit-config.yaml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ repos:
1010
- --hook-config=--add-to-existing-file=true
1111
- --hook-config=--create-file-if-not-exist=true
1212
- id: terraform_docs
13-
name: terraform_docs (lambda-at-edge module)
13+
name: terraform_docs (edge-functions module)
1414
args:
1515
- --hook-config=--path-to-file=README.md
1616
- --hook-config=--add-to-existing-file=true
1717
- --hook-config=--create-file-if-not-exist=true
18-
files: ^modules/lambda-at-edge/
18+
files: ^modules/edge-functions/
1919
- id: terraform_providers_lock
2020
args:
2121
- --args=-platform=linux_amd64
@@ -26,3 +26,16 @@ repos:
2626
rev: v6.0.0
2727
hooks:
2828
- id: check-merge-conflict
29+
30+
- repo: local
31+
hooks:
32+
# The edge-functions Terraform module consumes the committed uri-rewrite
33+
# CloudFront Function bundle directly. Rebuild it from source on any change so
34+
# the committed artifact is never stale.
35+
- id: build-uri-rewrite
36+
name: build uri-rewrite CloudFront Function
37+
language: system
38+
entry: make build-uri-rewrite-committed
39+
pass_filenames: false
40+
require_serial: true
41+
files: ^lib/cloudfront-functions/(packages/uri-rewrite/(src/|esbuild\.js|package\.json|tsconfig\.json)|yarn\.lock|package\.json)

Makefile

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
LAMBDA_DIR := lib/lambda-at-edge
55
CF_DIR := lib/cloudfront-functions
66

7+
LAMBDA_FUNCTIONS := filter-function prerender-proxy geo-redirect response-handler
8+
SEED_VERSION ?= 1.0.0
9+
EDGE_ARTIFACTS_BUCKET ?= $(shell terraform output -raw edge_artifacts_bucket 2>/dev/null)
10+
711
##@ General
812

913
.PHONY: help
@@ -96,6 +100,11 @@ build-response-handler: ## Build response-handler (Lambda@Edge)
96100
build-uri-rewrite: ## Build uri-rewrite (CloudFront Function)
97101
cd $(CF_DIR) && yarn workspace @krishanthisera/uri-rewrite build
98102

103+
.PHONY: build-uri-rewrite-committed
104+
build-uri-rewrite-committed: ## Rebuild + stage the committed uri-rewrite bundle (used by pre-commit)
105+
cd $(CF_DIR) && yarn install --frozen-lockfile --silent && yarn workspace @krishanthisera/uri-rewrite build
106+
git add $(CF_DIR)/packages/uri-rewrite/build/index.js
107+
99108
##@ Lint
100109

101110
.PHONY: lint
@@ -109,12 +118,16 @@ lint-fix: ## Run linters and fix issues (Lambda@Edge)
109118
##@ Format
110119

111120
.PHONY: format
112-
format: ## Check code formatting (Lambda@Edge)
121+
format: ## Check code formatting (Lambda@Edge + Terraform/HCL)
113122
cd $(LAMBDA_DIR) && yarn format:check
123+
@echo "Checking Terraform/HCL formatting..."
124+
terraform fmt -check -recursive
114125

115126
.PHONY: format-fix
116-
format-fix: ## Fix code formatting (Lambda@Edge)
127+
format-fix: ## Fix code formatting (Lambda@Edge + Terraform/HCL)
117128
cd $(LAMBDA_DIR) && yarn format:fix
129+
@echo "Formatting Terraform/HCL..."
130+
terraform fmt -recursive
118131

119132
##@ Release
120133

@@ -152,6 +165,19 @@ tf-docs: ## Regenerate Terraform documentation (requires terraform-docs)
152165
terraform-docs markdown table --output-file README.md --output-mode inject .
153166
terraform-docs markdown table --output-file modules/edge-functions/README.md --output-mode inject modules/edge-functions
154167

168+
.PHONY: mirror-seed
169+
mirror-seed: build-lambda ## One-time: upload locally-built Lambda@Edge bundles to the artifacts bucket (EDGE_ARTIFACTS_BUCKET=... , SEED_VERSION=1.0.0)
170+
@test -n "$(EDGE_ARTIFACTS_BUCKET)" || { echo "EDGE_ARTIFACTS_BUCKET is empty - apply the bucket first or pass it explicitly"; exit 1; }
171+
@for fn in $(LAMBDA_FUNCTIONS); do \
172+
tmp=$$(mktemp -d); \
173+
cp $(LAMBDA_DIR)/packages/$$fn/build/index.js $$tmp/index.js; \
174+
( cd $$tmp && zip -q fn.zip index.js ); \
175+
echo "-> s3://$(EDGE_ARTIFACTS_BUCKET)/lambda-at-edge/$$fn/$(SEED_VERSION).zip"; \
176+
aws s3 cp $$tmp/fn.zip s3://$(EDGE_ARTIFACTS_BUCKET)/lambda-at-edge/$$fn/$(SEED_VERSION).zip; \
177+
rm -rf $$tmp; \
178+
done
179+
@echo "✓ seeded $(LAMBDA_FUNCTIONS) at $(SEED_VERSION)"
180+
155181
##@ Development
156182

157183
.PHONY: dev
@@ -184,6 +210,8 @@ clean: ## Clean build artifacts
184210
cd $(LAMBDA_DIR) && rm -rf packages/*/build function_archives .turbo packages/*/.turbo
185211
@echo "Cleaning CloudFront Functions build artifacts..."
186212
cd $(CF_DIR) && rm -rf packages/*/build .turbo packages/*/.turbo
213+
@echo "Restoring the committed uri-rewrite bundle (consumed by Terraform)..."
214+
cd $(CF_DIR) && yarn workspace @krishanthisera/uri-rewrite build
187215
@echo "✓ Build artifacts cleaned"
188216

189217
.PHONY: clean-all

0 commit comments

Comments
 (0)