【问题标题】:Creating a standard AWS Cloudwatch alarm using AWS cloudformation template使用 AWS cloudformation 模板创建标准 AWS Cloudwatch 警报
【发布时间】:2021-08-09 20:44:30
【问题描述】:

假设我正在为一家杂货店开发一个应用程序。我们都知道杂货店里有数百种杂货。现在,我的要求是使用 AWS Cloudformation 模板 (CFT) 创建一个 AWS Cloudwatch 警报。

之前假设我们的杂货店里只有大米和小麦,因此我们在 CFT 中创建了单独的警报资源。一个例子:

    {
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "AWS cloudwatch Grocery",
    "Parameters": {
        "Email": {
            "Type": "String",
            "Description": "Email address to notify when alarm is triggered",
            "Default": "email@email.com"
        }
    },
    "Resources": {
        "AlarmNotificationTopic": {
            "Type": "AWS::SNS::Topic",
            "Properties": {
                "Subscription": [
                    {
                        "Endpoint": {
                            "Ref": "Email"
                        },
                        "Protocol": "email"
                    }
                ]
            }
        },
        "RiceQuantityLowAlarm": {
            "Type": "AWS::CloudWatch::Alarm",
            "Properties": {
                "AlarmName": "RiceQuantityLowAlarm",
                "AlarmDescription": "Alarm which gets triggered when Rice quantity is low",
                "AlarmActions": [
                    {
                        "Ref": "AlarmNotificationTopicTest"
                    }
                ],
                "MetricName": "Quantity",
                "Namespace": "Grocery",
                "Dimensions": [
                    {
                        "Name": "Item",
                        "Value": "Rice"
                    }
                ],
                "ComparisonOperator": "LessThanOrEqualToThreshold",
                "EvaluationPeriods": "10",
                "Period": "360",
                "Statistic": "Sum",
                "Threshold": "1",
                "TreatMissingData": "notBreaching"
            }
        },
        "WheatQuantityLowAlarm": {
            "Type": "AWS::CloudWatch::Alarm",
            "Properties": {
                "AlarmName": "WheatQuantityLowAlarm",
                "AlarmDescription": "Alarm which gets triggered when Wheat quantity is low",
                "AlarmActions": [
                    {
                        "Ref": "AlarmNotificationTopicTest"
                    }
                ],
                "MetricName": "Quantity",
                "Namespace": "Grocery",
                "Dimensions": [
                    {
                        "Name": "Item",
                        "Value": "Wheat"
                    }
                ],
                "ComparisonOperator": "LessThanOrEqualToThreshold",
                "EvaluationPeriods": "10",
                "Period": "360",
                "Statistic": "Sum",
                "Threshold": "1",
                "TreatMissingData": "notBreaching"
            }
        }
    }
}

现在假设我想在我的杂货店中添加更多商品,并且我不想仅限于大米和小麦。在这种情况下,假设我想在我的杂货店中添加 5 件新商品。因此,如果我遵循上述方法,我将在 CFT 中创建 5 个新的单独的 cloudwatch 警报资源,并且每当有新项目出现时都会这样做。但我不想这样做,因为我很懒。

有什么方法可以标准化 CFT 资源?可以看到 CFT cloudwatch 告警资源上面只有名称(大米/小麦)的区别,其余都是通用的。

【问题讨论】:

  • 进展如何?仍然不清楚为什么你不能这样做?

标签: amazon-web-services amazon-cloudformation amazon-cloudwatch


【解决方案1】:

这对于纯 CloudFormation 来说是不可能的。模板是声明性的,您不能使用循环等代码结构来生成资源。以下列表概述了一些可以使模板更具动态性或能够重用代码的方法(无特定顺序):

  1. 使用nested stack 减少要管理的代码量
  2. 编写一个custom resource,接受项目列表并使用使用 SDK 的代码维护警报
  3. 使用第三方库(如SparkleFormation 或troposphere)使用脚本/编程语言生成模板
  4. 使用不同的 IaC 工具,例如 Terraform(允许一些类似编程的构造,并且比 CF 更灵活)或 AWS CDK(用多种语言编写实际代码,编译成 CloudFormation 模板)

这些都有各自的优缺点,而且都比ctrl/cmd+c、ctrl/cmd多得多的工作+v 所以在做决定时要记住这一点!

【讨论】:

    猜你喜欢
    • 2019-11-09
    • 1970-01-01
    • 2021-05-25
    • 2019-07-14
    • 1970-01-01
    • 2018-01-24
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多