Skip to content
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

[lambda][logs] Make LogRetention construct reusable for any log group not just Lambda's #9671

Closed
1 of 2 tasks
humanzz opened this issue Aug 13, 2020 · 11 comments · Fixed by #9808
Closed
1 of 2 tasks
Assignees
Labels
@aws-cdk/aws-lambda Related to AWS Lambda @aws-cdk/aws-logs Related to Amazon CloudWatch Logs effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2

Comments

@humanzz
Copy link
Contributor

humanzz commented Aug 13, 2020

Various AWS resources autocreate log groups if they don't exist e.g. Lambda functions, SageMaker Endpoints.

For a while, my team have managed to create log groups explicitly for lambda functions to customize the log retention policies. This worked well because Lambda didn't attempt to create the log groups until the first request. However, I believe that with recent changes over the past year in Lambda (VPC improvements, Provisioned Concurrency), log groups can be created before the first request which makes our approach slightly problematic.

We noticed the introduction of LogRetention construct and log retention props for Lambda functions that utilize it to let the function create the log group and have a custom resource that manages setting the retention policy.

We think that the LogRetention construct can be reused outside of just Lambda functions e.g. SageMaker Endpoints.

This is a feature request to update LogsRetention construct and the provider it uses to make them reusable for non-Lambda function log groups.

Use Case

Enable using the LogRetention construct for any AWS resource that auocreates logs to manage those logs retention.
Out of scope is introducing different props to different resource constructs to add that support. That can come in later changes.

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

