cloudformation-coverage-roadmap icon indicating copy to clipboard operation
cloudformation-coverage-roadmap copied to clipboard

AWS::CloudWatch::Alarm - allow tags

Open Limess opened this issue 6 years ago • 79 comments

1. Title

CloudWatch supports tagging and untagging alarms, and it should be supported in CloudFormation.

2. Scope of request

Allow for tags on create, updating tags, etc.

3. Expected behavior

See 2 above.

4. Test case recommendation

  • Adding tags on create of CloudWatch alarm.
  • Addings tags on update of CloudWach alarm.
  • Removing tags on update of CloudWatch Alarm.

5. Links to existing API doc

API Docs: https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_TagResource.html

In CloudWatch, alarms can be tagged.

6. Category tag

Management

7. Any additional context

The documentation on this feature is sparse.

Existing resource: AWS::CloudWatch::Alarm.

This is supported by Boto: CloudWatch.Client.tag_resource.

Limess avatar Jul 31 '19 21:07 Limess

@luiseduardocolon It has been sitting in status "We're working on it" since Jan 11th. Any update for this? My finance dept. is killing me for not being able to filter on tags for our alarms.

tvb avatar Jun 17 '20 14:06 tvb

Also hoping this gets implemented soon. We want to set incident priority and urgency based on CloudWatch alarm tags, and not have to configure these separately in another system.

JanneKataja-TomTom avatar Jul 08 '20 12:07 JanneKataja-TomTom

@luiseduardocolon any updates on this ?

qmg-drettie avatar Aug 28 '20 09:08 qmg-drettie

Also hoping this gets implemented soon. We want to set incident priority and urgency based on CloudWatch alarm tags, and not have to configure these separately in another system.

THIS. It would be so much easier to tag alarms and then create a CloudWatch event rule which forwards alarm state changes to a Lambda which checks the tags and forwards messages.. Thats my plan once tagging is available

thomasklinger1234 avatar Sep 24 '20 12:09 thomasklinger1234

Argh. Just ran into this one.

alexbarnes avatar Nov 04 '20 22:11 alexbarnes

Adding my plus-one here. We have so many teams and applications we don't know who to contact about cleaning up their alarms that point to resources that may no longer exist.

djfurman avatar Dec 23 '20 14:12 djfurman

+1 as well - this would be huge for us to be able to fully utilize tags for automating dashboard creation.

butangero avatar Feb 11 '21 17:02 butangero

+1 We need cloudwatch alarm tags so we can allow people to tag their alarms with information that's needed for our shared lambda functions.

lisakester avatar Mar 04 '21 21:03 lisakester

Hoping to implement the following in one go: https://aws.amazon.com/about-aws/whats-new/2019/04/you-can-now-use-resource-level-permissions-for-amazon-cloudwatch/ would be nice to deploy tagged cloudwatch alarms via CFN alongside the policy that applies to them.

ptdel avatar Apr 15 '21 02:04 ptdel

using description for tags. so desperate we are. no patience we have.

jenshoffmann1331 avatar May 21 '21 05:05 jenshoffmann1331

