feat: add LogGroup property to AWS::Serverless::Function for stack-managed CloudWatch log groups#3957
feat: add LogGroup property to AWS::Serverless::Function for stack-managed CloudWatch log groups#3957GuidoNebiolo wants to merge 5 commits into
Conversation
…oup property Add a LogGroup property to AWS::Serverless::Function that generates a stack-managed AWS::Logs::LogGroup. By default the group uses the conventional /aws/lambda/<function-name> name (no LoggingConfig or IAM change needed, since the group coincides with Lambda's default destination and AWSLambdaBasicExecutionRole already grants log writes). A custom LogGroupName binds the function via its native LoggingConfig.LogGroup. RetentionInDays is passed through and DeletionPolicy (Delete by default, or Retain) is applied to both DeletionPolicy and UpdateReplacePolicy so the group is deleted with the stack, avoiding orphaned log groups. When absent, output is unchanged for backward compatibility. Refs aws#2665, aws#3791, aws#1216, aws#2057
Cover the default log group name (Mechanism 1), custom log group name with LoggingConfig wiring (Mechanism 2), DeletionPolicy Retain, Condition propagation to the log group, Globals inheritance/override, and the invalid DeletionPolicy error case, across all partitions. Refs aws#2665, aws#3791, aws#1216, aws#2057
|
I don't see any check or guardrails in case a user sets My initial instinct would be to prefer adding the log group name and other information as part of MyFunction:
Properties:
..
LoggingConfig:
CreateLogGroup: true
LogGroup: "..."
AdditionalProperties:
DeletionPolicy: ...
...Although it is a little more verbose, but it would prevent users from having the log group information separate in two different places. There's also the option of just accomplishing the same by just manually defining the LogGroup outside of the function directly. which is fairly straightforward. The only case where this is not that simple is when you want to continue using Lambda's default name The other issue when you want to keep using Lambda's default name (in your solution too, and it's mentioned in a few of the linked GitHub issues) is the fact that if for some reason the function gets invoked before the log group gets created, then Lambda will still create the log group, and the CFN deployment would fail when it tries to create the log group. The point is that there are some edge cases that makes this not "as simple" as adding a new property. We're very interested in making a good solution for customers, but we don't want to have a confusing experience that can make it harder for new customers. Let me know your opinions about the above issues, and we'll continue the discussion internally with the team too. |
|
Thanks @valerena , these are really helpful points. Going through them:
Proposal: add the conflict validation from (1), lean the feature toward the default-name use case from (3), and document (4). On (2) I'm open, do you prefer extending LoggingConfig or a dedicated property? |
Issue #, if available
Refs #2665, #3791, #1216, #2057
Description of changes
Adds a new
LogGroupproperty toAWS::Serverless::Functionthat generates a stack-managedAWS::Logs::LogGroup, so the log group is created/updated/deleted together with the stack and its retention is configurable.Two naming mechanisms:
LogGroupName): the group uses the conventional/aws/lambda/<function-name>name viaFn::Sub.LoggingConfigis left untouched and no extra IAM is needed — the name coincides with Lambda's default destination andAWSLambdaBasicExecutionRolealready grants log writes.LogGroupName): binds the function via its nativeLoggingConfig.LogGroup, merging with any existingLoggingConfig.Supported sub-properties:
RetentionInDays(pass-through),DeletionPolicy(Deletedefault, orRetain) applied to bothDeletionPolicyandUpdateReplacePolicy, andLogGroupName. Inheritable viaGlobals. When the property is absent, the transform output is unchanged (backward compatible).This makes retention configurable and deletes the log group with the stack, addressing orphaned log groups (#1216).
Description of how you validated changes
DeletionPolicy: Retain,Conditionpropagation, Globals inheritance/override, and an invalidDeletionPolicyerror case.LogGroupresource model and_construct_log_group.make pris green: ruff, mypy --strict, format-check (schema regenerated), full test suite passing with 95.64% coverage.AI usage disclosure
This contribution was developed with the assistance of a generative AI coding tool. All code and tests were reviewed and are fully understood by the author, who is able to answer maintainer questions during review. The generated code was held to the same quality standards as hand-written code, and the diff/tests were kept minimal to ease review.
Checklist