Skip to content

Commit f9bfde8

Browse files
committed
fix(aws-lambda): nest the arm64 video function so sam deploy needs no image repository
sam deploy refuses a template with any top-level image function unless it is given an image repository, even when that function's Condition is false, so every deploy of the template failed. The video function now lives in video-function.yaml, a nested stack created only when VideoImageUri is set. The release ships it beside template.yaml and the CLI stages both. A new workflow runs the CLI's sam deploy argv against the template with fake credentials and requires it to reach the first upload.
1 parent f901661 commit f9bfde8

9 files changed

Lines changed: 260 additions & 52 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Lambda SAM template
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- "examples/aws-lambda/**"
7+
- ".github/workflows/lambda-sam-template.yml"
8+
workflow_dispatch:
9+
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.ref }}
12+
cancel-in-progress: true
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
sam-deploy:
19+
name: sam deploy validation
20+
runs-on: ubuntu-24.04
21+
timeout-minutes: 10
22+
steps:
23+
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
24+
- run: pipx install aws-sam-cli==1.169.0
25+
- run: bash examples/aws-lambda/scripts/check-sam-deploy.sh
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
# `sam deploy` validates the template before it calls AWS. With fake credentials it must get past that
3+
# validation and fail at the first upload, with and without a video image (an image function at the
4+
# top level of template.yaml, for one, fails validation for every deploy).
5+
set -uo pipefail
6+
cd "$(dirname "$0")/.."
7+
work=$(mktemp -d)
8+
trap 'rm -rf "$work"' EXIT
9+
mkdir -p "$work/examples/aws-lambda" "$work/packages/aws-lambda/dist"
10+
cp template.yaml video-function.yaml "$work/examples/aws-lambda/"
11+
echo zip > "$work/packages/aws-lambda/dist/handler.zip"
12+
cd "$work/examples/aws-lambda"
13+
sam validate --lint --region us-east-1 -t template.yaml || exit 1
14+
sam validate --lint --region us-east-1 -t video-function.yaml || exit 1
15+
digest=sha256:$(printf '%064d' 0)
16+
for uri in '""' "123456789012.dkr.ecr.us-east-1.amazonaws.com/hf-video@$digest"; do
17+
# The CLI's argv (sam.ts), with --s3-bucket in place of --resolve-s3 so nothing calls AWS before the upload.
18+
out=$(env -u AWS_PROFILE AWS_ACCESS_KEY_ID=AKIAFAKEFAKEFAKEFAKE AWS_SECRET_ACCESS_KEY=fake \
19+
sam deploy --stack-name hf-sam-check --region us-east-1 --s3-bucket hf-sam-check-none \
20+
--capabilities CAPABILITY_IAM --no-confirm-changeset --no-fail-on-empty-changeset \
21+
--parameter-overrides ChromeSource=sparticuz "VideoImageUri=$uri" </dev/null 2>&1)
22+
if ! grep -q "Unable to upload artifact" <<<"$out"; then
23+
echo "sam deploy stopped before reaching AWS (VideoImageUri=$uri):"
24+
echo "$out" | tail -5
25+
exit 1
26+
fi
27+
echo "VideoImageUri=$uri: past validation, stopped at the upload"
28+
done

‎examples/aws-lambda/template.yaml‎

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ Globals:
8585
# terminate at the Step Functions → Lambda boundary instead of
8686
# following into per-function spans.
8787
Tracing: Active
88-
# Cost-allocation tags for every AWS::Serverless::Function (the render
89-
# function and the optional video function). Bucket + state-machine carry
90-
# the same tags resource-locally because Globals only covers functions.
88+
# Cost-allocation tags for the render function. Bucket, state machine and
89+
# video-function.yaml carry the same tags themselves; Globals covers only SAM functions.
9190
Tags:
9291
Project: !Ref ProjectName
9392
HyperFramesComponent: lambda-renderer
@@ -151,7 +150,7 @@ Resources:
151150
HyperFrames distributed render handler. Dispatches on event.Action.
152151
Handler: handler.handler
153152
Runtime: nodejs22.x
154-
# The ZIP's @sparticuz/chromium ships x86_64 Chrome only; arm64 runs as VideoFunction's image.
153+
# The ZIP's @sparticuz/chromium ships x86_64 Chrome only; video-function.yaml runs chunks on arm64.
155154
Architectures: [x86_64]
156155
# Local path is resolved by `sam build` + `sam deploy --resolve-s3`
157156
# (or `--s3-bucket`); the resulting CodeUri rewrites to s3://.
@@ -190,34 +189,25 @@ Resources:
190189
# IAM into adopters' accounts.
191190

