-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from base2Services/feature/alarm-tags
feature/alarm tags
- Loading branch information
Showing
7 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Guardian Alarm Tags | ||
|
||
AWS tags can be applied to Cloudwatch alarms created by guardian. This is available as a separate guardian command [`cfn-guardian tag-alarms`] because Cloudformation doesn't support creating tags on Cloudwatch alarms. | ||
|
||
## Default Tags | ||
|
||
Guardian will add the following default tags to each alarm | ||
|
||
``` | ||
guardian:resource:id | ||
guardian:resource:group | ||
guardian:alarm:name | ||
guardian:alarm:metric | ||
guardian:alarm:severity | ||
``` | ||
|
||
## Adding Tags | ||
|
||
Additional tags can added through the alarms yaml configuration file. They can be applied globally to all alarms, to all alarms in a resource group or a specific alarm. | ||
|
||
### Global Tags | ||
|
||
Global tags are applied to every alarm created by guardian. Add the `GlobalTags` key at the top level of the alarms yaml config with key:value pairs. | ||
|
||
```yml | ||
GlobalTags: | ||
key: value | ||
env: production | ||
``` | ||
### Resource Group Tags | ||
Resource group tags are applied to every alarm in a guardian resource group using the `Templates` section to add the tags. | ||
|
||
```yaml | ||
Templates: | ||
Ec2Instance: | ||
GroupOverrides: | ||
Tags: | ||
key: value | ||
env: production | ||
``` | ||
|
||
### Specific Alarm Tags | ||
|
||
To add tags to a specific guardian alarm you can apply the tags in the `Templates` section of the alarms yaml config. | ||
|
||
```yaml | ||
Templates: | ||
Ec2Instance: | ||
CPUUtilizationHigh: | ||
Tags: | ||
key: value | ||
alarm-action: restart ec2 instance | ||
``` | ||
|
||
## Applying tags | ||
|
||
To apply the tags run the `tag-alarms` command passing the alarms yaml config. | ||
|
||
```sh | ||
cfn-guardian tag-alarms --config alarms.yaml | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'aws-sdk-cloudwatch' | ||
require 'cfnguardian/cloudwatch' | ||
require 'cfnguardian/log' | ||
|
||
module CfnGuardian | ||
class Tagger | ||
include Logging | ||
|
||
def initialize() | ||
@client = Aws::CloudWatch::Client.new(max_attempts: 5) | ||
end | ||
|
||
def tag_alarm(alarm, global_tags={}) | ||
alarm_arn = CfnGuardian::CloudWatch.get_alarm_arn(alarm) | ||
|
||
new_tags = get_tags(alarm, global_tags) | ||
current_tags = get_alarm_tags(alarm_arn) | ||
tags_to_delete = get_tags_to_delete(current_tags, new_tags) | ||
|
||
if tags_to_delete.any? | ||
logger.debug "Removing tags #{tags_to_delete} from alarm #{alarm_arn}" | ||
@client.untag_resource({ | ||
resource_arn: alarm_arn, | ||
tag_keys: tags_to_delete | ||
}) | ||
end | ||
|
||
if tags_changed?(current_tags, new_tags) | ||
logger.debug "Updating tags on alarm #{alarm_arn}" | ||
@client.tag_resource({ | ||
resource_arn: alarm_arn, | ||
tags: new_tags.map {|key,value| {key: key, value: value}} | ||
}) | ||
end | ||
end | ||
|
||
def get_tags(alarm, global_tags) | ||
defaults = { | ||
'guardian:resource:id' => alarm.resource_id, | ||
'guardian:resource:group' => alarm.group, | ||
'guardian:alarm:name' => alarm.name, | ||
'guardian:alarm:metric' => alarm.metric_name, | ||
'guardian:alarm:severity' => alarm.alarm_action | ||
} | ||
tags = global_tags.merge(defaults) | ||
return alarm.tags.merge(tags) | ||
end | ||
|
||
def get_alarm_tags(alarm_arn) | ||
resp = @client.list_tags_for_resource({ | ||
resource_arn: alarm_arn | ||
}) | ||
return resp.tags | ||
end | ||
|
||
def get_tags_to_delete(current_tags, new_tags) | ||
return current_tags.select {|tag| !new_tags.has_key?(tag.key)}.map {|tag| tag.key} | ||
end | ||
|
||
def tags_changed?(current_tags, new_tags) | ||
return tags_to_hash(current_tags) != new_tags | ||
end | ||
|
||
def tags_to_hash(tags) | ||
return tags.map {|tag| {tag.key => tag.value} }.reduce(Hash.new, :merge) | ||
end | ||
|
||
end | ||
end |