Skip to content

Commit e69a0a5

Browse files
authored
Use ARM64 AWS Lambdas (#201)
Also build the lambda in an ARM64 Github runner
1 parent 307d047 commit e69a0a5

File tree

11 files changed

+40
-43
lines changed

11 files changed

+40
-43
lines changed

.github/workflows/deploy-dev.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ jobs:
2626
uses: actions/cache@v4
2727
with:
2828
path: node_modules
29-
key: yarn-modules-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}-dev
29+
key: yarn-modules-${{ runner.arch }}-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}-dev
3030
restore-keys: |
31-
yarn-modules-${{ runner.os }}-
31+
yarn-modules-${{ runner.arch }}-${{ runner.os }}-
3232
3333
- name: Run unit testing
3434
run: make test_unit
3535

3636
build:
37-
runs-on: ubuntu-latest
37+
runs-on: ubuntu-24.04-arm
3838
name: Build Application
3939
steps:
4040
- uses: actions/checkout@v4
@@ -51,9 +51,9 @@ jobs:
5151
uses: actions/cache@v4
5252
with:
5353
path: node_modules
54-
key: yarn-modules-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}-dev
54+
key: yarn-modules-${{ runner.arch }}-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}-dev
5555
restore-keys: |
56-
yarn-modules-${{ runner.os }}-
56+
yarn-modules-${{ runner.arch }}-${{ runner.os }}-
5757
5858
- name: Run build
5959
run: make build
@@ -99,9 +99,9 @@ jobs:
9999
uses: actions/cache@v4
100100
with:
101101
path: node_modules
102-
key: yarn-modules-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}-dev
102+
key: yarn-modules-${{ runner.arch }}-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}-dev
103103
restore-keys: |
104-
yarn-modules-${{ runner.os }}-
104+
yarn-modules-${{ runner.arch }}-${{ runner.os }}-
105105
106106
- name: Download Build files
107107
uses: actions/download-artifact@v4

.github/workflows/deploy-prod.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
name: Deploy all resources to DEV/PROD
2-
run-name: DEV/PROD deploy - @${{ github.actor }}
1+
name: Deploy all resources to PROD
2+
run-name: PROD deploy - @${{ github.actor }}
33

44
on:
55
workflow_dispatch:
@@ -25,15 +25,15 @@ jobs:
2525
uses: actions/cache@v4
2626
with:
2727
path: node_modules
28-
key: yarn-modules-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}-dev
28+
key: yarn-modules-${{ runner.arch }}-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}-dev
2929
restore-keys: |
30-
yarn-modules-${{ runner.os }}-
30+
yarn-modules-${{ runner.arch }}-${{ runner.os }}-
3131
3232
- name: Run unit testing
3333
run: make test_unit
3434

3535
build:
36-
runs-on: ubuntu-latest
36+
runs-on: ubuntu-24.04-arm
3737
name: Build Application
3838
steps:
3939
- uses: actions/checkout@v4
@@ -50,9 +50,9 @@ jobs:
5050
uses: actions/cache@v4
5151
with:
5252
path: node_modules
53-
key: yarn-modules-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}-prod
53+
key: yarn-modules-${{ runner.arch }}-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}-prod
5454
restore-keys: |
55-
yarn-modules-${{ runner.os }}-
55+
yarn-modules-${{ runner.arch }}-${{ runner.os }}-
5656
5757
- name: Run build
5858
run: make build

