Skip to content

Commit

Permalink
add option for lambda log retention period
Browse files Browse the repository at this point in the history
  • Loading branch information
ryndaniels committed Jul 29, 2024
1 parent 7d8096a commit 24e5bfa
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 32 deletions.
1 change: 1 addition & 0 deletions lib/shortcuts/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ Log Group, a Role, an Alarm on function errors, and the Lambda Function itself.
| [options.EvaluateLowSampleCountPercentile] | <code>String</code> | | See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html#cfn-cloudwatch-alarms-evaluatelowsamplecountpercentile). |
| [options.ExtendedStatistic] | <code>String</code> | | See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html#cfn-cloudwatch-alarms-extendedstatistic)] |
| [options.OKActions] | <code>Array.&lt;String&gt;</code> | | See [AWS documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html#cfn-cloudwatch-alarms-okactions). |
| [options.LogRetentionInDays] | <code>Number</code> | <code>14</code> | 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
Expand Down
6 changes: 4 additions & 2 deletions lib/shortcuts/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>} [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');
Expand Down Expand Up @@ -104,7 +105,8 @@ class Lambda {
TreatMissingData = 'notBreaching',
EvaluateLowSampleCountPercentile,
ExtendedStatistic,
OKActions
OKActions,
LogRetentionInDays = 14
} = options;

if (options.EvaluationPeriods < Math.ceil(Timeout / Period))
Expand Down Expand Up @@ -138,7 +140,7 @@ class Lambda {
LogGroupName: {
'Fn::Sub': ['/aws/lambda/${name}', { name: FunctionName }]
},
RetentionInDays: 14
RetentionInDays: LogRetentionInDays
}
},

Expand Down
39 changes: 10 additions & 29 deletions test/fixtures/shortcuts/lambda-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
"Mappings": {},
"Conditions": {
"Always": {
"Fn::Equals": [
"1",
"1"
]
"Fn::Equals": ["1", "1"]
}
},
"Resources": {
Expand All @@ -28,7 +25,7 @@
}
]
},
"RetentionInDays": 14
"RetentionInDays": 30
}
},
"MyLambda": {
Expand All @@ -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",
Expand All @@ -63,12 +58,8 @@
"Mode": "Active"
},
"VpcConfig": {
"SecurityGroupIds": [
"sg-12345"
],
"SubnetIds": [
"fake"
]
"SecurityGroupIds": ["sg-12345"],
"SubnetIds": ["fake"]
},
"Tags": [
{
Expand All @@ -77,10 +68,7 @@
}
],
"Role": {
"Fn::GetAtt": [
"MyLambdaRole",
"Arn"
]
"Fn::GetAtt": ["MyLambdaRole", "Arn"]
}
}
},
Expand All @@ -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,
Expand All @@ -101,9 +87,7 @@
"ComparisonOperator": "LessThanThreshold",
"TreatMissingData": "breaching",
"EvaluateLowSampleCountPercentile": "ignore",
"OKActions": [
"devnull@mapbox.com"
],
"OKActions": ["devnull@mapbox.com"],
"Namespace": "AWS/Lambda",
"Dimensions": [
{
Expand Down Expand Up @@ -134,10 +118,7 @@
"Effect": "Allow",
"Action": "logs:*",
"Resource": {
"Fn::GetAtt": [
"MyLambdaLogs",
"Arn"
]
"Fn::GetAtt": ["MyLambdaLogs", "Arn"]
}
}
]
Expand Down Expand Up @@ -180,4 +161,4 @@
}
},
"Outputs": {}
}
}
3 changes: 2 additions & 1 deletion test/shortcuts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 24e5bfa

Please sign in to comment.