Files
floci/compatibility-tests/sdk-test-awscli/test/lambda-web.bats
T
TATSUNO “Taz” Yasuhiro 6e01216f88 feat(lambdaweb): serve endpoint domains from a Node.js environment per revision (#5760)
* feat(lambdaweb): serve endpoint domains from a Node.js environment per revision

A request whose Host is an endpoint's domainName goes over HTTP/1.1 to
a container that Floci starts from the revision's S3 code on the first
request: public.ecr.aws/lambda/nodejs:24 with the code in /var/task,
index.js or AWS_LAMBDA_NODEJS_ENTRYPOINT as the entry file, the node
flags and the environment variables AWS sets, ten seconds to listen,
SIGTERM and one second when idle, and a fresh environment after the
process exits between requests.

The forwarded request carries the headers AWS adds and loses the ones
AWS drops. The response carries lambda-web-request-id and
lambda-web-logstream and streams as the app writes it. Two revisions in
revisionWeights split the traffic by weight. The platform pages for an
unsupported method, an unknown domain, HTTP/1.0, a server that does not
listen, a timeout, a crash and a disabled endpoint are the AWS pages.

Part of #5699

* fix(lambdaweb): index endpoint domains and claim only the minted host shapes

A request looked its domain up by scanning every stored function across
accounts and iterating live endpoint maps outside the function lock. The
service now keeps a domain index, filled from storage at start and kept
by the create and delete paths, and copies the function under its lock.
The data plane claims only hosts shaped like the domains Floci mints, in
any case and with any port, so a dotted S3 bucket with a lambda-web
label still reaches S3.

* fix(lambdaweb): reserve environments against the idle reaper and track exits

acquire now reserves the environment under the same lock the idle
reaper takes, so a request cannot start on a container the reaper is
about to stop. Docker faults in the reaper and in discard are contained
so one error does not end the periodic run or skip the removal. The exit
watcher records the exit time and arms itself again when its stream
drops, and a request that found a dead process is replayed only when
that recorded exit came first, never because the container is gone. The
unpacked code directory is removed on every path. The container gets the
execution role's session credentials when the role exists, and the
function account's credentials otherwise, as the Lambda launcher does.

* fix(lambdaweb): bound the whole exchange by timeoutSeconds and forward chunked bodies

timeoutSeconds is a deadline on the whole request, as recorded against
AWS: a response that has not started by then is the 504 page, one that
is still streaming is cut, and a slow upload gets its 504 once the body
is in, since the edge takes the body before it answers. A body without a
Content-Length is sent chunked instead of carrying the client's
Transfer-Encoding header. A failure of the upload stream ends the
exchange and releases the environment once.

* test(compat): probe the unknown Lambda Web domain in the shape Floci mints

The data plane claims only hosts shaped like the domains it mints, so
the unknown-domain check needs a 16 character id to reach it.
2026-10-15 22:50:05 -05:00

167 lines
7.4 KiB
Bash

#!/usr/bin/env bats
# Lambda Web Functions integration tests
#
# The `lambda-web` service model signs as lambda and is served under the lambda
# service. Code comes from an S3 object, so each test seeds one first.
setup() {
load 'test_helper/common-setup'
FUNCTION_NAME=""
BUCKET="cli-lambda-web-code-$$"
ROLE_ARN="arn:aws:iam::000000000000:role/lambda-web"
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null 2>&1 || true
printf 'zip' > "$BATS_TEST_TMPDIR/app.zip"
aws_cmd s3api put-object --bucket "$BUCKET" --key app.zip --body "$BATS_TEST_TMPDIR/app.zip" >/dev/null
}
teardown() {
if [ -n "$FUNCTION_NAME" ]; then
aws_cmd lambda-web delete-web-function --function-name "$FUNCTION_NAME" >/dev/null 2>&1 || true
aws_cmd iam delete-role-policy --role-name "awscli-lambdaweb-$FUNCTION_NAME" --policy-name awscli-lambdaweb-logs >/dev/null 2>&1 || true
aws_cmd iam delete-role --role-name "awscli-lambdaweb-$FUNCTION_NAME" >/dev/null 2>&1 || true
fi
aws_cmd s3 rb "s3://$BUCKET" --force >/dev/null 2>&1 || true
}
build_config() {
printf '{"codeConfig":{"s3Object":{"bucket":"%s","key":"app.zip"}},"runtimeConfig":{"runtime":"nodejs24.x"}}' "$BUCKET"
}
create_function() {
local name="$1"
aws_cmd lambda-web create-web-function \
--function-name "$name" \
--revision-config "{\"buildConfig\":$(build_config),\"serviceConfig\":{\"executionRoleArn\":\"$ROLE_ARN\"}}" \
--endpoint-config '{"endpointName":"dev","endpointType":"HomeRegion","authType":"ApplicationManaged","autoDeploymentMode":"LatestRevision"}'
}
@test "Lambda Web: create a function with its first revision and endpoint" {
FUNCTION_NAME=$(unique_name "cli-web")
run create_function "$FUNCTION_NAME"
assert_success
state=$(json_get "$output" '.state')
revision_state=$(json_get "$output" '.revision.state')
endpoint_state=$(json_get "$output" '.endpoint.state')
domain=$(json_get "$output" '.endpoint.domainName')
[ "$state" = "Active" ]
[ "$revision_state" = "Pending" ]
[ "$endpoint_state" = "Pending" ]
[[ "$domain" =~ \.lambda-web\.us-east-1\. ]]
run aws_cmd lambda-web wait web-function-endpoint-active --function-name "$FUNCTION_NAME" --endpoint-name dev
assert_success
}
@test "Lambda Web: a new revision moves a LatestRevision endpoint" {
FUNCTION_NAME=$(unique_name "cli-web")
create_function "$FUNCTION_NAME" >/dev/null
run aws_cmd lambda-web create-web-function-revision \
--function-name "$FUNCTION_NAME" \
--build-config "$(build_config)" \
--service-config "{\"executionRoleArn\":\"$ROLE_ARN\",\"timeoutSeconds\":60}"
assert_success
revision_id=$(json_get "$output" '.revisionId')
[[ "$revision_id" =~ ^rev-[a-z0-9]{5}$ ]]
run aws_cmd lambda-web wait web-function-revision-active --function-name "$FUNCTION_NAME" --revision-id "$revision_id"
assert_success
run aws_cmd lambda-web get-web-function-endpoint --function-name "$FUNCTION_NAME" --endpoint-name dev
assert_success
routed=$(json_get "$output" '.revisionWeights[0].revisionId')
[ "$routed" = "$revision_id" ]
}
@test "Lambda Web: weighted routing and the in-use revision guard" {
FUNCTION_NAME=$(unique_name "cli-web")
create_function "$FUNCTION_NAME" >/dev/null
first=$(aws_cmd lambda-web list-web-function-revisions --function-name "$FUNCTION_NAME" | jq -r '.revisions[0].revisionId')
run aws_cmd lambda-web update-web-function-endpoint \
--function-name "$FUNCTION_NAME" --endpoint-name dev \
--auto-deployment-mode Disabled \
--revision-weights "[{\"revisionId\":\"$first\",\"weight\":100}]"
assert_success
status=$(json_get "$output" '.updateStatus')
[ "$status" = "InProgress" ]
run aws_cmd lambda-web wait web-function-endpoint-updated --function-name "$FUNCTION_NAME" --endpoint-name dev
assert_success
run aws_cmd lambda-web delete-web-function-revision --function-name "$FUNCTION_NAME" --revision-id "$first"
assert_failure
assert_output --partial "ConflictException"
assert_output --partial "the endpoints [dev] are using it"
}
@test "Lambda Web: tags and the resource policy" {
FUNCTION_NAME=$(unique_name "cli-web")
create_function "$FUNCTION_NAME" >/dev/null
arn="arn:aws:lambda:us-east-1:000000000000:web-function/$FUNCTION_NAME"
run aws_cmd lambda-web tag-resource --resource "$arn" --tags env=test
assert_success
run aws_cmd lambda-web list-tags --resource "$arn"
assert_success
env_tag=$(json_get "$output" '.tags.env')
[ "$env_tag" = "test" ]
run aws_cmd lambda-web put-resource-policy --resource-arn "$arn" \
--policy "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::000000000000:root\"},\"Action\":\"lambda:InvokeWebFunctionEndpoint\",\"Resource\":\"$arn/endpoint/*\"}]}"
assert_success
revision_id=$(json_get "$output" '.revisionId')
[ -n "$revision_id" ]
run aws_cmd lambda-web delete-resource-policy --resource-arn "$arn" --revision-id "$revision_id"
assert_success
run aws_cmd lambda-web get-resource-policy --resource-arn "$arn"
assert_failure
assert_output --partial "does not have a policy"
}
@test "Lambda Web: invalid names and missing functions" {
run aws_cmd lambda-web get-web-function --function-name cli-no-such-web-function
assert_failure
assert_output --partial "ResourceNotFoundException"
# The = form keeps the CLI from reading a name that starts with a dash as an option.
run aws_cmd lambda-web create-web-function --function-name=-bad \
--revision-config "{\"buildConfig\":$(build_config),\"serviceConfig\":{\"executionRoleArn\":\"$ROLE_ARN\"}}" \
--endpoint-config '{"endpointName":"dev","endpointType":"HomeRegion","authType":"ApplicationManaged"}'
assert_failure
assert_output --partial "Invalid function name or ARN '-bad'"
}
@test "Lambda Web: deploy the hello world app and invoke its endpoint" {
FUNCTION_NAME=$(unique_name "cli-web")
# deploy passes --endpoint-url only to its Lambda Web client, so the environment variable routes the rest.
run bash -c "cd '$BATS_TEST_TMPDIR' && AWS_ENDPOINT_URL='$FLOCI_ENDPOINT' aws lambda-web deploy --name '$FUNCTION_NAME' --hello-world --create"
assert_success
domain=$(aws_cmd lambda-web get-web-function-endpoint --function-name "$FUNCTION_NAME" --endpoint-name dev | jq -r '.domainName')
run curl -sS -i -H "Host: $domain" "$FLOCI_ENDPOINT/hello?from=bats"
assert_success
assert_output --partial "HTTP/1.1 200"
assert_output --partial "lambda-web-request-id: "
assert_output --partial "lambda-web-logstream: "
assert_output --partial "Hello from Lambda Web!"
# A domain Floci never minted, in the shape it mints.
run curl -sS -i -H "Host: nopenopenopenope.001.lambda-web.us-east-1.localhost:4566" "$FLOCI_ENDPOINT/"
assert_success
assert_output --partial "HTTP/1.1 404"
assert_output --partial "lambda-web-error-type: Platform.InvalidEndpoint"
}
@test "Lambda Web: delete removes the function and the waiter settles" {
FUNCTION_NAME=$(unique_name "cli-web")
create_function "$FUNCTION_NAME" >/dev/null
run aws_cmd lambda-web delete-web-function --function-name "$FUNCTION_NAME"
assert_success
run aws_cmd lambda-web wait web-function-deleted --function-name "$FUNCTION_NAME"
assert_success
FUNCTION_NAME=""
}