Makefile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,19 @@ build: src/ cloudformation/
6161
VITE_BUILD_HASH=$(GIT_HASH) yarn build
6262
cp -r src/api/resources/ dist/api/resources
6363
rm -rf dist/lambda/sqs
64-
sam build --template-file cloudformation/main.yml --use-container
64+
sam build --template-file cloudformation/main.yml --use-container --parallel
6565
mkdir -p .aws-sam/build/AppApiLambdaFunction/node_modules/aws-crt/
6666
cp -r node_modules/aws-crt/dist .aws-sam/build/AppApiLambdaFunction/node_modules/aws-crt
67+
## IF WE EVER CHANGE THE LAMBDA ARCH, BE SURE TO CHANGE THESE ##
68+
rm -rf .aws-sam/build/AppApiLambdaFunction/node_modules/aws-crt/dist/bin/darwin*
69+
rm -rf .aws-sam/build/AppApiLambdaFunction/node_modules/aws-crt/dist/bin/linux-x64*
70+
rm -rf .aws-sam/build/AppApiLambdaFunction/node_modules/aws-crt/dist/bin/linux-arm64-musl
71+
rm -rf .aws-sam/build/AppApiLambdaFunction/node_modules/argon2/prebuilds/darwin*
72+
rm -rf .aws-sam/build/AppApiLambdaFunction/node_modules/argon2/prebuilds/freebsd*
73+
rm -rf .aws-sam/build/AppApiLambdaFunction/node_modules/argon2/prebuilds/linux-arm
74+
rm -rf .aws-sam/build/AppApiLambdaFunction/node_modules/argon2/prebuilds/linux-x64*
75+
rm -rf .aws-sam/build/AppApiLambdaFunction/node_modules/argon2/prebuilds/win32-x64*
76+
rm -rf .aws-sam/build/AppApiLambdaFunction/node_modules/argon2/prebuilds/linux-arm64/argon2.armv8.musl.node
6777

6878
local:
6979
VITE_BUILD_HASH=$(GIT_HASH) yarn run dev

cloudformation/main.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,23 +250,24 @@ Resources:
250250
DependsOn:
251251
- AppLogGroups
252252
Properties:
253-
Architectures: [x86_64]
253+
Architectures: [arm64]
254254
CodeUri: ../dist/lambda
255255
AutoPublishAlias: live
256256
Runtime: nodejs22.x
257257
Description: !Sub "${ApplicationFriendlyName} API Lambda"
258258
FunctionName: !Sub ${ApplicationPrefix}-lambda
259259
Handler: lambda.handler
260-
MemorySize: 768
260+
MemorySize: 2048
261261
Role: !GetAtt AppSecurityRoles.Outputs.MainFunctionRoleArn
262262
Timeout: 60
263263
Environment:
264264
Variables:
265265
RunEnvironment: !Ref RunEnvironment
266266
EntraRoleArn: !GetAtt AppSecurityRoles.Outputs.EntraFunctionRoleArn
267267
LinkryKvArn: !GetAtt LinkryRecordsCloudfrontStore.Arn
268-
AWS_CRT_NODEJS_BINARY_RELATIVE_PATH: node_modules/aws-crt/dist/bin/linux-x64-glibc/aws-crt-nodejs.node
268+
AWS_CRT_NODEJS_BINARY_RELATIVE_PATH: node_modules/aws-crt/dist/bin/linux-arm64-glibc/aws-crt-nodejs.node
269269
ORIGIN_VERIFY_KEY: !Join ['-', ['secret', !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref AWS::StackId]]]]]]
270+
NODE_OPTIONS: --enable-source-maps
270271
VpcConfig:
271272
Ipv6AllowedForDualStack: !If [ShouldAttachVpc, True, !Ref AWS::NoValue]
272273
SecurityGroupIds:
@@ -292,14 +293,14 @@ Resources:
292293
DependsOn:
293294
- AppLogGroups
294295
Properties:
295-
Architectures: [x86_64]
296+
Architectures: [arm64]
296297
CodeUri: ../dist/sqsConsumer
297298
AutoPublishAlias: live
298299
Runtime: nodejs22.x
299300
Description: !Sub "${ApplicationFriendlyName} SQS Lambda"
300301
FunctionName: !Sub ${ApplicationPrefix}-sqs-lambda
301302
Handler: index.handler
302-
MemorySize: 512
303+
MemorySize: 2048
303304
Role:
304305
Fn::GetAtt: AppSecurityRoles.Outputs.SqsFunctionRoleArn
305306
Timeout: !Ref SqsLambdaTimeout
@@ -309,6 +310,7 @@ Resources:
309310
Variables:
310311
RunEnvironment: !Ref RunEnvironment
311312
EntraRoleArn: !GetAtt AppSecurityRoles.Outputs.EntraFunctionRoleArn
313+
NODE_OPTIONS: --enable-source-maps
312314
VpcConfig:
313315
Ipv6AllowedForDualStack: !If [ShouldAttachVpc, True, !Ref AWS::NoValue]
314316
SecurityGroupIds:

