feat(cloudformation): provision AWS::Lambda::Url (#3423)

A function URL in a template was falling to the stub arm, so a stack exported the literal
string "LogicalId.FunctionUrl" in place of a URL and nothing was listening on it.

The physical id is the FunctionArn the URL is attached to, the alias ARN when Qualifier names
one, which is the registry schema's primary identifier and enough to address the config again
since a qualified ARN parses back into function and qualifier. TargetFunctionArn and Qualifier
are create-only, so an update that moves the URL creates against the new function and removes
the one it displaced.
This commit is contained in:
Anders Wasen authored and GitHub committed 2026-09-13 18:45:25 -05:00
1 parent e8edc3d4b1
commit 10becc593f
4 files changed
+312 -9

No files matched your search

@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.lambda.model.LambdaUrlConfig;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
@@ -16,6 +17,8 @@ import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
@@ -30,7 +33,8 @@ import static org.mockito.Mockito.when;
/**
* The permission-replacement path: a stack update removes the previous statement before adding
* its replacement, so a rejected replacement must not leave the function with neither.
* its replacement, so a rejected replacement must not leave the function with neither. Plus
* {@code AWS::Lambda::Url}, whose {@code FunctionUrl} attribute is the one a template exports.
*/
class LambdaAddressingCfnProvisionerTest {
@@ -117,4 +121,175 @@ class LambdaAddressingCfnProvisionerTest {
.put("Action", "lambda:InvokeFunction")
.put("Principal", "s3.amazonaws.com");
}
// ── AWS::Lambda::Url ─────────────────────────────────────────────────────
private static final String FN_ARN = "arn:aws:lambda:us-east-1:000000000000:function:app-fn";
private StackResource urlResource() {
StackResource r = new StackResource();
r.setLogicalId("AppFnLambdaFunctionUrl");
r.setResourceType("AWS::Lambda::Url");
r.setAttributes(new HashMap<>());
return r;
}
private LambdaUrlConfig urlConfig(String functionArn) {
LambdaUrlConfig config = new LambdaUrlConfig();
config.setFunctionArn(functionArn);
config.setFunctionUrl("http://abc123.lambda-url.us-east-1.localhost:4566/");
return config;
}
private void noExistingUrlConfig() {
doThrow(new AwsException("ResourceNotFoundException", "Function URL config not found", 404))
.when(lambda).getFunctionUrlConfig(anyString(), anyString(), any());
}
@Test
void urlPublishesTheFunctionUrlAttribute() {
noExistingUrlConfig();
when(lambda.createFunctionUrlConfig(eq(REGION), eq(FN_ARN), isNull(), anyMap()))
.thenReturn(urlConfig(FN_ARN));
StackResource r = urlResource();
provisioner.provision(r, mapper.createObjectNode()
.put("TargetFunctionArn", FN_ARN)
.put("AuthType", "NONE"), ctx());
// Without the attribute, Fn::GetAtt resolves to the literal "LogicalId.FunctionUrl" and
// that string is what a stack export hands out in place of a URL.
assertEquals("http://abc123.lambda-url.us-east-1.localhost:4566/",
r.getAttributes().get("FunctionUrl"));
assertEquals(FN_ARN, r.getAttributes().get("FunctionArn"));
assertEquals(FN_ARN, r.getPhysicalId());
}
@Test
void urlUpdatesTheConfigTheTargetAlreadyHas() {
when(lambda.getFunctionUrlConfig(REGION, FN_ARN, null)).thenReturn(urlConfig(FN_ARN));
when(lambda.updateFunctionUrlConfig(eq(REGION), eq(FN_ARN), isNull(), anyMap()))
.thenReturn(urlConfig(FN_ARN));
StackResource r = urlResource();
r.setPhysicalId(FN_ARN);
provisioner.provision(r, mapper.createObjectNode()
.put("TargetFunctionArn", FN_ARN)
.put("AuthType", "AWS_IAM"), ctx());
// CreateFunctionUrlConfig answers 409 for a function that already has one, so a re-deploy
// has to update rather than create.
verify(lambda).updateFunctionUrlConfig(eq(REGION), eq(FN_ARN), isNull(), anyMap());
verify(lambda, never()).createFunctionUrlConfig(anyString(), anyString(), any(), anyMap());
}
@Test
void urlMovedToAnotherFunctionDeletesTheDisplacedOne() {
String newArn = "arn:aws:lambda:us-east-1:000000000000:function:other-fn";
noExistingUrlConfig();
when(lambda.createFunctionUrlConfig(eq(REGION), eq(newArn), isNull(), anyMap()))
.thenReturn(urlConfig(newArn));
StackResource r = urlResource();
r.setPhysicalId(FN_ARN);
ProvisionContext update = new ProvisionContext(ctx().engine(), REGION, "000000000000",
"test-stack", FN_ARN);
provisioner.provision(r, mapper.createObjectNode()
.put("TargetFunctionArn", newArn)
.put("AuthType", "NONE"), update);
// TargetFunctionArn is create-only. The URL on the previous function is not addressable
// from any resource any more, so leaving it would keep it invokable forever.
verify(lambda).deleteFunctionUrlConfig(REGION, FN_ARN, null);
assertEquals(newArn, r.getPhysicalId());
}
@Test
void urlCorsArrivesTypedNotStringified() {
noExistingUrlConfig();
when(lambda.createFunctionUrlConfig(eq(REGION), eq(FN_ARN), isNull(), anyMap()))
.thenReturn(urlConfig(FN_ARN));
CloudFormationTemplateEngine engine = ctx().engine();
when(engine.resolveStringList(any())).thenAnswer(inv -> {
JsonNode node = inv.getArgument(0);
List<String> values = new java.util.ArrayList<>();
if (node != null && node.isArray()) {
node.forEach(element -> values.add(element.asText()));
}
return values;
});
ProvisionContext ctx = new ProvisionContext(engine, REGION, "000000000000", "test-stack");
ObjectNode props = mapper.createObjectNode()
.put("TargetFunctionArn", FN_ARN)
.put("AuthType", "NONE");
ObjectNode cors = props.putObject("Cors");
cors.put("AllowCredentials", true).put("MaxAge", 600);
cors.putArray("AllowOrigins").add("https://app.example.com");
cors.putArray("AllowMethods").add("GET").add("POST");
provisioner.provision(urlResource(), props, ctx);
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.captor();
verify(lambda).createFunctionUrlConfig(eq(REGION), eq(FN_ARN), isNull(), captor.capture());
Map<String, Object> cfg = (Map<String, Object>) captor.getValue().get("Cors");
// The service reads these with Boolean.TRUE.equals and an int coercion: the resolved
// strings a scalar resolve hands back would read as false and 0.
assertEquals(Boolean.TRUE, cfg.get("AllowCredentials"));
assertEquals(600, cfg.get("MaxAge"));
assertEquals(List.of("https://app.example.com"), cfg.get("AllowOrigins"));
assertEquals(List.of("GET", "POST"), cfg.get("AllowMethods"));
}
@Test
void anUpdateThatDroppedCorsClearsIt() {
when(lambda.getFunctionUrlConfig(REGION, FN_ARN, null)).thenReturn(urlConfig(FN_ARN));
when(lambda.updateFunctionUrlConfig(eq(REGION), eq(FN_ARN), isNull(), anyMap()))
.thenReturn(urlConfig(FN_ARN));
StackResource r = urlResource();
r.setPhysicalId(FN_ARN);
provisioner.provision(r, mapper.createObjectNode()
.put("TargetFunctionArn", FN_ARN)
.put("AuthType", "NONE"), ctx());
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.captor();
verify(lambda).updateFunctionUrlConfig(eq(REGION), eq(FN_ARN), isNull(), captor.capture());
// UpdateFunctionUrlConfig clears the policy only for a Cors member that is present and
// null; omitting it left the rules the template no longer declares in force.
assertTrue(captor.getValue().containsKey("Cors"));
assertNull(captor.getValue().get("Cors"));
}
@Test
void urlWithoutATargetIsRefused() {
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(urlResource(),
mapper.createObjectNode().put("AuthType", "NONE"), ctx()));
assertTrue(e.getMessage().contains("TargetFunctionArn"), e.getMessage());
verify(lambda, never()).createFunctionUrlConfig(anyString(), anyString(), any(), anyMap());
}
@Test
void urlDeleteRemovesTheConfigTheArnPointsAt() {
provisioner.delete("AWS::Lambda::Url", FN_ARN, REGION);
// A qualified ARN parses back into function and qualifier, so the physical id is enough.
verify(lambda).deleteFunctionUrlConfig(REGION, FN_ARN, null);
}
@Test
void urlDeleteToleratesAnAlreadyGoneConfig() {
doThrow(new AwsException("ResourceNotFoundException", "not found", 404))
.when(lambda).deleteFunctionUrlConfig(REGION, FN_ARN, null);
provisioner.delete("AWS::Lambda::Url", FN_ARN, REGION);
}
@Test
void urlDeletePropagatesARealFailure() {
doThrow(new AwsException("ResourceConflictException", "update in progress", 409))
.when(lambda).deleteFunctionUrlConfig(REGION, FN_ARN, null);
assertThrows(AwsException.class, () -> provisioner.delete("AWS::Lambda::Url", FN_ARN, REGION));
}
}