From 24e5bfaa28961a97af5fa329412b366ea8564df4 Mon Sep 17 00:00:00 2001 From: ryndaniels Date: Mon, 29 Jul 2024 10:41:03 +0300 Subject: [PATCH] add option for lambda log retention period --- lib/shortcuts/api.md | 1 + lib/shortcuts/lambda.js | 6 ++-- test/fixtures/shortcuts/lambda-full.json | 39 ++++++------------------ test/shortcuts.test.js | 3 +- 4 files changed, 17 insertions(+), 32 deletions(-) diff --git a/lib/shortcuts/api.md b/lib/shortcuts/api.md index 2466446..b578f40 100644 --- a/lib/shortcuts/api.md +++ b/lib/shortcuts/api.md @@ -422,6 +422,7 @@ Log Group, a Role, an Alarm on function errors, and the Lambda Function itself. | [options.EvaluateLowSampleCountPercentile] | String | | See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html#cfn-cloudwatch-alarms-evaluatelowsamplecountpercentile). | | [options.ExtendedStatistic] | String | | See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html#cfn-cloudwatch-alarms-extendedstatistic)] | | [options.OKActions] | Array.<String> | | See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html#cfn-cloudwatch-alarms-okactions). | +| [options.LogRetentionInDays] | Number | 14 | How long to retain CloudWatch logs for this Lambda function. See [AWS Documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-loggroup.html) for allowed values. | **Example** ```js diff --git a/lib/shortcuts/lambda.js b/lib/shortcuts/lambda.js index 6c54cf5..99d0b66 100644 --- a/lib/shortcuts/lambda.js +++ b/lib/shortcuts/lambda.js @@ -46,6 +46,7 @@ const ServiceRole = require('./service-role'); * @param {String} [options.EvaluateLowSampleCountPercentile=undefined] - See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html#cfn-cloudwatch-alarms-evaluatelowsamplecountpercentile). * @param {String} [options.ExtendedStatistic=undefined] - See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html#cfn-cloudwatch-alarms-extendedstatistic)] * @param {Array} [options.OKActions=undefined] - See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html#cfn-cloudwatch-alarms-okactions). + * @param {Number} [options.LogRetentionInDays=14] - How long to retain CloudWatch logs for this Lambda function. See [AWS Documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-loggroup.html) for allowed values. * * @example * const cf = require('@mapbox/cloudfriend'); @@ -104,7 +105,8 @@ class Lambda { TreatMissingData = 'notBreaching', EvaluateLowSampleCountPercentile, ExtendedStatistic, - OKActions + OKActions, + LogRetentionInDays = 14 } = options; if (options.EvaluationPeriods < Math.ceil(Timeout / Period)) @@ -138,7 +140,7 @@ class Lambda { LogGroupName: { 'Fn::Sub': ['/aws/lambda/${name}', { name: FunctionName }] }, - RetentionInDays: 14 + RetentionInDays: LogRetentionInDays } }, diff --git a/test/fixtures/shortcuts/lambda-full.json b/test/fixtures/shortcuts/lambda-full.json index a36b639..5a5a480 100644 --- a/test/fixtures/shortcuts/lambda-full.json +++ b/test/fixtures/shortcuts/lambda-full.json @@ -6,10 +6,7 @@ "Mappings": {}, "Conditions": { "Always": { - "Fn::Equals": [ - "1", - "1" - ] + "Fn::Equals": ["1", "1"] } }, "Resources": { @@ -28,7 +25,7 @@ } ] }, - "RetentionInDays": 14 + "RetentionInDays": 30 } }, "MyLambda": { @@ -52,9 +49,7 @@ "FunctionName": "my-function", "Handler": "index.something", "KmsKeyArn": "arn:aws:kms:us-east-1:123456789012:key/fake", - "Layers": [ - "arn:aws:fake:layer/abc" - ], + "Layers": ["arn:aws:fake:layer/abc"], "MemorySize": 512, "ReservedConcurrentExecutions": 10, "Runtime": "nodejs18.x", @@ -63,12 +58,8 @@ "Mode": "Active" }, "VpcConfig": { - "SecurityGroupIds": [ - "sg-12345" - ], - "SubnetIds": [ - "fake" - ] + "SecurityGroupIds": ["sg-12345"], + "SubnetIds": ["fake"] }, "Tags": [ { @@ -77,10 +68,7 @@ } ], "Role": { - "Fn::GetAtt": [ - "MyLambdaRole", - "Arn" - ] + "Fn::GetAtt": ["MyLambdaRole", "Arn"] } } }, @@ -90,9 +78,7 @@ "Properties": { "AlarmName": "my-alarm", "AlarmDescription": "some alarm", - "AlarmActions": [ - "devnull@mapbox.com" - ], + "AlarmActions": ["devnull@mapbox.com"], "Period": 120, "EvaluationPeriods": 2, "DatapointsToAlarm": 1, @@ -101,9 +87,7 @@ "ComparisonOperator": "LessThanThreshold", "TreatMissingData": "breaching", "EvaluateLowSampleCountPercentile": "ignore", - "OKActions": [ - "devnull@mapbox.com" - ], + "OKActions": ["devnull@mapbox.com"], "Namespace": "AWS/Lambda", "Dimensions": [ { @@ -134,10 +118,7 @@ "Effect": "Allow", "Action": "logs:*", "Resource": { - "Fn::GetAtt": [ - "MyLambdaLogs", - "Arn" - ] + "Fn::GetAtt": ["MyLambdaLogs", "Arn"] } } ] @@ -180,4 +161,4 @@ } }, "Outputs": {} -} \ No newline at end of file +} diff --git a/test/shortcuts.test.js b/test/shortcuts.test.js index c54e7c5..96df8dc 100644 --- a/test/shortcuts.test.js +++ b/test/shortcuts.test.js @@ -190,7 +190,8 @@ test('[shortcuts] lambda', (assert) => { TreatMissingData: 'breaching', EvaluateLowSampleCountPercentile: 'ignore', ExtendedStatistics: 'p100', - OKActions: ['devnull@mapbox.com'] + OKActions: ['devnull@mapbox.com'], + LogRetentionInDays: 30 }); template = cf.merge(