Skip to content

feat: add LogGroup property to AWS::Serverless::Function for stack-managed CloudWatch log groups#3957

Open
GuidoNebiolo wants to merge 5 commits into
aws:developfrom
GuidoNebiolo:feat/function-log-group
Open

feat: add LogGroup property to AWS::Serverless::Function for stack-managed CloudWatch log groups#3957
GuidoNebiolo wants to merge 5 commits into
aws:developfrom
GuidoNebiolo:feat/function-log-group

Conversation

@GuidoNebiolo

Copy link
Copy Markdown

Issue #, if available

Refs #2665, #3791, #1216, #2057

Description of changes

Adds a new LogGroup property to AWS::Serverless::Function that generates a stack-managed AWS::Logs::LogGroup, so the log group is created/updated/deleted together with the stack and its retention is configurable.

Two naming mechanisms:

  • Default (no LogGroupName): the group uses the conventional /aws/lambda/<function-name> name via Fn::Sub. LoggingConfig is left untouched and no extra IAM is needed — the name coincides with Lambda's default destination and AWSLambdaBasicExecutionRole already grants log writes.
  • Custom name (LogGroupName): binds the function via its native LoggingConfig.LogGroup, merging with any existing LoggingConfig.

Supported sub-properties: RetentionInDays (pass-through), DeletionPolicy (Delete default, or Retain) applied to both DeletionPolicy and UpdateReplacePolicy, and LogGroupName. Inheritable via Globals. 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

  • Added transform tests across all partitions (aws, aws-cn, aws-us-gov): default name, custom name with LoggingConfig wiring, DeletionPolicy: Retain, Condition propagation, Globals inheritance/override, and an invalid DeletionPolicy error case.
  • Added unit tests for the LogGroup resource model and _construct_log_group.
  • make pr is 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

  • Review the generative AI contribution guidelines
  • Adheres to the development guidelines
  • Add/update transform tests
    • Using correct values
    • Using wrong values
  • Add/update integration tests

Add a LogGroup resource model backing the AWS::Logs::LogGroup CloudFormation type, with runtime attributes for Ref (name) and Fn::GetAtt Arn. This is the building block for a stack-managed CloudWatch log group on AWS::Serverless::Function.

Refs aws#2665, aws#3791, aws#1216, aws#2057
…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
Add the FunctionLogGroup model (RetentionInDays, DeletionPolicy, LogGroupName) to the Function Properties and Globals schema, document the new properties in sam-docs, and regenerate the SAM and unified JSON schemas.

Refs aws#2665, aws#3791
Add LogGroup to the Function properties supported in the Globals section so a common log group configuration (for example retention or deletion policy) can be defined once and inherited by all functions, with resource-level overrides merged on top.

Refs aws#2665, aws#3791
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant