Skip to content

Conversation

nsteffens
Copy link

@nsteffens nsteffens commented Aug 22, 2025

Problem

Issue number, if available: #2965

Changes

  • Refactored logging_options_parser.ts to use modern, non-deprecated AWS CDK properties for Lambda logging

  • Replaced the deprecated logRetention property with a dedicated LogGroup resource

  • Updated applicationLogLevelV2 and loggingFormat usage to follow AWS CDK best practices

  • Updated tests to verify the new implementation works correctly

  • Maintained backward compatibility for end users (no API changes)

  • Updated documentation to have logging field documented in general. See PR [Amplify JS] add logging configuration options for functions docs#8428

Why

AWS CDK marked several properties in NodejsFunction as deprecated:

  • logRetention - Recommended to use a dedicated LogGroup resource
  • Creating a LogGroup automatically to be used if logRetention is set. Will fallback to default NodeJsFunction when undefined.

This change ensures that Lambda functions continue to work properly with future AWS CDK versions.

Testing

  • Updated and extended unit tests to verify the new implementation
  • Manually verified that logs are correctly configured with the proper retention settings
  • Ensured backward compatibility for user-facing APIs

Corresponding docs PR, if applicable: TBD

Validation

Have ran tests locally. Should be tested against an AWS account before going live.

Checklist

  • If this PR includes a functional change to the runtime behavior of the code, I have added or updated automated test coverage for this change.
  • If this PR requires a change to the Project Architecture README, I have included that update in this PR.
  • If this PR requires a docs update, I have linked to that docs PR above.
  • If this PR modifies E2E tests, makes changes to resource provisioning, or makes SDK calls, I have run the PR checks with the run-e2e label set.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link

changeset-bot bot commented Aug 22, 2025

🦋 Changeset detected

Latest commit: 259e15b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@aws-amplify/backend-function Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines +66 to +76
// We can't easily check the CDK properties from the instance
// So we verify the synthesized CloudFormation template
const template = JSON.parse(
JSON.stringify(app.synth().getStackArtifact('TestStack').template),
);

// Find the LogGroup resource
const logGroupResources = Object.values(template.Resources).filter(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(resource: any) => resource.Type === 'AWS::Logs::LogGroup',
);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too proud of this, however, I can see that template is also defined ultimate as any. If someone has a better idea how to implement this, feel free to let me know.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you might be able to get the same functionality out of template.resourceCountIs() (which is a part of the aws-cdk-lib/assertions library that is already used in this file). See: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.assertions-readme.html#counting-resources -- you were doing this in the factory test file as well

Comment on lines +81 to +84
const logGroupResource = logGroupResources[0] as {
// eslint-disable-next-line @typescript-eslint/naming-convention
Properties: { RetentionInDays: number };
};
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I hate using eslint-disable commands. However, the resource names are not following the same naming convention as the amplify-backend repo. Open for other recommendations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also be able to use aws-cdk-lib/assertions for this with template.hasResourceProperties -- https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.assertions-readme.html#resource-matching--retrieval

@nsteffens
Copy link
Author

@pahud Could you maybe assist in testing this against a real AWS environment? I have no account at hand that I can run the test suite against without having the risk of possibly causing some costs on my personal credit card.

@ShadowCat567
Copy link
Contributor

Overall this PR looks pretty good, I am going to approve PR workflows to run on it and we'll see how it does. I'll get back to you about testing in a real AWS environment once we see how your PR did with the workflows.

@ShadowCat567
Copy link
Contributor

Based on the PR checks run, it confirms that your won't be introducing any API changes to backend-function (you can ignore the other things it complains about, that test can be a bit sensitive).
To fix the PR changesets error, add a patch bump to '@aws-amplify/backend' in the same changeset file that your bump to @aws-amplify/backend-function is in, see this PR for a similar changeset: https://github.com/aws-amplify/amplify-backend/pull/2444/files (it also introduces changes that only affect backend-function and has a bump to backend)
What is it you want to test with a real AWS environment related to this PR?

Copy link
Contributor

@ShadowCat567 ShadowCat567 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks pretty good

Comment on lines +66 to +76
// We can't easily check the CDK properties from the instance
// So we verify the synthesized CloudFormation template
const template = JSON.parse(
JSON.stringify(app.synth().getStackArtifact('TestStack').template),
);

// Find the LogGroup resource
const logGroupResources = Object.values(template.Resources).filter(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(resource: any) => resource.Type === 'AWS::Logs::LogGroup',
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you might be able to get the same functionality out of template.resourceCountIs() (which is a part of the aws-cdk-lib/assertions library that is already used in this file). See: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.assertions-readme.html#counting-resources -- you were doing this in the factory test file as well

Comment on lines +81 to +84
const logGroupResource = logGroupResources[0] as {
// eslint-disable-next-line @typescript-eslint/naming-convention
Properties: { RetentionInDays: number };
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also be able to use aws-cdk-lib/assertions for this with template.hasResourceProperties -- https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.assertions-readme.html#resource-matching--retrieval


/**
* Converts logging options to CDK format using non-deprecated properties
* Note: This function no longer includes 'retention' in the return object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the note about retention being removed from here and references to the deprecated logRetention property in the other comments in this file, this will not be useful for people in the future who don't need to know this was previously implemented with logRetention.

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.

2 participants