192191
# ── Optional arm64 chunk renderer: the same handler as a container image ──
193-
VideoFunction:
194-
Type: AWS::Serverless::Function
192+
# Nested: `sam deploy` demands an image repository for any image function in this template.
193+
VideoFunctionStack:
194+
Type: AWS::CloudFormation::Stack
195195
Condition: HasVideoFunction
196196
Properties:
197-
FunctionName: !Sub "${ProjectName}-video"
198-
Description: >-
199-
HyperFrames distributed render handler on arm64, invoked for renderChunk only.
200-
PackageType: Image
201-
ImageUri: !Ref VideoImageUri
202-
Architectures: [arm64]
203-
ReservedConcurrentExecutions: !If
204-
- HasReservedConcurrency
205-
- !Ref ReservedConcurrency
206-
- !Ref AWS::NoValue
207-
EphemeralStorage:
208-
Size: 10240
209-
Environment:
210-
Variables:
211-
TMPDIR: /tmp
212-
HYPERFRAMES_RENDER_BUCKET: !Ref RenderBucket
213-
HYPERFRAMES_LAMBDA_RESERVED_CONCURRENCY: !Ref ReservedConcurrency
214-
Policies:
215-
- S3CrudPolicy:
216-
BucketName: !Ref RenderBucket
217-
- Statement:
218-
- Effect: Allow
219-
Action: s3:AbortMultipartUpload
220-
Resource: !Sub "arn:${AWS::Partition}:s3:::${RenderBucket}/*"
197+
TemplateURL: video-function.yaml
198+
Parameters:
199+
ProjectName: !Ref ProjectName
200+
ImageUri: !Ref VideoImageUri
201+
RenderBucket: !Ref RenderBucket
202+
MemorySize: !Ref LambdaMemoryMb
203+
Timeout: !Ref LambdaTimeoutSec
204+
ReservedConcurrency: !Ref ReservedConcurrency
205+
ChromeSource: !Ref ChromeSource
206+
Tags:
207+
- Key: Project
208+
Value: !Ref ProjectName
209+
- Key: HyperFramesComponent
210+
Value: lambda-renderer
221211

222212
# ── CloudWatch log group for the state machine ──────────────────────────
223213
# SAM doesn't auto-create one when `LoggingConfiguration` is set, so we
@@ -460,7 +450,11 @@ Resources:
460450
Resource: arn:aws:states:::lambda:invoke
461451
Parameters:
462452
FunctionName:
463-
!If [HasVideoFunction, !GetAtt VideoFunction.Arn, !GetAtt RenderFunction.Arn]
453+
!If [
454+
HasVideoFunction,
455+
!GetAtt VideoFunctionStack.Outputs.FunctionArn,
456+
!GetAtt RenderFunction.Arn,
457+
]
464458
Payload:
465459
Action: renderChunk
466460
PlanProtocol: v1
@@ -581,7 +575,7 @@ Resources:
581575
FunctionName:
582576
!If [
583577
HasVideoFunction,
584-
!GetAtt VideoFunction.Arn,
578+
!GetAtt VideoFunctionStack.Outputs.FunctionArn,
585579
!GetAtt RenderFunction.Arn,
586580
]
587581
Payload:
@@ -768,7 +762,11 @@ Resources:
768762
Action: lambda:InvokeFunction
769763
Resource:
770764
- !GetAtt RenderFunction.Arn
771-
- !If [HasVideoFunction, !GetAtt VideoFunction.Arn, !Ref AWS::NoValue]
765+
- !If [
766+
HasVideoFunction,
767+
!GetAtt VideoFunctionStack.Outputs.FunctionArn,
768+
!Ref AWS::NoValue,
769+
]
772770
# A Distributed Map runs each chunk as a child execution of this state machine.
773771
# ARNs built by name: !Ref RenderStateMachine here would be a dependency cycle.
774772
- PolicyName: RunDistributedMapChildren
@@ -823,7 +821,12 @@ Resources:
823821
MetricName: Invocations
824822
Dimensions:
825823
- Name: FunctionName
826-
Value: !If [HasVideoFunction, !Ref VideoFunction, !Ref RenderFunction]
824+
Value:
825+
!If [
826+
HasVideoFunction,
827+
!GetAtt VideoFunctionStack.Outputs.FunctionName,
828+
!Ref RenderFunction,
829+
]
827830
Statistic: Sum
828831
Period: 3600
829832
EvaluationPeriods: 1
@@ -867,7 +870,7 @@ Resources:
867870
MetricName: Errors
868871
Dimensions:
869872
- Name: FunctionName
870-
Value: !Ref VideoFunction
873+
Value: !GetAtt VideoFunctionStack.Outputs.FunctionName
871874
Statistic: Sum
872875
Period: 300
873876
EvaluationPeriods: 1
@@ -918,7 +921,7 @@ Outputs:
918921
VideoFunctionArn:
919922
Condition: HasVideoFunction
920923
Description: ARN of the arm64 chunk-rendering function.
921-
Value: !GetAtt VideoFunction.Arn
924+
Value: !GetAtt VideoFunctionStack.Outputs.FunctionArn
922925

923926
RenderStateMachineArn:
924927
Description: ARN of the Step Functions state machine. Pass to `aws stepfunctions start-execution`.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
AWSTemplateFormatVersion: "2010-09-09"
2+
Description: >-
3+
HyperFrames arm64 chunk renderer: the render handler as a container image, invoked for renderChunk
4+
only. Nested in template.yaml when VideoImageUri is set; plain CloudFormation, because `sam deploy`
5+
demands an image repository for any top-level image function, even one whose Condition is false.
6+
7+
Parameters:
8+
ProjectName:
9+
Type: String
10+
ImageUri:
11+
Type: String
12+
RenderBucket:
13+
Type: String
14+
MemorySize:
15+
Type: Number
16+
Timeout:
17+
Type: Number
18+
ReservedConcurrency:
19+
Type: Number
20+
ChromeSource:
21+
Type: String
22+
23+
Conditions:
24+
HasReservedConcurrency: !Not [!Equals [!Ref ReservedConcurrency, -1]]
25+
26+
Resources:
27+
Role:
28+
Type: AWS::IAM::Role
29+
Properties:
30+
AssumeRolePolicyDocument:
31+
Version: "2012-10-17"
32+
Statement:
33+
- Effect: Allow
34+
Principal:
35+
Service: lambda.amazonaws.com
36+
Action: sts:AssumeRole
37+
ManagedPolicyArns:
38+
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
39+
- !Sub "arn:${AWS::Partition}:iam::aws:policy/AWSXRayDaemonWriteAccess"
40+
# The render function's S3CrudPolicy plus AbortMultipartUpload, spelled out.
41+
Policies:
42+
- PolicyName: render-bucket
43+
PolicyDocument:
44+
Version: "2012-10-17"
45+
Statement:
46+
- Effect: Allow
47+
Action:
48+
- s3:GetObject
49+
- s3:ListBucket
50+
- s3:GetBucketLocation
51+
- s3:GetObjectVersion
52+
- s3:PutObject
53+
- s3:PutObjectAcl
54+
- s3:GetLifecycleConfiguration
55+
- s3:PutLifecycleConfiguration
56+
- s3:DeleteObject
57+
- s3:AbortMultipartUpload
58+
Resource:
59+
- !Sub "arn:${AWS::Partition}:s3:::${RenderBucket}"
60+
- !Sub "arn:${AWS::Partition}:s3:::${RenderBucket}/*"
61+
Tags:
62+
- Key: Project
63+
Value: !Ref ProjectName
64+
- Key: HyperFramesComponent
65+
Value: lambda-renderer
66+
67+
Function:
68+
Type: AWS::Lambda::Function
69+
Properties:
70+
FunctionName: !Sub "${ProjectName}-video"
71+
Description: HyperFrames distributed render handler on arm64, invoked for renderChunk only.
72+
PackageType: Image
73+
Code:
74+
ImageUri: !Ref ImageUri
75+
Architectures: [arm64]
76+
Role: !GetAtt Role.Arn
77+
MemorySize: !Ref MemorySize
78+
Timeout: !Ref Timeout
79+
ReservedConcurrentExecutions: !If
80+
- HasReservedConcurrency
81+
- !Ref ReservedConcurrency
82+
- !Ref AWS::NoValue
83+
EphemeralStorage:
84+
Size: 10240
85+
TracingConfig:
86+
Mode: Active
87+
Environment:
88+
Variables:
89+
NODE_OPTIONS: "--enable-source-maps"
90+
HYPERFRAMES_LAMBDA_CHROME_SOURCE: !Ref ChromeSource
91+
TMPDIR: /tmp
92+
HYPERFRAMES_RENDER_BUCKET: !Ref RenderBucket
93+
HYPERFRAMES_LAMBDA_RESERVED_CONCURRENCY: !Ref ReservedConcurrency
94+
Tags:
95+
- Key: Project
96+
Value: !Ref ProjectName
97+
- Key: HyperFramesComponent
98+
Value: lambda-renderer
99+
100+
Outputs:
101+
FunctionArn:
102+
Value: !GetAtt Function.Arn
103+
FunctionName:
104+
Value: !Ref Function