As my team might be able to contribute to this work, we're asking for feedback on this approach. If you think it's sensible, then one of the questions we have is about the location of this construct (it's currently part of the lambda module) but if it's to become general purpose, it probably should go somewhere else e.g. the logs module.


This is a 🚀 Feature Request

@humanzz humanzz added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Aug 13, 2020
@github-actions github-actions bot added @aws-cdk/aws-lambda Related to AWS Lambda @aws-cdk/aws-logs Related to Amazon CloudWatch Logs labels Aug 13, 2020
@nija-at
Copy link
Contributor

nija-at commented Aug 13, 2020

We currently don't have higher level construct support for sagemaker. This makes sense to implement something like this when we build that there.

@humanzz - do you have a wishlist (besides sagemaker) where this would be useful?

@nija-at nija-at added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed @aws-cdk/aws-logs Related to Amazon CloudWatch Logs labels Aug 13, 2020
@humanzz
Copy link
Contributor Author

humanzz commented Aug 13, 2020

The point is, I'm trying to separate the 2 things

  1. Make LogRetention construct reusable across multiple services. We're willing to contribute here
  2. Other services to implement the same approach. That's the part we want to leave to you guys

The usage of 1 need not be coupled with other higher level constructs. For instance, we use Glue... and Glue creates a central non Glue job specific glue that we'd also like to set retention on.

In terms of the immediate services we have in mind; Lambda ✅ , SageMaker, AWS Chatbot where we're also considering contributing a higher level construct for

AWS Chatbot has an interesting aspect to it though because it's a global service that puts its logs in us-east-1 regardless of the region you create the chatbot resource in. My understanding is that this is also a common pattern for global services.
In our own infrastructure, we're doing a quick PoC that shows that the constructs in 1 above can also handle that.

So in our mind we had a plan a long the lines of

  1. Do the contribution in 1 above
  2. Do the AWS Chatbot contribution utilizing it

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Aug 14, 2020
@jogold
Copy link
Contributor

jogold commented Aug 14, 2020

@humanzz the LogRetention construct is already re-usable outside Lambda. It acts on a log group not a Lambda function. It has been initially located in the aws-lambda module instead of the aws-logs module to avoid a circular dependency between those two modules. Not ideal in terms of discoverability, I agree.

It's currently used in aws-rds for example:

for (const log of this.cloudwatchLogsExports) {
new lambda.LogRetention(this, `LogRetention${log}`, {
logGroupName: `/aws/rds/instance/${this.instanceIdentifier}/${log}`,
retention: this.cloudwatchLogsRetention,
role: this.cloudwatchLogsRetentionRole,
});

@nija-at nija-at added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Aug 14, 2020
@humanzz
Copy link
Contributor Author

humanzz commented Aug 14, 2020

@jogold Thanks for pointing that out. My initial scan of the provider code drove me to the wrong conclusion as I thought the implementation is Lambda-specific

await createLogGroupSafe(`/aws/lambda/${context.functionName}`, retryOptions);
await setRetentionPolicy(`/aws/lambda/${context.functionName}`, retryOptions, 1);
as I thought that's the Lambda function whose logs we were setting the retention for. Turns out these lines are just for the custom resource Lambda function.

The other thing we might try to contribute to (if we figure this out properly) is updating the construct to allow for cross-region log retention setting for the case I described above for Global AWS regions that create logs in us-east-1.

Maybe we can repurpose this issue for that?

@nija-at nija-at removed the closing-soon This issue will automatically close in 4 days unless further comments are made. label Aug 14, 2020
@nija-at
Copy link
Contributor

nija-at commented Aug 14, 2020

@jogold - you're right that it is reusable. However, it creates a dependency on the aws-lambda module can be avoided.

If we switch the implementation to use core.CustomResourceProvider, we should be able to move this to the core module, this will avoid any unneeded bloat on the dependency graph for the module using it.

@eladb - what do you think?

The other thing we might try to contribute to (if we figure this out properly) is updating the construct to allow for cross-region log retention setting for the case I described above for Global AWS regions that create logs in us-east-1.

Maybe we can repurpose this issue for that?

@humanzz - since this is an orthogonal request, would be nice to open a separate issue and track this separately. Could you do that?

@humanzz
Copy link
Contributor Author

humanzz commented Aug 14, 2020

@nija-at sure.

@eladb
Copy link
Contributor

eladb commented Aug 16, 2020

Feels like this should be in the @aws-cdk/aws-logs module.

@nija-at nija-at added effort/medium Medium work item – several days of effort p2 labels Aug 17, 2020
@humanzz
Copy link
Contributor Author

humanzz commented Aug 17, 2020

We'll work on #9703 first (likely a non-breaking change). We can then work on this one (moving the construct which is a breaking change) unless you plan to do it soon.

@nija-at
Copy link
Contributor

nija-at commented Aug 17, 2020

This will have to be a non-breaking change as well.
You will need to move the construct to its new home, mark this as deprecated and try to delegate as much of its logic as possible to the new implementation.

(Suggested ordering) might make sense to move this before making the change to support global AWS services, given that lambda is not one of the global AWS services.

@nija-at nija-at added the @aws-cdk/aws-logs Related to Amazon CloudWatch Logs label Aug 17, 2020
@humanzz
Copy link
Contributor Author

humanzz commented Aug 18, 2020

@nija-at I think our preference is to do the other change first since it's a smaller one.
We'll probably have spare cycles to use for it this week.
For this one, it will probably take a bit longer... because as far as your comments and our initial scan of code see, some code will have to be reimplemented such that there's no dependency on lambda module; more specifically the SingletonFunction.
Also, this being our first contribution, it's easier for us to start with the other more straightforward change.

@humanzz
Copy link
Contributor Author

humanzz commented Aug 18, 2020

@nija-at given i'll be off the next couple of days, I wanted to leave a draft PR so you can have some initial comments on it. I'll be addressing them on Monday
The main things I wanna call out

  • I had to reimplement some singleton function functionality using CfnResource. This led to some changes in logical Ids
  • There's some funky code in there about function dependencies on role/policy which I feel can be improved. I look forwards to your comments on that

nija-at pushed a commit that referenced this issue Aug 26, 2020
…9808)

move LogRetention construct definition from lambda to logs while refactoring it so it does not depend on lambda constructs
this required reimplementing the functionality provided by lambda.SingletonFunction using CfnResource

keep declared classes/interfaces in lambda for compatability while marking them as deprecated
they should be removed in an upcoming breaking change for their current customers in lambda and rds

Fixes #9671
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda @aws-cdk/aws-logs Related to Amazon CloudWatch Logs effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants