forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lambda): add support for log retention
Adds a new property `logRetentionDays` on `Function` to control the log retention policy of the function logs in CloudWatch Logs. The implementation uses a Custom Resource to create the log group if it doesn't exist yet and to set the retention policy as discussed in aws#667. A retention policy of 1 day is set on the logs of the Lambda provider. The different retention days supported by CloudWatch Logs have been centralized in `@aws-cdk/aws-logs`. Some have been renamed to better match the console experience. Closes aws#667 BREAKING CHANGE: `cloudWatchLogsRetentionTimeDays` in `@aws-cdk/aws-cloudtrail` now uses a `logs.RetentionDays` instead of a `LogRetention`.
- Loading branch information
Showing
13 changed files
with
820 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/@aws-cdk/aws-lambda/lib/log-retention/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// tslint:disable:no-console | ||
import AWS = require('aws-sdk'); | ||
|
||
const cloudwatchlogs = new AWS.CloudWatchLogs({ apiVersion: '2014-03-28' }); | ||
|
||
/** | ||
* Creates a log group and doesn't throw if it exists. | ||
* | ||
* @param logGroupName the name of the log group to create | ||
*/ | ||
/* istanbul ignore next */ | ||
async function createLogGroupSafe(logGroupName: string) { | ||
try { // Try to create the log group | ||
await cloudwatchlogs.createLogGroup({ logGroupName }).promise(); | ||
} catch (e) { | ||
if (e.code !== 'ResourceAlreadyExistsException') { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Puts or deletes a retention policy on a log group. | ||
* | ||
* @param logGroupName the name of the log group to create | ||
* @param retentionInDays the number of days to retain the log events in the specified log group. | ||
*/ | ||
/* istanbul ignore next */ | ||
async function setRetentionPolicy(logGroupName: string, retentionInDays?: number) { | ||
if (!retentionInDays) { | ||
await cloudwatchlogs.deleteRetentionPolicy({ logGroupName }).promise(); | ||
} else { | ||
await cloudwatchlogs.putRetentionPolicy({ logGroupName, retentionInDays }).promise(); | ||
} | ||
} | ||
|
||
/* istanbul ignore next */ | ||
export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent, context: AWSLambda.Context) { | ||
try { | ||
console.log(JSON.stringify(event)); | ||
|
||
// The target log group | ||
const logGroupName = `/aws/lambda/${event.ResourceProperties.FunctionName}`; | ||
|
||
if (event.RequestType === 'Create' || event.RequestType === 'Update') { | ||
// Act on the target log group | ||
await createLogGroupSafe(logGroupName); | ||
await setRetentionPolicy(logGroupName, event.ResourceProperties.RetentionInDays); | ||
|
||
if (event.RequestType === 'Create') { | ||
// Set a retention policy of 1 day on the logs of this function. The log | ||
// group for this function should already exist at this stage because we | ||
// already logged the event but due to the async nature of Lambda logging | ||
// there could be a race condition. So we also try to create the log group | ||
// of this function first. | ||
await createLogGroupSafe(`/aws/lambda/${context.functionName}`); | ||
await setRetentionPolicy(`/aws/lambda/${context.functionName}`, 1); | ||
} | ||
} | ||
|
||
await respond('SUCCESS', 'OK', logGroupName); | ||
} catch (e) { | ||
console.log(e); | ||
|
||
await respond('FAILED', e.message, context.logStreamName); | ||
} | ||
|
||
function respond(responseStatus: string, reason: string, physicalResourceId: string) { | ||
const responseBody = JSON.stringify({ | ||
Status: responseStatus, | ||
Reason: reason, | ||
PhysicalResourceId: physicalResourceId, | ||
StackId: event.StackId, | ||
RequestId: event.RequestId, | ||
LogicalResourceId: event.LogicalResourceId, | ||
Data: {} | ||
}); | ||
|
||
console.log('Responding', responseBody); | ||
|
||
const parsedUrl = require('url').parse(event.ResponseURL); | ||
const requestOptions = { | ||
hostname: parsedUrl.hostname, | ||
path: parsedUrl.path, | ||
method: 'PUT', | ||
headers: { 'content-type': '', 'content-length': responseBody.length } | ||
}; | ||
|
||
return new Promise((resolve, reject) => { | ||
try { | ||
const request = require('https').request(requestOptions, resolve); | ||
request.on('error', reject); | ||
request.write(responseBody); | ||
request.end(); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.