Skip to content

Commit c4edfc5

Browse files
authored
Set up alarm for exceptions in task runner (#18)
* #5 Create metric and alarm to track exception for TaskRunner * #5 Fix template for custom metric and alarm * #5 create LogGroup to use for custom metric * #5 Update command to create bucket in same region as deployed * #5 Update tests
1 parent b3a7b81 commit c4edfc5

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

lambda_cron/cli/cli_tool.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ def bucket_exists(self):
257257
command_result = self.exec_aws_command(check_bucket_command)
258258
return command_result == 0
259259

260+
def get_s3_bucket_uri(self):
261+
return "s3://{bucket_name}".format(bucket_name=self.config.bucket)
262+
260263
def check_bucket(self):
261264
bucket_exists = self.bucket_exists()
262265
if self.cli.create_bucket:
@@ -265,7 +268,7 @@ def check_bucket(self):
265268
exit(1)
266269
else:
267270
print "Creating bucket '{}'".format(self.config.bucket)
268-
create_bucket_command = ["aws", "s3api", "create-bucket", "--bucket", self.config.bucket]
271+
create_bucket_command = ["aws", "s3", "mb", self.get_s3_bucket_uri()]
269272
self.exec_aws_command(create_bucket_command)
270273
else:
271274
if not bucket_exists:
@@ -298,7 +301,7 @@ def delete(self):
298301
self.run_aws_cloudformation_wait_command('delete')
299302

300303
if self.cli.delete_bucket:
301-
delete_bucket_command = ["aws", "s3api", "delete-bucket", "--bucket", self.config.bucket]
304+
delete_bucket_command = ["aws", "s3", "rb", self.get_s3_bucket_uri()]
302305
self.exec_aws_command(delete_bucket_command)
303306

304307
def invoke(self):

lambda_cron/template.cfn.yml

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Resources:
111111
SourceArn: !GetAtt LambdaCronHourlyEvent.Arn
112112

113113
LambdaCronSNSTopic:
114-
Type: "AWS::SNS::Topic"
114+
Type: AWS::SNS::Topic
115115
Condition: IsAlarmActive
116116
Properties:
117117
Subscription:
@@ -120,8 +120,14 @@ Resources:
120120
Protocol: "email"
121121
TopicName: !Sub LambdaCron-${Environment}
122122

123-
LambdaCronAalarm:
124-
Type: "AWS::CloudWatch::Alarm"
123+
LambdaCronLogGroup:
124+
Type: AWS::Logs::LogGroup
125+
Properties:
126+
LogGroupName: !Sub /aws/lambda/LambdaCron-${Environment}
127+
RetentionInDays: 90
128+
129+
LambdaCronAlarm:
130+
Type: AWS::CloudWatch::Alarm
125131
Condition: IsAlarmActive
126132
Properties:
127133
Namespace: AWS/Lambda
@@ -144,6 +150,32 @@ Resources:
144150
AlarmDescription: !Sub Errors in LambdaCron ${Environment}
145151
ActionsEnabled: True
146152

153+
LambdaCronErrorsMetric:
154+
Type: AWS::Logs::MetricFilter
155+
Properties:
156+
LogGroupName: !Ref LambdaCronLogGroup
157+
FilterPattern: "ERROR"
158+
MetricTransformations:
159+
- MetricValue: '1'
160+
MetricNamespace: !Sub LambdaCron-${Environment}/Errors
161+
MetricName: ErrorsCount
162+
163+
LambdaCronErrorsAlarm:
164+
Type: AWS::CloudWatch::Alarm
165+
Condition: IsAlarmActive
166+
Properties:
167+
Namespace: !Sub LambdaCron-${Environment}/Errors
168+
MetricName: ErrorsCount
169+
ComparisonOperator: GreaterThanOrEqualToThreshold
170+
Threshold: 1
171+
Statistic: Sum
172+
Period: !Ref AlarmPeriod
173+
EvaluationPeriods: '1'
174+
AlarmActions:
175+
- Ref: LambdaCronSNSTopic
176+
AlarmDescription: !Sub Errors running tasks for LambdaCron ${Environment}
177+
ActionsEnabled: True
178+
147179
Outputs:
148180
LambdaCronFunction:
149181
Value: !Ref LambdaCronFunction

tests/test_cli_tool.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,9 @@ def test_delete_command_with_delete_bucket(monkeypatch):
184184
assert '--profile' not in lambda_cron.commands_list[0]
185185
assert 'stack-delete-complete' in lambda_cron.commands_list[1]
186186
assert '--profile' not in lambda_cron.commands_list[1]
187-
assert 's3api' in lambda_cron.commands_list[2]
188-
assert 'delete-bucket' in lambda_cron.commands_list[2]
189-
assert '--bucket' in lambda_cron.commands_list[2]
190-
assert 'test-bucket-custom' in lambda_cron.commands_list[2]
187+
assert 's3' in lambda_cron.commands_list[2]
188+
assert 'rb' in lambda_cron.commands_list[2]
189+
assert 's3://test-bucket-custom' in lambda_cron.commands_list[2]
191190

192191

193192
def test_add_profile_to_delete_commands(monkeypatch):
@@ -472,10 +471,9 @@ def test_check_bucket_need_create_bucket(bucket_exists_mock, monkeypatch):
472471

473472
assert len(lambda_cron.commands_list) == 1
474473
assert "aws" in lambda_cron.commands_list[0]
475-
assert "s3api" in lambda_cron.commands_list[0]
476-
assert "create-bucket" in lambda_cron.commands_list[0]
477-
assert "--bucket" in lambda_cron.commands_list[0]
478-
assert "test-bucket-custom" in lambda_cron.commands_list[0]
474+
assert "s3" in lambda_cron.commands_list[0]
475+
assert "mb" in lambda_cron.commands_list[0]
476+
assert "s3://test-bucket-custom" in lambda_cron.commands_list[0]
479477

480478

481479
@patch.object(LambdaCronCLISpy, 'bucket_exists')

0 commit comments

Comments
 (0)