‎packages/aws-lambda/scripts/pin-handler-zip.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import { join } from "node:path";
66
import { adapterPackageRoot, prebuiltHandler } from "../src/sdk/prebuiltHandler.js";
77
import { pinHandlerZip } from "./pin-handler-zip.js";
88

9-
const realTemplate = readFileSync(
10-
join(import.meta.dir, "..", "..", "..", "examples", "aws-lambda", "template.yaml"),
11-
"utf-8",
12-
);
9+
const examples = join(import.meta.dir, "..", "..", "..", "examples", "aws-lambda");
10+
const realTemplate = readFileSync(join(examples, "template.yaml"), "utf-8");
11+
const videoTemplate = readFileSync(join(examples, "video-function.yaml"), "utf-8");
1312
const dirs: string[] = [];
1413

1514
function fixtureRepo(): { root: string; pkgDir: string } {
@@ -27,6 +26,7 @@ function fixtureRepo(): { root: string; pkgDir: string } {
2726
);
2827
mkdirSync(join(root, "examples", "aws-lambda"), { recursive: true });
2928
writeFileSync(join(root, "examples", "aws-lambda", "template.yaml"), realTemplate);
29+
writeFileSync(join(root, "examples", "aws-lambda", "video-function.yaml"), videoTemplate);
3030
return { root, pkgDir };
3131
}
3232

@@ -66,6 +66,10 @@ describe("pinHandlerZip", () => {
6666
const template = readFileSync(pinned!.templatePath, "utf-8");
6767
expect(template).toContain(" CodeUri: handler.zip\n");
6868
expect(template).not.toContain("packages/aws-lambda/dist/handler.zip\n");
69+
expect(template).toContain(" TemplateURL: video-function.yaml\n");
70+
expect(readFileSync(join(pkgDir, "prebuilt", "video-function.yaml"), "utf-8")).toBe(
71+
videoTemplate,
72+
);
6973
});
7074

7175
it("pins a zip a previous run already attached to the release", () => {
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
1+
import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
22
import { join } from "node:path";
33

44
const CHECKOUT_CODE_URI = "CodeUri: ../../packages/aws-lambda/dist/handler.zip";
55

6-
/** Writes `<outDir>/template.yaml`: the example SAM template, pointed at a `handler.zip` beside it. */
6+
/**
7+
* Writes `<outDir>/template.yaml`, the example SAM template pointed at a `handler.zip` beside it, and
8+
* the `video-function.yaml` it nests.
9+
*/
710
export function writePrebuiltTemplate(repoRoot, outDir) {
8-
const template = readFileSync(join(repoRoot, "examples", "aws-lambda", "template.yaml"), "utf-8");
11+
const examples = join(repoRoot, "examples", "aws-lambda");
12+
const template = readFileSync(join(examples, "template.yaml"), "utf-8");
913
if (template.split(CHECKOUT_CODE_URI).length !== 2) {
1014
throw new Error(
1115
`[prebuilt-template] expected exactly one "${CHECKOUT_CODE_URI}" in the template`,
@@ -16,4 +20,5 @@ export function writePrebuiltTemplate(repoRoot, outDir) {
1620
join(outDir, "template.yaml"),
1721
template.replace(CHECKOUT_CODE_URI, "CodeUri: handler.zip"),
1822
);
23+
copyFileSync(join(examples, "video-function.yaml"), join(outDir, "video-function.yaml"));
1924
}

0 commit comments

Comments
 (0)