diff --git a/src/main/java/io/floci/aws/services/cloudformation/SamTransformProcessor.java b/src/main/java/io/floci/aws/services/cloudformation/SamTransformProcessor.java index 04ff0cfb94..fd8a812dde 100644 --- a/src/main/java/io/floci/aws/services/cloudformation/SamTransformProcessor.java +++ b/src/main/java/io/floci/aws/services/cloudformation/SamTransformProcessor.java @@ -1847,8 +1847,12 @@ class SamTransformProcessor { roleProps.set("AssumeRolePolicyDocument", assumePolicy); ArrayNode managedPolicies = objectMapper.createArrayNode(); + JsonNode durableConfig = properties.path("DurableConfig"); + String basicPolicyArn = durableConfig.isObject() && !durableConfig.isEmpty() + ? "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicDurableExecutionRolePolicy" + : "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"; ObjectNode basicExecutionRole = objectMapper.createObjectNode(); - basicExecutionRole.put("Fn::Sub", "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"); + basicExecutionRole.put("Fn::Sub", basicPolicyArn); managedPolicies.add(basicExecutionRole); JsonNode userPolicies = properties.path("Policies"); @@ -1907,6 +1911,8 @@ class SamTransformProcessor { copyIfPresent(properties, "EphemeralStorage", lambdaProps); copyIfPresent(properties, "VpcConfig", lambdaProps); copyIfPresent(properties, "FileSystemConfigs", lambdaProps); + copyIfPresent(properties, "LoggingConfig", lambdaProps); + copyIfPresent(properties, "DurableConfig", lambdaProps); JsonNode tracing = properties.path("Tracing"); if (!tracing.isMissingNode()) { diff --git a/src/test/java/io/floci/aws/services/cloudformation/SamTransformIntegrationTest.java b/src/test/java/io/floci/aws/services/cloudformation/SamTransformIntegrationTest.java index cbb4397fc9..933e6ea5be 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/SamTransformIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/SamTransformIntegrationTest.java @@ -912,6 +912,56 @@ class SamTransformIntegrationTest { .body("Configuration.Environment.Variables.STAGE", equalTo("local")); } + @Test + void samDurableFunctionKeepsDurableAndLoggingConfig() { + String stackName = "sam-durable-stack"; + stacksToDelete.add(stackName); + + String template = """ + AWSTemplateFormatVersion: '2010-09-09' + Transform: AWS::Serverless-2016-10-31 + Globals: + Function: + LoggingConfig: + LogFormat: JSON + ApplicationLogLevel: DEBUG + Resources: + DurableFunc: + Type: AWS::Serverless::Function + Properties: + FunctionName: sam-durable-func + Handler: app.handler + Runtime: python3.13 + InlineCode: "def handler(event, context): return event" + DurableConfig: + ExecutionTimeout: 60 + RetentionPeriodInDays: 3 + """; + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateStack") + .formParam("StackName", stackName) + .formParam("TemplateBody", template) + .formParam("Capabilities.member.1", "CAPABILITY_IAM") + .when() + .post("/") + .then() + .statusCode(200); + + CfnStackWaits.awaitStatus(stackName, "CREATE_COMPLETE"); + + given() + .when() + .get("/2015-03-31/functions/sam-durable-func") + .then() + .statusCode(200) + .body("Configuration.DurableConfig.ExecutionTimeout", equalTo(60)) + .body("Configuration.DurableConfig.RetentionPeriodInDays", equalTo(3)) + .body("Configuration.LoggingConfig.LogFormat", equalTo("JSON")) + .body("Configuration.LoggingConfig.ApplicationLogLevel", equalTo("DEBUG")); + } + @Test void samSimpleTable_createsDynamoDbTable() { String stackName = "sam-table-stack"; diff --git a/src/test/java/io/floci/aws/services/cloudformation/SamTransformProcessorTest.java b/src/test/java/io/floci/aws/services/cloudformation/SamTransformProcessorTest.java index 80f0e99204..4101e6ade3 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/SamTransformProcessorTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/SamTransformProcessorTest.java @@ -1117,6 +1117,57 @@ class SamTransformProcessorTest { lambdaProps.path("FileSystemConfigs").get(0).path("LocalMountPath").asText()); } + @Test + void expandSamTemplate_durableFunctionKeepsDurableAndLoggingConfig() throws Exception { + JsonNode template = objectMapper.readTree(""" + { + "Transform": "AWS::Serverless-2016-10-31", + "Globals": { + "Function": { + "LoggingConfig": {"LogFormat": "JSON", "ApplicationLogLevel": "DEBUG"} + } + }, + "Resources": { + "DurableFn": { + "Type": "AWS::Serverless::Function", + "Properties": { + "Handler": "index.handler", + "Runtime": "python3.13", + "InlineCode": "def handler(e, c): return e", + "DurableConfig": {"ExecutionTimeout": 60, "RetentionPeriodInDays": 3} + } + }, + "PlainFn": { + "Type": "AWS::Serverless::Function", + "Properties": { + "Handler": "index.handler", + "Runtime": "python3.13", + "InlineCode": "def handler(e, c): return e" + } + } + } + } + """); + + JsonNode resources = processor.expandSamTemplate(template).path("Resources"); + + JsonNode durableProps = resources.path("DurableFn").path("Properties"); + assertEquals(objectMapper.readTree("{\"ExecutionTimeout\": 60, \"RetentionPeriodInDays\": 3}"), + durableProps.path("DurableConfig")); + assertEquals(objectMapper.readTree("{\"LogFormat\": \"JSON\", \"ApplicationLogLevel\": \"DEBUG\"}"), + durableProps.path("LoggingConfig")); + assertEquals(objectMapper.readTree(""" + [{"Fn::Sub": "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicDurableExecutionRolePolicy"}] + """), resources.path("DurableFnRole").path("Properties").path("ManagedPolicyArns")); + + JsonNode plainProps = resources.path("PlainFn").path("Properties"); + assertTrue(plainProps.path("DurableConfig").isMissingNode()); + assertEquals("JSON", plainProps.path("LoggingConfig").path("LogFormat").asText()); + assertEquals(objectMapper.readTree(""" + [{"Fn::Sub": "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"}] + """), resources.path("PlainFnRole").path("Properties").path("ManagedPolicyArns")); + } + @Test void expandSamTemplate_functionWithImageConfig() throws Exception { // ImageConfig (EntryPoint/Command/WorkingDirectory overrides for a container-image