src/api/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ const commonParams = {
1414
".json": "file",
1515
}, // File loaders
1616
target: "es2022", // Target ES2022
17-
sourcemap: false,
17+
sourcemap: true,
1818
platform: "node",
19-
external: ["aws-sdk", ...packagesToTransfer],
19+
external: ["@aws-sdk/*", ...packagesToTransfer],
2020
alias: {
2121
"moment-timezone": resolve(
2222
process.cwd(),

src/api/createLambdaPackage.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ function getPath() {
99
return { pathname, dirname, basename };
1010
}
1111
// These are packages not bundled into the JS file by esbuild
12+
// These packages have native deps that break when bundled
1213
export const packagesToTransfer = [
1314
"moment-timezone",
1415
"passkit-generator",
15-
"fastify",
16-
"zod",
1716
"argon2",
1817
"ioredis",
1918
];

src/api/esbuild.config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ const buildOptions = {
3030
},
3131
banner: {
3232
js: `
33-
import { fileURLToPath } from 'url';
3433
import { createRequire as topLevelCreateRequire } from 'module';
3534
const require = topLevelCreateRequire(import.meta.url);
36-
const __filename = fileURLToPath(import.meta.url);
37-
const __dirname = path.dirname(__filename);
3835
`.trim(),
3936
}, // Banner for compatibility with CommonJS
4037
plugins: [

src/api/functions/apiKey.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createHash, randomBytes } from "crypto";
2-
import * as argon2 from "argon2";
2+
import { hash, verify } from "argon2";
33
import { UnauthenticatedError } from "common/errors/index.js";
44
import NodeCache from "node-cache";
55
import {
@@ -33,7 +33,7 @@ export const createApiKey = async () => {
3333
const rawKey = randomBytes(32).toString("hex");
3434
const checksum = createChecksum(rawKey);
3535
const apiKey = `${prefix}_${rawKey}_${checksum}`;
36-
const hashedKey = await argon2.hash(rawKey);
36+
const hashedKey = await hash(rawKey);
3737
return { apiKey, hashedKey, keyId };
3838
};
3939

@@ -75,7 +75,7 @@ export const verifyApiKey = async ({
7575
if (!isChecksumValid) {
7676
return false;
7777
}
78-
return await argon2.verify(hashedKey, rawKey);
78+
return await verify(hashedKey, rawKey);
7979
} catch (e) {
8080
if (e instanceof UnauthenticatedError) {
8181
return false;

src/api/lambda.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import awsLambdaFastify, { LambdaResponse } from "@fastify/aws-lambda";
1+
import awsLambdaFastify from "@fastify/aws-lambda";
22
import init from "./index.js";
3-
import warmer from "lambda-warmer";
43
import { type APIGatewayEvent, type Context } from "aws-lambda";
54
import { InternalServerError, ValidationError } from "common/errors/index.js";
65

@@ -11,10 +10,6 @@ const realHandler = awsLambdaFastify(app, {
1110
callbackWaitsForEmptyEventLoop: false,
1211
});
1312
const handler = async (event: APIGatewayEvent, context: Context) => {
14-
// if a warming event
15-
if (await warmer(event, { correlationId: context.awsRequestId }, context)) {
16-
return "warmed";
17-
}
1813
if (process.env.ORIGIN_VERIFY_KEY) {
1914
// check that the request has the right header (coming from cloudfront)
2015
if (

src/api/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"ioredis": "^5.6.1",
4949
"jsonwebtoken": "^9.0.2",
5050
"jwks-rsa": "^3.2.0",
51-
"lambda-warmer": "^2.3.0",
5251
"moment": "^2.30.1",
5352
"moment-timezone": "^0.6.0",
5453
"node-cache": "^5.1.2",
@@ -71,4 +70,4 @@
7170
"nodemon": "^3.1.10",
7271
"pino-pretty": "^13.0.0"
7372
}
74-
}
73+
}

0 commit comments

Comments
 (0)