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

feat(lambda): avail function log group in the CDK #5878

Merged
merged 11 commits into from
Jan 24, 2020

Conversation

nija-at
Copy link
Contributor

@nija-at nija-at commented Jan 20, 2020

Commit Message

The lambda function's log group is now available for use in the CDK app
so it can be used further to create subscription filters, metric
filters, etc. or in any other way that a regular log group may be used.

The implementation uses an underlying CustomResource to guarantee the
existence of this log group as part of the CloudFormation stack.
However, the custom resource is only created either when the
logRetention property is set or when logGroup getter is called. This
prevents existing stacks that use Lambda function to get the custom
resource and can be opted into.

closes #3838


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

The lambda function's log group is now available for use in the CDK app
so it can be used further to create subscription filters, metric
filters, etc. or in any other way that a regular log group may be used.

The implementation uses an underlying CustomResource to guarantee the
existence of this log group as part of the CloudFormation stack.
A new property called `exposeLogGroup` to enable this, so that existing
stacks that have a Lambda function don't change significantly by
automatically getting this CustomResource, as well as, users who are
not interested in this log group can opt out.

closes #3838
@nija-at nija-at added the contribution/core This is a PR that came from AWS. label Jan 20, 2020
@nija-at nija-at self-assigned this Jan 20, 2020
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@nija-at nija-at added the pr/do-not-merge This PR should not be merged at this time. label Jan 21, 2020
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

eladb
eladb previously requested changes Jan 21, 2020
Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

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

Two notes:

  1. Feels like there is something we can do to lazily create the LogRetention in case logGroup is accessed instead of "polluting" the API with exposeLogGroup, which feels a bit hacky.
  2. What was the rational for implementing ILogGroup by LogRetention instead of just logs.LogGroup.fromLogGroupArn(). Feels like it would have saved a lot code duplication from the logs module.

packages/@aws-cdk/aws-lambda/lib/log-retention.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-lambda/lib/log-retention.ts Outdated Show resolved Hide resolved
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@nija-at
Copy link
Contributor Author

nija-at commented Jan 21, 2020

instead of just logs.LogGroup.fromLogGroupArn(). Feels like it would have saved a lot code duplication from the logs module.

You are correct. That is simpler. I was struggling a bit to get that right. Turns out the solution is quite nifty.

Feels like there is something we can do to lazily create the LogRetention in case logGroup is accessed instead of "polluting" the API with exposeLogGroup, which feels a bit hacky.

I was hoping to give more power to the user to control this, but you're right. Switched this around.

@nija-at nija-at requested a review from eladb January 21, 2020 18:53
@nija-at nija-at removed the pr/do-not-merge This PR should not be merged at this time. label Jan 21, 2020
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@nija-at nija-at dismissed eladb’s stale review January 22, 2020 10:44

addressed feedback

Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Yes, this works. Looks good, please add a ## Commit Message to the PR description and update the text in there to match what you ended up doing.

@nija-at
Copy link
Contributor Author

nija-at commented Jan 24, 2020

Yes, this works. Looks good, please add a ## Commit Message to the PR description and update the text in there to match what you ended up doing.

@rix0rrr - done!

@nija-at nija-at requested a review from rix0rrr January 24, 2020 11:09
packages/@aws-cdk/aws-lambda/lib/function.ts Outdated Show resolved Hide resolved
Comment on lines +582 to +585
const logretention = new LogRetention(this, 'LogRetention', {
logGroupName: `/aws/lambda/${this.functionName}`,
retention: logs.RetentionDays.INFINITE,
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we even need this? Can't we just fromAttributes the LogGroup?

I feel like setting INFINITE retention here may not be the right thing to do... People may have manually changed the retention to something else before they went through this code path, and they probably don't expect you'll be resetting it to Expire: NEVER when they just access the logGroup property...

Now obviously, you could have the problem that the LogGroup will not exist in the service before the function has been invoked at least once (if you don't create it yourself)... In such case, it'd be better to have a custom handler that just creates the log group if missing, but leaves the configured retention if it already exists...

Copy link
Contributor Author

@nija-at nija-at Jan 24, 2020

Choose a reason for hiding this comment

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

Do we even need this? Can't we just fromAttributes the LogGroup?

We need it because the log group might not exist. Take a look at LogRetention if you haven't already and you'll get the idea.

Now obviously, you could have the problem that the LogGroup will not exist in the service before the function has been invoked at least once (if you don't create it yourself)... In such case, it'd be better to have a custom handler that just creates the log group if missing, but leaves the configured retention if it already exists...

LogRetention does exactly that.

they probably don't expect you'll be resetting it to Expire: NEVER when they just access the logGroup property...

Good point. Let me look into that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

they probably don't expect you'll be resetting it to Expire: NEVER when they just access the logGroup property...

Good point. Let me look into that.

Wait a second. They shouldn't have set the retention policy of the function out of band; they should use the logRetention property here. If they did set it out of band, it's expected that a stack deployment may reset it, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's not what Vanilla CloudFormation would do for you... I don't have a problem if you'll be resetting aggressively and consider you have to change it through here, but this should be mentioned in the documentation at the very least!

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe the correct solution is to make the retention property optional and have the custom resource do nothing if the property is unset and the group already exists?

On the other hand, I don't think this is going to come up a lot. Who really precreates Lambda Function Log Groups AND subsequently uses CDK to manage their Lambdas? With generated names? Plus, the reset will only happen on the first deployment, because afterwards none of the resource properties will have changed so CFN is never going to touch this custom resource ever again.

Copy link
Contributor

Choose a reason for hiding this comment

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

My concern is more towards the fact that a getter should be side-effects free, as it's only observing state. There are possible side-effects here, which isn't something you'd expect (and hence check for).

If your lambda logs stuff out that you are not allowed to keep for longer than X for some reason... You may suddenly be in violation of law without knowing it...

An alternative way to go here would be to always create a (regular) LogGroup, and use an auto-generated physical name (instead of ever leaving it out to CloudFormation to decide).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

An alternative way to go here would be to always create a (regular) LogGroup, and use an auto-generated physical name (instead of ever leaving it out to CloudFormation to decide).

I'm not sure I follow your alternative here.
However, reminding you that the lambda service decides that the log group must be /aws/lambda/<function-name> and we have no control over it.

If your lambda logs stuff out that you are not allowed to keep for longer than X for some reason... You may suddenly be in violation of law without knowing it...

You're right, but if you need to comply with the law, you should be setting the logRetention property which will do the correct thing for you. By not managing an AWS resource that is regulated lawfully in you IaaC is already bad.

However, your philosophical point is well taken.

I've updated the README with more documentation on this. Does that satisfy, or do you feel strongly that I change the logic?

@rix0rrr rix0rrr added the pr/do-not-merge This PR should not be merged at this time. label Jan 24, 2020
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@nija-at nija-at removed the pr/do-not-merge This PR should not be merged at this time. label Jan 24, 2020
@mergify
Copy link
Contributor

mergify bot commented Jan 24, 2020

Thank you for contributing! Your pull request is now being automatically merged.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Jan 24, 2020

Thank you for contributing! Your pull request is now being automatically merged.

@mergify mergify bot merged commit fd54a17 into master Jan 24, 2020
@mergify mergify bot deleted the nija-at/lambda-loggroup branch January 24, 2020 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[lambda] expose log group and support metric filter
6 participants