Just ran into this one as well :( any ETAs?

tkrajca avatar Jun 16 '21 02:06 tkrajca

If anyone needs a solution to this before it's supported natively, this is the custom resource Lambda that I'm using:

const response = require('cfn-response');
const AWS = require('aws-sdk');
const cloudwatch = new AWS.CloudWatch();

exports.handler = async (event, context) => {
  console.log(event);

  try {
    const tags = event.ResourceProperties.Tags;
    const arn = event.ResourceProperties.AlarmArn;

    if (!tags || !tags.length || !arn) {
      console.error('AlarmArn and Tags properties must be defined');
      await response.send(event, context, response.FAILED, {});
    }

    if (event.RequestType === 'Create') {
      // Create all tags on the custom resource
      await cloudwatch.tagResource({
        ResourceARN: arn,
        Tags: tags,
      }).promise();
    } else if (event.RequestType === 'Update') {
      // Remove tags that were present in the old resource properties, but are
      // no longer present
      const previousTags = event.OldResourceProperties.Tags;
      const currentTagKeys = tags.map(t => t.Key);

      const staleTags = previousTags.filter(p => !currentTagKeys.includes(p.Key));

      if (staleTags.length) {
          await cloudwatch.untagResource({
            ResourceARN: arn,
            TagKeys: staleTags.map(t => t.Key),
          }).promise();
      }

      // Create/update all values present in the current resource properties
      await cloudwatch.tagResource({
        ResourceARN: arn,
        Tags: tags,
      }).promise();
    } else if (event.RequestType === 'Delete') {
      // Remove all tags on the custom resource
      await cloudwatch.untagResource({
        ResourceARN: arn,
        TagKeys: tags.map(t => t.Key),
      }).promise();
    }

    await response.send(event, context, response.SUCCESS, {});
  } catch (error) {
    console.error(error);
    await response.send(event, context, response.FAILED, {});
  }
};

Which would work with a custom resource like this:

  MyAlarmTags:
    Type: Custom::CloudWatchAlarmTags
    Properties:
      ServiceToken: !Ref CloudWatchAlarmTaggerServiceToken
      AlarmArn: !GetAtt MyAlarm.Arn
      Tags:
        - { Key: Severity, Value: Critical }
        - { Key: LogGroup, Value: arn:aws:logs:us-east-1:1234567890:log-group:my-service-logs }

farski avatar Jul 19 '21 18:07 farski

Is there any news about this? please.

venkiit58 avatar Aug 24 '21 10:08 venkiit58

+1

MrSakhs avatar Sep 07 '21 13:09 MrSakhs

+1

qguang avatar Sep 28 '21 11:09 qguang

+1 from 2021!

ytsipun avatar Oct 01 '21 11:10 ytsipun

+1

jrombi avatar Oct 01 '21 12:10 jrombi

I'm impressed that this feature doesn't have any avances 2 years after. We need tagging for create a team-based billing strategy and CW is one of our better consumers...

abejaranog avatar Oct 07 '21 12:10 abejaranog

Covered this in CDK:

class AlarmTags extends cdk.Construct {
...
  const sdkCall: customResource.AwsSdkCall = {
    service: "CloudWatch",
    action: "tagResource",
    physicalResourceId: customResource.PhysicalResourceId.of(`${alarmName}Tags`),
    parameters: {
      ResourceARN: alarmArn,
      Tags: tags
    }
  }
  new customResource.AwsCustomResource(this, id, {
    resourceType: "Custom::AlarmTags",
    onCreate: sdkCall,
    onUpdate: sdkCall,
    logRetention: logs.RetentionDays.THREE_DAYS,
    policy: customResource.AwsCustomResourcePolicy.fromStatements([new iam.PolicyStatement({
      actions: ["cloudwatch:TagResource"],
      resources: ["*"]
    })]),
    timeout: cdk.Duration.minutes(1)
  })
...

Synth results in:

  1. Lambda function which performs the SDK call.
  2. Logs-related Lambda function.
  3. Custom::AlarmTags resource holding the SDK call parameters you provide and referencing the SDK call Lambda in Create and Update properties.

🚀 Lambdas aren't populated when instantiating several AlarmTags in a stack.

ytsipun avatar Oct 07 '21 12:10 ytsipun

For anyone trying to implement this on their own like we did, be warned that the rate limit on CloudWatch tagging is 1 per second, so it's extremely easy to hit when tagging a lot of resources at once via CloudFormation, such as when spinning up a new stack.

We set a high max retries and default backoff on the CloudWatch client, and give the function a bunch of time to run, basically just to let it fail a bunch and sort itself out.

farski avatar Oct 13 '21 21:10 farski

+1 from 2022

phene avatar Jan 05 '22 16:01 phene

+1

pchaganti avatar Mar 09 '22 00:03 pchaganti

+1

ryanzhu2017 avatar Mar 23 '22 22:03 ryanzhu2017

+1 from April 2022

djfurman avatar Apr 20 '22 00:04 djfurman

+1....

Risae avatar Jun 16 '22 16:06 Risae

+1

dougallaninthecloud avatar Jun 22 '22 01:06 dougallaninthecloud

+1

gmahe avatar Jul 05 '22 16:07 gmahe

+1 from 2022

JamesKyburz avatar Jul 14 '22 13:07 JamesKyburz

+1 from August 2022. Come on guys, this is basic stuff. This issue has been open for 3+ years.

towerbe avatar Aug 18 '22 11:08 towerbe

+1

teague-mfa avatar Sep 29 '22 17:09 teague-mfa