使用 AWS cloudformation 模板创建标准 AWS Cloudwatch 警报
Creating a standard AWS Cloudwatch alarm using AWS cloudformation template
假设我正在为一家杂货店开发应用程序。我们都知道杂货店里有数百种杂货。现在,我的要求是使用 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 警报资源中只有上面的名称不同(rice/wheat),其余的都是两者之间的共同点。
这对于纯 CloudFormation 来说是不可能的。模板是声明性的,您不能使用循环等代码结构来生成资源。以下列表概述了一些方法(排名不分先后),您可以使模板更加动态或能够重用代码:
- 使用 nested stack 减少要管理的代码量
- 编写一个 custom resource 接受项目列表并使用使用 sdk
的代码维护警报
- 使用 SparkleFormation or troposphere
等第三方库以 scripting/programming 语言生成模板
- 使用不同的 IaC 工具,例如
Terraform (allows some programming-like constructs and more flexible than CF) or the AWS CDK(用各种语言编写实际代码,编译成 CloudFormation 模板)
每一个都有自己的优点和缺点,并且都比 ctrl/cmd+c 涉及更多的工作, ctrl/cmd+v所以做决定的时候要记住这一点!
假设我正在为一家杂货店开发应用程序。我们都知道杂货店里有数百种杂货。现在,我的要求是使用 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 警报资源中只有上面的名称不同(rice/wheat),其余的都是两者之间的共同点。
这对于纯 CloudFormation 来说是不可能的。模板是声明性的,您不能使用循环等代码结构来生成资源。以下列表概述了一些方法(排名不分先后),您可以使模板更加动态或能够重用代码:
- 使用 nested stack 减少要管理的代码量
- 编写一个 custom resource 接受项目列表并使用使用 sdk 的代码维护警报
- 使用 SparkleFormation or troposphere 等第三方库以 scripting/programming 语言生成模板
- 使用不同的 IaC 工具,例如 Terraform (allows some programming-like constructs and more flexible than CF) or the AWS CDK(用各种语言编写实际代码,编译成 CloudFormation 模板)
每一个都有自己的优点和缺点,并且都比 ctrl/cmd+c 涉及更多的工作, ctrl/cmd+v所以做决定的时候要记住这一点!