mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cloudformation): pass Lambda DurableConfig through (#5218)
* feat(cloudformation): pass Lambda DurableConfig through AWS::Lambda::Function.DurableConfig reaches CreateFunction and UpdateFunctionConfiguration. As on AWS, a RetentionPeriodInDays the template leaves out is 14 on update too, a durable function created without Timeout gets min(ExecutionTimeout, 900), and adding or removing DurableConfig replaces the function. * test(terraform): cover aws_lambda_function durable_config The provider creates a durable function, reads it back without a diff, updates execution_timeout in place, and before destroy stops a RUNNING durable execution through ListDurableExecutionsByFunction, StopDurableExecution and GetDurableExecution. * fix(cloudformation): honor AWS::NoValue, cleared keys and large numbers in DurableConfig A DurableConfig that Fn::If removes with AWS::NoValue leaves the function plain. A KMSKeyArn the template leaves out or sets to "" clears the key, as CloudFormation does on AWS, since Lambda keeps a stored key when the update omits the member. A number beyond an integer reaches Lambda unchanged, so its range check rejects it. * fix(cloudformation): reject a DurableConfig that is not an object Only an intrinsic that resolves to AWS::NoValue removes DurableConfig. CloudFormation rejects a string, number or list in its schema check before the function is touched, so an update with one no longer replaces and deletes a durable function. * test(cloudformation): cover Lambda DurableConfig with the AWS SDK The CloudFormation and Lambda SDK clients create a durable function, update DurableConfig in place, and remove it by replacing the function. * fix(cloudformation): require ExecutionTimeout and known members in DurableConfig CloudFormation checks DurableConfig against the schema on update too: an update without ExecutionTimeout, or with a member the schema does not list, fails before the function is touched. Floci let such an update through and kept the old timeout. * refactor(cloudformation): look up a Lambda function once per deploy The name choice and the replacement decision now share one lookup and one durable check, so they cannot disagree.
This commit is contained in:
1 parent
bdb37f0ca3
commit
ee2d1a68a6
9 files changed
+667
-8
No files matched your search
+163
@@ -0,0 +1,163 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
/**
|
||||
* {@code AWS::Lambda::Function.DurableConfig} through CloudFormation: created, updated in place,
|
||||
* and removed by replacing the function, with the defaults CloudFormation applies on AWS.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationLambdaDurableConfigIntegrationTest {
|
||||
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
|
||||
private static final String LAMBDA = "/2015-03-31/functions/";
|
||||
|
||||
@Test
|
||||
void durableConfigIsCreatedUpdatedInPlaceAndRemovedByReplacement() {
|
||||
String stackName = "cfn-lambda-durable-" + Long.toString(System.nanoTime(), 36);
|
||||
|
||||
deploy("CreateStack", stackName, template("""
|
||||
"DurableConfig": {"ExecutionTimeout": {"Ref": "ExecutionTimeout"}, "RetentionPeriodInDays": 3}
|
||||
"""), "60");
|
||||
String created = functionName(stackName);
|
||||
given()
|
||||
.when()
|
||||
.get(LAMBDA + created + "/configuration")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Timeout", equalTo(60))
|
||||
.body("DurableConfig.ExecutionTimeout", equalTo(60))
|
||||
.body("DurableConfig.RetentionPeriodInDays", equalTo(3))
|
||||
.body("LoggingConfig.LogFormat", equalTo("JSON"));
|
||||
given()
|
||||
.when()
|
||||
.get(LAMBDA + created + "/configuration?Qualifier=1")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DurableConfig.ExecutionTimeout", equalTo(60));
|
||||
|
||||
deploy("UpdateStack", stackName, template("""
|
||||
"DurableConfig": {"ExecutionTimeout": {"Ref": "ExecutionTimeout"}}
|
||||
"""), "120");
|
||||
assertEquals(created, functionName(stackName), "a changed DurableConfig updates the function in place");
|
||||
given()
|
||||
.when()
|
||||
.get(LAMBDA + created + "/configuration")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Timeout", equalTo(3))
|
||||
.body("DurableConfig.ExecutionTimeout", equalTo(120))
|
||||
.body("DurableConfig.RetentionPeriodInDays", equalTo(14));
|
||||
|
||||
deploy("UpdateStack", stackName, template("\"Description\": \"plain\""), "120");
|
||||
String replaced = functionName(stackName);
|
||||
assertNotEquals(created, replaced, "Lambda cannot remove a DurableConfig, so the function is replaced");
|
||||
given()
|
||||
.when()
|
||||
.get(LAMBDA + replaced + "/configuration")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DurableConfig", nullValue())
|
||||
.body("LoggingConfig.LogFormat", equalTo("Text"));
|
||||
given()
|
||||
.when()
|
||||
.get(LAMBDA + created)
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDurableConfigRemovedWithNoValueLeavesTheFunctionPlain() {
|
||||
String stackName = "cfn-lambda-novalue-" + Long.toString(System.nanoTime(), 36);
|
||||
String template = """
|
||||
{
|
||||
"Parameters": {"ExecutionTimeout": {"Type": "Number"}},
|
||||
"Conditions": {"Durable": {"Fn::Equals": ["yes", "no"]}},
|
||||
"Resources": {
|
||||
"Fn": {
|
||||
"Type": "AWS::Lambda::Function",
|
||||
"Properties": {
|
||||
"Runtime": "python3.14",
|
||||
"Handler": "index.handler",
|
||||
"Role": "arn:aws:iam::000000000000:role/lambda-role",
|
||||
"Code": {"ZipFile": "def handler(e, c): return 'ok'"},
|
||||
"DurableConfig": {"Fn::If": ["Durable", {"ExecutionTimeout": {"Ref": "ExecutionTimeout"}},
|
||||
{"Ref": "AWS::NoValue"}]}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Outputs": {"Name": {"Value": {"Ref": "Fn"}}}
|
||||
}
|
||||
""";
|
||||
|
||||
deploy("CreateStack", stackName, template, "60");
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get(LAMBDA + functionName(stackName) + "/configuration")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DurableConfig", nullValue());
|
||||
}
|
||||
|
||||
private static String template(String functionProperty) {
|
||||
return """
|
||||
{
|
||||
"Parameters": {"ExecutionTimeout": {"Type": "Number"}},
|
||||
"Resources": {
|
||||
"Fn": {
|
||||
"Type": "AWS::Lambda::Function",
|
||||
"Properties": {
|
||||
"Runtime": "python3.14",
|
||||
"Handler": "index.handler",
|
||||
"Role": "arn:aws:iam::000000000000:role/lambda-role",
|
||||
"Code": {"ZipFile": "def handler(e, c): return 'ok'"},
|
||||
%s
|
||||
}
|
||||
},
|
||||
"Ver": {"Type": "AWS::Lambda::Version", "Properties": {"FunctionName": {"Ref": "Fn"}}}
|
||||
},
|
||||
"Outputs": {"Name": {"Value": {"Ref": "Fn"}}}
|
||||
}
|
||||
""".formatted(functionProperty);
|
||||
}
|
||||
|
||||
private static void deploy(String action, String stackName, String template, String executionTimeout) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", action)
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.formParam("Parameters.member.1.ParameterKey", "ExecutionTimeout")
|
||||
.formParam("Parameters.member.1.ParameterValue", executionTimeout)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName);
|
||||
assertEquals("CreateStack".equals(action) ? "CREATE_COMPLETE" : "UPDATE_COMPLETE", state.status(),
|
||||
state.reason());
|
||||
}
|
||||
|
||||
private static String functionName(String stackName) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("**.find { it.name() == 'OutputValue' }");
|
||||
}
|
||||
}
|
||||
+92
@@ -20,6 +20,7 @@ import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyMap;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
@@ -27,6 +28,7 @@ import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -183,6 +185,96 @@ class LambdaCfnProvisionerTest {
|
||||
verify(lambda).tagResource(FUNCTION_ARN + "my-fn", Map.of("team", "b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addingDurableConfigToANamedFunctionCannotReplaceIt() {
|
||||
stubInPlaceUpdate("my-fn", Map.of());
|
||||
ObjectNode props = props("my-fn");
|
||||
props.putObject("DurableConfig").put("ExecutionTimeout", 60);
|
||||
|
||||
AwsException rejected = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(function("my-fn"), props, updateCtx("my-fn")));
|
||||
|
||||
assertEquals("Cannot replace Lambda function my-fn without a new FunctionName", rejected.getMessage());
|
||||
verify(lambda, never()).updateFunctionConfiguration(anyString(), anyString(), anyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDurableConfigThatIsNotAnObjectLeavesTheFunctionAlone() {
|
||||
// A generated name, so treating the value as removed would replace and delete the function.
|
||||
String generated = "my-stack-MyFunction-ABCDEFGHIJKL";
|
||||
LambdaFunction existing = lambdaFunction(generated);
|
||||
existing.setDurableExecutionTimeout(60);
|
||||
when(lambda.getFunction(REGION, generated)).thenReturn(existing);
|
||||
when(lambda.createFunction(eq(REGION), anyMap())).thenReturn(lambdaFunction("replacement"));
|
||||
for (JsonNode malformed : List.of(mapper.getNodeFactory().textNode("abc"), mapper.getNodeFactory().textNode(""),
|
||||
mapper.getNodeFactory().numberNode(5), mapper.createArrayNode())) {
|
||||
StackResource r = function(generated);
|
||||
r.getAttributes().put("FlociLambdaFunctionNameMode", "generated");
|
||||
ObjectNode props = mapper.createObjectNode();
|
||||
props.set("DurableConfig", malformed);
|
||||
|
||||
AwsException rejected = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(r, props, updateCtx(generated)));
|
||||
|
||||
assertEquals("ValidationError", rejected.getErrorCode());
|
||||
}
|
||||
verify(lambda, never()).createFunction(anyString(), anyMap());
|
||||
verify(lambda, never()).updateFunctionConfiguration(anyString(), anyString(), anyMap());
|
||||
verify(lambda, never()).deleteFunction(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDurableConfigWithoutExecutionTimeoutOrWithAnUnknownMemberIsRejectedOnUpdate() {
|
||||
LambdaFunction existing = lambdaFunction("my-fn");
|
||||
existing.setDurableExecutionTimeout(60);
|
||||
when(lambda.getFunction(REGION, "my-fn")).thenReturn(existing);
|
||||
ObjectNode withoutTimeout = props("my-fn");
|
||||
withoutTimeout.putObject("DurableConfig").put("RetentionPeriodInDays", 5);
|
||||
ObjectNode unknownMember = props("my-fn");
|
||||
unknownMember.putObject("DurableConfig").put("ExecutionTimeout", 90).put("executionTimeout", 1);
|
||||
|
||||
AwsException missing = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(function("my-fn"), withoutTimeout, updateCtx("my-fn")));
|
||||
AwsException unknown = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(function("my-fn"), unknownMember, updateCtx("my-fn")));
|
||||
|
||||
assertEquals("AWS::Lambda::Function DurableConfig requires ExecutionTimeout", missing.getMessage());
|
||||
assertEquals("AWS::Lambda::Function DurableConfig does not allow the member executionTimeout",
|
||||
unknown.getMessage());
|
||||
verify(lambda, never()).updateFunctionConfiguration(anyString(), anyString(), anyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExecutionTimeoutBeyondAnIntegerReachesLambdaUnchanged() {
|
||||
when(lambda.createFunction(eq(REGION), anyMap())).thenReturn(lambdaFunction("my-fn"));
|
||||
ObjectNode props = props("my-fn");
|
||||
props.putObject("DurableConfig").put("ExecutionTimeout", 4_294_967_356L);
|
||||
|
||||
provisioner.provision(function(null), props, ctx());
|
||||
|
||||
assertEquals(4_294_967_356L, ((Map<?, ?>) capturedCreateRequest().get("DurableConfig")).get("ExecutionTimeout"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKmsKeyArnTheTemplateDropsIsClearedOnUpdate() {
|
||||
LambdaFunction existing = lambdaFunction("my-fn");
|
||||
existing.setDurableExecutionTimeout(60);
|
||||
existing.setDurableRetentionPeriodInDays(14);
|
||||
existing.setDurableKmsKeyArn("arn:aws:kms:us-east-1:000000000000:key/k1");
|
||||
when(lambda.getFunction(REGION, "my-fn")).thenReturn(existing);
|
||||
when(lambda.updateFunctionConfiguration(anyString(), anyString(), anyMap())).thenReturn(existing);
|
||||
when(lambda.updateFunctionCode(anyString(), anyString(), anyMap())).thenReturn(existing);
|
||||
when(lambda.listTags(FUNCTION_ARN + "my-fn")).thenReturn(new HashMap<>());
|
||||
ObjectNode props = props("my-fn");
|
||||
props.putObject("DurableConfig").put("ExecutionTimeout", 60);
|
||||
|
||||
provisioner.provision(function("my-fn"), props, updateCtx("my-fn"));
|
||||
|
||||
verify(lambda).updateFunctionConfiguration(eq(REGION), eq("my-fn"),
|
||||
argThat(request -> "".equals(((Map<?, ?>) request.get("DurableConfig")).get("KMSKeyArn"))));
|
||||
verify(lambda, times(1)).getFunction(REGION, "my-fn");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Object> capturedCreateRequest() {
|
||||
ArgumentCaptor<Map<String, Object>> request = ArgumentCaptor.forClass(Map.class);
|
||||
|
||||
Reference in new issue
Block a user