Skip to content

Commit 90486a6

Browse files
authored
Add support for Lambda logging config (#19)
* Add support for Lambda logging config * Update version
1 parent 3f98a1c commit 90486a6

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed

galaxy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace: mattclay
99
name: aws
1010

1111
# The version of the collection. Must be compatible with semantic versioning
12-
version: 4.0.1
12+
version: 4.1.0
1313

1414
# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
1515
readme: README.md

plugins/modules/lambda.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,45 @@
106106
- A list of layers used by the Lambda function.
107107
type: list
108108
elements: str
109+
log_format:
110+
description:
111+
- The format of the Lambda function logs sent to CloudWatch.
112+
type: str
113+
default: Text
114+
choices:
115+
- JSON
116+
- Text
117+
log_group:
118+
description:
119+
- The CloudWatch log group to send the Lambda function logs to.
120+
- If not set, the default log group named after the function will be used.
121+
type: str
122+
application_log_level:
123+
description:
124+
- The log level filter for Lambda function log messages sent to CloudWatch.
125+
- Only messages at the selected level and lower are logged, with C(fatal) being the lowest level.
126+
- Ignored when log_format is not JSON.
127+
type: str
128+
default: info
129+
choices:
130+
- trace
131+
- debug
132+
- info
133+
- warn
134+
- error
135+
- fatal
136+
system_log_level:
137+
description:
138+
- The log level filter for Lambda system log messages sent to CloudWatch.
139+
- Only messages at the selected level and lower are logged, with C(warn) being the lowest level.
140+
- Ignored when log_format is not JSON.
141+
type: str
142+
default: info
143+
choices:
144+
- debug
145+
- info
146+
- warn
147+
109148
extends_documentation_fragment:
110149
- amazon.aws.common.modules
111150
- amazon.aws.region.modules
@@ -176,6 +215,10 @@ def main():
176215
preserve_environment=dict(required=False, default=False, type='bool'),
177216
environment=dict(required=False, default=None, type='dict'),
178217
layers=dict(required=False, default=None, type='list', elements='str'),
218+
log_format=dict(required=False, default='Text', type='str', choices=['JSON', 'Text']),
219+
log_group=dict(required=False, default=None, type='str'),
220+
application_log_level=dict(required=False, default='info', type='str', choices=['trace', 'debug', 'info', 'warn', 'error', 'fatal']),
221+
system_log_level=dict(required=False, default='info', type='str', choices=['debug', 'info', 'warn']),
179222
))
180223

181224
module = AnsibleModule(
@@ -445,8 +488,16 @@ def make_result(self, data):
445488
memory_size=data['MemorySize'],
446489
environment=(data.get('Environment') or {}).get('Variables') or {},
447490
layers=data.get('Layers'),
491+
log_format=data['LoggingConfig']['LogFormat'],
492+
log_group=data['LoggingConfig']['LogGroup'],
448493
)
449494

495+
if data['LoggingConfig']['LogFormat'] == 'JSON':
496+
result.update(
497+
application_log_level=data['LoggingConfig']['ApplicationLogLevel'].lower(),
498+
system_log_level=data['LoggingConfig']['SystemLogLevel'].lower(),
499+
)
500+
450501
if 'CodeSha256' in data:
451502
more = dict(
452503
function_arn=data['FunctionArn'],
@@ -472,9 +523,19 @@ def make_function_configuration(self):
472523
'MemorySize': self.params['memory_size'],
473524
'Environment': None,
474525
'Layers': self.params['layers'],
526+
'LoggingConfig': {
527+
'LogFormat': self.params['log_format'],
528+
'LogGroup': self.params['log_group'] or f'/aws/lambda/{self.params["function_name"]}',
529+
},
475530
# VpcConfig not implemented
476531
}
477532

