Skip to content

Functions logging #2245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Nov 27, 2024
Merged

Functions logging #2245

merged 17 commits into from
Nov 27, 2024

Conversation

sobolk
Copy link
Member

@sobolk sobolk commented Nov 19, 2024

Problem

Implements feature request #2058 .

Changes

  1. Add types and converters in platform core for cdk enums
  2. Add new package entry point in platform core to avoid loading cdk eagerly for other purposes (bundling)
  3. Adds cdk as peer dep to platform core (it's already transitive dep via platform-types)
  4. Adds type aliases in backend functions and backend ai packages.

DX

image

image

image

image

image

Validation

Added tests

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 Nov 19, 2024

🦋 Changeset detected

Latest commit: 23a7409

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

This PR includes changesets to release 5 packages
Name Type
@aws-amplify/ai-constructs Minor
@aws-amplify/backend-ai Minor
@aws-amplify/backend-function Minor
@aws-amplify/backend Minor
@aws-amplify/platform-core Minor

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

@ataylorme
Copy link
Contributor

@sobolk Why logging only and not all CDK supported parameters? See the conversation in #1971

I am grateful for this PR, it is definitely and improvement, just curious about the reasoning behind the limited scope

@sobolk
Copy link
Member Author

sobolk commented Nov 27, 2024

@sobolk Why logging only and not all CDK supported parameters? See the conversation in #1971

I am grateful for this PR, it is definitely and improvement, just curious about the reasoning behind the limited scope

The reason is that we're using some of these parameters for our internal implementation purposes. Therefore, exposing them in uncontrolled way will impact some of our functionalities like secret resolution or bundling.

For "just give me all CDK knobs" we're evaluating different approach described here #1543 and prototyped here #1602 .

@sobolk sobolk marked this pull request as ready for review November 27, 2024 03:48
@sobolk sobolk requested review from a team as code owners November 27, 2024 03:48
@ataylorme
Copy link
Contributor

Thank you for the additional context. If someone really wants all the CDK knobs the can use CDK. Some trade offs are expected with a managed solution such as Amplify.

thank you to you and the other contributors for your work on Amplify!

Comment on lines 104 to 112
retention: RetentionDays.INFINITE,
retention: this.props.logging?.retention,
Copy link
Contributor

Choose a reason for hiding this comment

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

is the default still INFINITE? Should we include it regardless to avoid CDK breaking it in future?

Copy link
Member Author

Choose a reason for hiding this comment

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

is the default still INFINITE?

yes, but we can be explicit about this.

Comment on lines +460 to +462
assert.ok(
Object.keys(lambdas).some((key) => key.startsWith('LogRetention'))
);
Copy link
Contributor

Choose a reason for hiding this comment

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

no way to assert the actual retention period?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll double check.

Copy link
Member Author

Choose a reason for hiding this comment

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

I found something. added below.

* Converts LogRetention to CDK types.
*/
export class LogRetentionConverter {
toRetentionDays = (
Copy link
Contributor

Choose a reason for hiding this comment

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

The converted type is specific to CDK, we should mention it as such.

Suggested change
toRetentionDays = (
toCDKLogsRetentionDays = (

* Converts LogLevel to CDK types.
*/
export class LogLevelConverter {
toApplicationLogLevel = (logLevel: LogLevel | undefined) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
toApplicationLogLevel = (logLevel: LogLevel | undefined) => {
toCDKLambdaApplicationLogLevel = (logLevel: LogLevel | undefined) => {

Comment on lines +457 to +459
// Enabling log retention adds extra lambda.
template.resourceCountIs('AWS::Lambda::Function', 2);
const lambdas = template.findResources('AWS::Lambda::Function');
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to avoid creating this extra lambda by creating the logGroup ourselves https://repost.aws/questions/QUzCgY0gSIQNGHZc6frGcI7g/log-retention-lambda#ANwKiEC22QQ-uV0r5yi0JdBQ?

Copy link
Member Author

Choose a reason for hiding this comment

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

This has been tried and adopted in conversation handler.
However, it was easier there because it was a new thing.

Here the problem space is constrained in such a way that:

  1. In order to use custom log group (us or customer) we have to pass reference to function. So log group must be created before calling NodejsFunction ctor.
  2. In order to retain log group naming we need function name first. This is not available until NodejsFunction ctor is called, making it circlular dependency problem.
  3. When working on conversation handler I tried to compute function name before creating log group and nodejsfunction but I was not successful at producing function name that would match implicit cdk algorithm. That computation is internalized and I didn't find a way to implement it using available apis from cdk.
  4. Changing function name and/or log group name algorithm at this point would mean destructive changes.

I wanted to avoid destructive changes, so I pursued a solution based on this extra lambda.

I think the proper solution to expose log group knobs would be to add another alternative to LoggingOptions in the future where we {..existing...} | { logGroupKnobs } .

wdyt ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good, at least it's only one extra function for all customer defined lambda functions.

@@ -136,13 +136,13 @@ export type LocalConfigurationFileName = 'usage_data_preferences.json';
// @public
class LogLevelConverter {
// (undocumented)
toApplicationLogLevel: (logLevel: LogLevel | undefined) => ApplicationLogLevel | undefined;
toCDKApplicationLogLevel: (logLevel: LogLevel | undefined) => ApplicationLogLevel | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd still suggest to include lambda in there, as other AWS services may have different log level names or types.

Suggested change
toCDKApplicationLogLevel: (logLevel: LogLevel | undefined) => ApplicationLogLevel | undefined;
toCDKLambdaApplicationLogLevel: (logLevel: LogLevel | undefined) => CDKLambdaApplicationLogLevel | undefined;

Copy link
Member Author

Choose a reason for hiding this comment

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

I can change the toCDKApplicationLogLevel.

But return type is already import { ApplicationLogLevel } from 'aws-cdk-lib/aws-lambda';
Even if we alias this for our purposes, customer still needs to import from cdk lib.

Copy link
Member Author

Choose a reason for hiding this comment

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

btw. I didn't notice Lambda in first comment, sorry.

Amplifiyer
Amplifiyer previously approved these changes Nov 27, 2024
@sobolk sobolk merged commit 65abf6a into main Nov 27, 2024
40 checks passed
@sobolk sobolk deleted the functions-logging branch November 27, 2024 17:44
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.

4 participants