Skip to content

Set up alarm for exceptions in task runner #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lambda_cron/cli/cli_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ def bucket_exists(self):
command_result = self.exec_aws_command(check_bucket_command)
return command_result == 0

def get_s3_bucket_uri(self):
return "s3://{bucket_name}".format(bucket_name=self.config.bucket)

def check_bucket(self):
bucket_exists = self.bucket_exists()
if self.cli.create_bucket:
Expand All @@ -265,7 +268,7 @@ def check_bucket(self):
exit(1)
else:
print "Creating bucket '{}'".format(self.config.bucket)
create_bucket_command = ["aws", "s3api", "create-bucket", "--bucket", self.config.bucket]
create_bucket_command = ["aws", "s3", "mb", self.get_s3_bucket_uri()]
self.exec_aws_command(create_bucket_command)
else:
if not bucket_exists:
Expand Down Expand Up @@ -298,7 +301,7 @@ def delete(self):
self.run_aws_cloudformation_wait_command('delete')

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

def invoke(self):
Expand Down
38 changes: 35 additions & 3 deletions lambda_cron/template.cfn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Resources:
SourceArn: !GetAtt LambdaCronHourlyEvent.Arn

LambdaCronSNSTopic:
Type: "AWS::SNS::Topic"
Type: AWS::SNS::Topic
Condition: IsAlarmActive
Properties:
Subscription:
Expand All @@ -120,8 +120,14 @@ Resources:
Protocol: "email"
TopicName: !Sub LambdaCron-${Environment}

LambdaCronAalarm:
Type: "AWS::CloudWatch::Alarm"
LambdaCronLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/LambdaCron-${Environment}
RetentionInDays: 90

LambdaCronAlarm:
Type: AWS::CloudWatch::Alarm
Condition: IsAlarmActive
Properties:
Namespace: AWS/Lambda
Expand All @@ -144,6 +150,32 @@ Resources:
AlarmDescription: !Sub Errors in LambdaCron ${Environment}
ActionsEnabled: True

LambdaCronErrorsMetric:
Type: AWS::Logs::MetricFilter
Properties:
LogGroupName: !Ref LambdaCronLogGroup
FilterPattern: "ERROR"
MetricTransformations:
- MetricValue: '1'
MetricNamespace: !Sub LambdaCron-${Environment}/Errors
MetricName: ErrorsCount

LambdaCronErrorsAlarm:
Type: AWS::CloudWatch::Alarm
Condition: IsAlarmActive
Properties:
Namespace: !Sub LambdaCron-${Environment}/Errors
MetricName: ErrorsCount
ComparisonOperator: GreaterThanOrEqualToThreshold
Threshold: 1
Statistic: Sum
Period: !Ref AlarmPeriod
EvaluationPeriods: '1'
AlarmActions:
- Ref: LambdaCronSNSTopic
AlarmDescription: !Sub Errors running tasks for LambdaCron ${Environment}
ActionsEnabled: True

Outputs:
LambdaCronFunction:
Value: !Ref LambdaCronFunction
Expand Down
14 changes: 6 additions & 8 deletions tests/test_cli_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,9 @@ def test_delete_command_with_delete_bucket(monkeypatch):
assert '--profile' not in lambda_cron.commands_list[0]
assert 'stack-delete-complete' in lambda_cron.commands_list[1]
assert '--profile' not in lambda_cron.commands_list[1]
assert 's3api' in lambda_cron.commands_list[2]
assert 'delete-bucket' in lambda_cron.commands_list[2]
assert '--bucket' in lambda_cron.commands_list[2]
assert 'test-bucket-custom' in lambda_cron.commands_list[2]
assert 's3' in lambda_cron.commands_list[2]
assert 'rb' in lambda_cron.commands_list[2]
assert 's3://test-bucket-custom' in lambda_cron.commands_list[2]


def test_add_profile_to_delete_commands(monkeypatch):
Expand Down Expand Up @@ -472,10 +471,9 @@ def test_check_bucket_need_create_bucket(bucket_exists_mock, monkeypatch):

assert len(lambda_cron.commands_list) == 1
assert "aws" in lambda_cron.commands_list[0]
assert "s3api" in lambda_cron.commands_list[0]
assert "create-bucket" in lambda_cron.commands_list[0]
assert "--bucket" in lambda_cron.commands_list[0]
assert "test-bucket-custom" in lambda_cron.commands_list[0]
assert "s3" in lambda_cron.commands_list[0]
assert "mb" in lambda_cron.commands_list[0]
assert "s3://test-bucket-custom" in lambda_cron.commands_list[0]


@patch.object(LambdaCronCLISpy, 'bucket_exists')
Expand Down