533+
if self.params['log_format'] == 'JSON':
534+
args['LoggingConfig'].update({
535+
'ApplicationLogLevel': self.params['application_log_level'].upper(),
536+
'SystemLogLevel': self.params['system_log_level'].upper(),
537+
})
538+
478539
if self.params['environment']:
479540
args['Environment'] = {
480541
'Variables': self.params['environment'],

tests/integration/targets/lambda/tasks/tests.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,91 @@
7878
qualifier: "test"
7979
register: update_unchanged
8080

81+
- name: Update Lambda function to use custom logging (check)
82+
lambda:
83+
name: "{{ function_name }}"
84+
code: "# updated code"
85+
runtime: python3.8
86+
timeout: 60
87+
handler: lambda_function.lambda_handler
88+
memory_size: 128
89+
role: "{{ iam_role_arn }}"
90+
publish: yes
91+
qualifier: "test"
92+
log_format: JSON
93+
log_group: "/aws/lambda/{{ function_name }}-custom"
94+
application_log_level: debug
95+
system_log_level: warn
96+
check_mode: yes
97+
register: logging_check
98+
99+
- name: Update Lambda function to use custom logging (changed)
100+
lambda:
101+
name: "{{ function_name }}"
102+
code: "# no actual code"
103+
runtime: python3.8
104+
timeout: 60
105+
handler: lambda_function.lambda_handler
106+
memory_size: 128
107+
role: "{{ iam_role_arn }}"
108+
publish: yes
109+
qualifier: "test"
110+
log_format: JSON
111+
log_group: "/aws/lambda/{{ function_name }}-custom"
112+
application_log_level: debug
113+
system_log_level: warn
114+
register: logging_changed
115+
116+
- name: Update Lambda function to use custom logging (unchanged)
117+
lambda:
118+
name: "{{ function_name }}"
119+
code: "# no actual code"
120+
runtime: python3.8
121+
timeout: 60
122+
handler: lambda_function.lambda_handler
123+
memory_size: 128
124+
role: "{{ iam_role_arn }}"
125+
publish: no # publishing would force an update and thus report changed
126+
qualifier: "test"
127+
log_format: JSON
128+
log_group: "/aws/lambda/{{ function_name }}-custom"
129+
application_log_level: debug
130+
system_log_level: warn
131+
register: logging_unchanged
132+
81133
- name: Check results
82134
assert:
83135
that:
84136
- create_check is changed
137+
- create_check.meta.log_format == 'Text'
138+
- create_check.meta.log_group == ("/aws/lambda/" + function_name)
85139
- create_changed is changed
140+
- create_changed.meta.log_format == 'Text'
141+
- create_changed.meta.log_group == ("/aws/lambda/" + function_name)
86142
- create_unchanged is not changed
143+
- create_unchanged.meta.log_format == 'Text'
144+
- create_unchanged.meta.log_group == ("/aws/lambda/" + function_name)
87145
- update_check is changed
146+
- update_check.meta.log_format == 'Text'
147+
- update_check.meta.log_group == ("/aws/lambda/" + function_name)
88148
- update_changed is changed
149+
- update_changed.meta.log_format == 'Text'
150+
- update_changed.meta.log_group == ("/aws/lambda/" + function_name)
89151
- update_unchanged is not changed
152+
- update_unchanged.meta.log_format == 'Text'
153+
- update_unchanged.meta.log_group == ("/aws/lambda/" + function_name)
154+
- logging_check is changed
155+
- logging_check.meta.log_format == 'JSON'
156+
- logging_check.meta.application_log_level == 'debug'
157+
- logging_check.meta.system_log_level == 'warn'
158+
- logging_check.meta.log_group == ("/aws/lambda/" + function_name + "-custom")
159+
- logging_changed is changed
160+
- logging_changed.meta.log_format == 'JSON'
161+
- logging_changed.meta.application_log_level == 'debug'
162+
- logging_changed.meta.system_log_level == 'warn'
163+
- logging_changed.meta.log_group == ("/aws/lambda/" + function_name + "-custom")
164+
- logging_unchanged is not changed
165+
- logging_unchanged.meta.log_format == 'JSON'
166+
- logging_unchanged.meta.application_log_level == 'debug'
167+
- logging_unchanged.meta.system_log_level == 'warn'
168+
- logging_unchanged.meta.log_group == ("/aws/lambda/" + function_name + "-custom")

0 commit comments

Comments
 (0)