Skip to content

Commit

Permalink
Set Lambda function log retention (#6070)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsotirho-ucsc committed Feb 27, 2025
1 parent 79148c3 commit 876615f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
15 changes: 14 additions & 1 deletion scripts/import_cloudwatch_log_groups.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

import boto3

from azul import (
Expand Down Expand Up @@ -28,7 +30,18 @@ def add_prefix(name):
log_group = '/aws/chatbot/' + config.qualified_resource_name('chatbot')
log_groups[name] = log_group
else:
pass
# Lambda function log groups
tf_path = f'{config.project_root}/terraform/api_gateway.tf.json'
with open(tf_path, 'r') as f:
tf_json = json.load(f)
for d1 in tf_json['resource']:
for k1, v1 in d1.items():
if k1 == 'aws_lambda_function':
for d2 in v1:
for k2, v2 in d2.items():
name = add_prefix(k2 + '_lambda')
log_group = '/aws/lambda/' + v2['function_name']
log_groups[name] = log_group

log_client = boto3.client('logs')
paginator = log_client.get_paginator('describe_log_groups')
Expand Down
1 change: 1 addition & 0 deletions terraform/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ rename_resources: validate

.PHONY: import_resources
import_resources: rename_resources
python $(project_root)/scripts/import_cloudwatch_log_groups.py

.PHONY: plan
plan: import_resources
Expand Down
7 changes: 7 additions & 0 deletions terraform/api_gateway.tf.json.template.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,13 @@ def for_domain(cls, domain):
app.name: {
'name': '/aws/apigateway/' + config.qualified_resource_name(app.name),
'retention_in_days': config.audit_log_retention_days,
},
**{
f'{resource_name}_lambda': {
'name': f'/aws/lambda/{resource['function_name']}',
'retention_in_days': config.audit_log_retention_days
} for resource_name, resource in
chalice.tf_config(app.name)['resource']['aws_lambda_function'].items()
}
},
'aws_iam_role': {
Expand Down

0 comments on commit 876615f

Please sign in to comment.