使用 CloudFormation 模板创建和配置 CloudWatch Application Insights 监控 - Amazon CloudWatch
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

使用 CloudFormation 模板创建和配置 CloudWatch Application Insights 监控

您可以直接从 Amazon CloudFormation 模板中将 Application Insights 监控(包括关键指标和遥测)添加到应用程序、数据库和 Web 服务器。

此部分提供 JSON 和 YAML 格式的示例 Amazon CloudFormation 模板,以帮助您创建和配置 Application Insights 监控。

要查看 Amazon CloudFormation 用户指南中的 Application Insights 资源和属性参考,请参阅 ApplicationInsights 资源类型参考

为整个 Amazon CloudFormation 堆栈创建 Application Insights 应用程序

要应用以下模板,必须创建 Amazon 资源和一个或多个资源组,用于创建 Application Insights 应用程序以监控这些资源。有关更多信息,请参阅 Amazon Resource Groups 入门

以下模板的前两部分指定资源和资源组。模板的最后一部分为资源组创建 Application Insights 应用程序,但不配置应用程序或应用监控。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考中的 CreateApplication 命令详细信息。

JSON 格式的模板

{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Test Resource Group stack", "Resources": { "EC2Instance": { "Type": "AWS::EC2::Instance", "Properties": { "ImageId" : "ami-abcd1234efgh5678i", "SecurityGroupIds" : ["sg-abcd1234"] } }, ... "ResourceGroup": { "Type": "AWS::ResourceGroups::Group", "Properties": { "Name": "my_resource_group" } }, "AppInsightsApp": { "Type": "AWS::ApplicationInsights::Application", "Properties": { "ResourceGroupName": "my_resource_group" }, "DependsOn" : "ResourceGroup" } } }

YAML 格式的模板

--- AWSTemplateFormatVersion: '2010-09-09' Description: Test Resource Group stack Resources: EC2Instance: Type: AWS::EC2::Instance Properties: ImageId: ami-abcd1234efgh5678i SecurityGroupIds: - sg-abcd1234 ... ResourceGroup: Type: AWS::ResourceGroups::Group Properties: Name: my_resource_group AppInsightsApp: Type: AWS::ApplicationInsights::Application Properties: ResourceGroupName: my_resource_group DependsOn: ResourceGroup

以下模板部分将默认监控配置应用于 Application Insights 应用程序。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考中的 CreateApplication 命令详细信息。

AutoConfigurationEnabled 设置为 true 时,将使用 DEFAULT 应用程序层的建议监控设置来配置应用程序的所有组件。有关这些设置和层的更多信息,请参阅 Amazon CloudWatch Application Insights API 参考中的 DescribeComponentConfigurationRecommendationUpdateComponentConfiguration

JSON 格式的模板

{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Test Application Insights Application stack", "Resources": { "AppInsightsApp": { "Type": "AWS::ApplicationInsights::Application", "Properties": { "ResourceGroupName": "my_resource_group", "AutoConfigurationEnabled": true } } } }

YAML 格式的模板

--- AWSTemplateFormatVersion: '2010-09-09' Description: Test Application Insights Application stack Resources: AppInsightsApp: Type: AWS::ApplicationInsights::Application Properties: ResourceGroupName: my_resource_group AutoConfigurationEnabled: true

创建具有详细设置的 Application Insights 应用程序

以下模板执行以下操作:

  • 创建启用了 CloudWatch Events 通知并启用 OpsCenter 的 Application Insights 应用程序。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考中的 CreateApplication 命令详细信息。

  • 使用两个标签来标记应用程序,其中一个标签没有标签值。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考 中的 TagResource

  • 创建两个自定义实例组组件。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考 中的 CreateComponent

  • 创建两个日志模式集。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考 中的 CreateLogPattern

  • AutoConfigurationEnabled 设置为 true,这会使用 DEFAULT 层的建议监控设置来配置应用程序的所有组件。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考 中的 DescribeComponentConfigurationRecommendation

JSON 格式的模板

{ "Type": "AWS::ApplicationInsights::Application", "Properties": { "ResourceGroupName": "my_resource_group", "CWEMonitorEnabled": true, "OpsCenterEnabled": true, "OpsItemSNSTopicArn": "arn:aws-cn:sns:cn-north-1:123456789012:my_topic", "AutoConfigurationEnabled": true, "Tags": [ { "Key": "key1", "Value": "value1" }, { "Key": "key2", "Value": "" } ], "CustomComponents": [ { "ComponentName": "test_component_1", "ResourceList": [ "arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-abcd1234efgh5678i" ] }, { "ComponentName": "test_component_2", "ResourceList": [ "arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-abcd1234efgh5678i", "arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-abcd1234efgh5678i" ] } ], "LogPatternSets": [ { "PatternSetName": "pattern_set_1", "LogPatterns": [ { "PatternName": "deadlock_pattern", "Pattern": ".*\\sDeadlocked\\sSchedulers(([^\\w].*)|($))", "Rank": 1 } ] }, { "PatternSetName": "pattern_set_2", "LogPatterns": [ { "PatternName": "error_pattern", "Pattern": ".*[\\s\\[]ERROR[\\s\\]].*", "Rank": 1 }, { "PatternName": "warning_pattern", "Pattern": ".*[\\s\\[]WARN(ING)?[\\s\\]].*", "Rank": 10 } ] } ] } }

YAML 格式的模板

--- Type: AWS::ApplicationInsights::Application Properties: ResourceGroupName: my_resource_group CWEMonitorEnabled: true OpsCenterEnabled: true OpsItemSNSTopicArn: arn:aws-cn:sns:cn-north-1:123456789012:my_topic AutoConfigurationEnabled: true Tags: - Key: key1 Value: value1 - Key: key2 Value: '' CustomComponents: - ComponentName: test_component_1 ResourceList: - arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-abcd1234efgh5678i - ComponentName: test_component_2 ResourceList: - arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-abcd1234efgh5678i - arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-abcd1234efgh5678i LogPatternSets: - PatternSetName: pattern_set_1 LogPatterns: - PatternName: deadlock_pattern Pattern: ".*\\sDeadlocked\\sSchedulers(([^\\w].*)|($))" Rank: 1 - PatternSetName: pattern_set_2 LogPatterns: - PatternName: error_pattern Pattern: ".*[\\s\\[]ERROR[\\s\\]].*" Rank: 1 - PatternName: warning_pattern Pattern: ".*[\\s\\[]WARN(ING)?[\\s\\]].*" Rank: 10

使用 CUSTOM 模式组件配置创建 Application Insights 应用程序

以下模板执行以下操作:

  • 创建 Application Insights 应用程序。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考 中的 CreateApplication

  • 组件 my_componentComponentConfigurationMode 设置为 CUSTOM,这将导致按照 CustomComponentConfiguration 中指定的配置来配置此组件。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考 中的 UpdateComponentConfiguration

JSON 格式的模板

{ "Type": "AWS::ApplicationInsights::Application", "Properties": { "ResourceGroupName": "my_resource_group, "ComponentMonitoringSettings": [ { "ComponentARN": "my_component", "Tier": "SQL_SERVER", "ComponentConfigurationMode": "CUSTOM", "CustomComponentConfiguration": { "ConfigurationDetails": { "AlarmMetrics": [ { "AlarmMetricName": "StatusCheckFailed" }, ... ], "Logs": [ { "LogGroupName": "my_log_group_1", "LogPath": "C:\\LogFolder_1\\*", "LogType": "DOT_NET_CORE", "Encoding": "utf-8", "PatternSet": "my_pattern_set_1" }, ... ], "WindowsEvents": [ { "LogGroupName": "my_windows_event_log_group_1", "EventName": "Application", "EventLevels": [ "ERROR", "WARNING", ... ], "Encoding": "utf-8", "PatternSet": "my_pattern_set_2" }, ... ], "Alarms": [ { "AlarmName": "my_alarm_name", "Severity": "HIGH" }, ... ] }, "SubComponentTypeConfigurations": [ { "SubComponentType": "EC2_INSTANCE", "SubComponentConfigurationDetails": { "AlarmMetrics": [ { "AlarmMetricName": "DiskReadOps" }, ... ], "Logs": [ { "LogGroupName": "my_log_group_2", "LogPath": "C:\\LogFolder_2\\*", "LogType": "IIS", "Encoding": "utf-8", "PatternSet": "my_pattern_set_3" }, ... ], "WindowsEvents": [ { "LogGroupName": "my_windows_event_log_group_2", "EventName": "Application", "EventLevels": [ "ERROR", "WARNING", ... ], "Encoding": "utf-8", "PatternSet": "my_pattern_set_4" }, ... ] } } ] } } ] } }

YAML 格式的模板

--- Type: AWS::ApplicationInsights::Application Properties: ResourceGroupName: my_resource_group ComponentMonitoringSettings: - ComponentARN: my_component Tier: SQL_SERVER ComponentConfigurationMode: CUSTOM CustomComponentConfiguration: ConfigurationDetails: AlarmMetrics: - AlarmMetricName: StatusCheckFailed ... Logs: - LogGroupName: my_log_group_1 LogPath: C:\LogFolder_1\* LogType: DOT_NET_CORE Encoding: utf-8 PatternSet: my_pattern_set_1 ... WindowsEvents: - LogGroupName: my_windows_event_log_group_1 EventName: Application EventLevels: - ERROR - WARNING ... Encoding: utf-8 PatternSet: my_pattern_set_2 ... Alarms: - AlarmName: my_alarm_name Severity: HIGH ... SubComponentTypeConfigurations: - SubComponentType: EC2_INSTANCE SubComponentConfigurationDetails: AlarmMetrics: - AlarmMetricName: DiskReadOps ... Logs: - LogGroupName: my_log_group_2 LogPath: C:\LogFolder_2\* LogType: IIS Encoding: utf-8 PatternSet: my_pattern_set_3 ... WindowsEvents: - LogGroupName: my_windows_event_log_group_2 EventName: Application EventLevels: - ERROR - WARNING ... Encoding: utf-8 PatternSet: my_pattern_set_4 ...

使用 DEFAULT 模式组件配置创建 Application Insights 应用程序

以下模板执行以下操作:

  • 创建 Application Insights 应用程序。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考 中的 CreateApplication

  • 组件 my_componentComponentConfigurationMode 设置为 DEFAULT,并将 Tier 设置为 SQL_SERVER,这将导致使用 Application Insights 为 SQL_Server 层推荐的配置设置来配置此组件。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考 中的 DescribeComponentConfigurationUpdateComponentConfiguration

JSON 格式的模板

{ "Type": "AWS::ApplicationInsights::Application", "Properties": { "ResourceGroupName": "my_resource_group", "ComponentMonitoringSettings": [ { "ComponentARN": "my_component", "Tier": "SQL_SERVER", "ComponentConfigurationMode": "DEFAULT" } ] } }

YAML 格式的模板

--- Type: AWS::ApplicationInsights::Application Properties: ResourceGroupName: my_resource_group ComponentMonitoringSettings: - ComponentARN: my_component Tier: SQL_SERVER ComponentConfigurationMode: DEFAULT

使用 DEFAULT_WITH_OVERWRITE 模式组件配置创建 Application Insights 应用程序

以下模板执行以下操作:

  • 创建 Application Insights 应用程序。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考 中的 CreateApplication

  • 组件 my_componentComponentConfigurationMode 设置为 DEFAULT_WITH_OVERWRITE,并将 tier 设置为 DOT_NET_CORE,这将导致使用 Application Insights 为 DOT_NET_CORE 层推荐的配置设置来配置此组件。覆盖的配置设置在 DefaultOverwriteComponentConfiguration 中指定:

    • 在组件级别,AlarmMetrics 设置将被覆盖。

    • 在子组件级别,对于 EC2_Instance 类型的子组件,将覆盖 Logs 设置。

    有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考 中的 UpdateComponentConfiguration

JSON 格式的模板

{ "Type": "AWS::ApplicationInsights::Application", "Properties": { "ResourceGroupName": "my_resource_group", "ComponentMonitoringSettings": [ { "ComponentName": "my_component", "Tier": "DOT_NET_CORE", "ComponentConfigurationMode": "DEFAULT_WITH_OVERWRITE", "DefaultOverwriteComponentConfiguration": { "ConfigurationDetails": { "AlarmMetrics": [ { "AlarmMetricName": "StatusCheckFailed" } ] }, "SubComponentTypeConfigurations": [ { "SubComponentType": "EC2_INSTANCE", "SubComponentConfigurationDetails": { "Logs": [ { "LogGroupName": "my_log_group", "LogPath": "C:\\LogFolder\\*", "LogType": "IIS", "Encoding": "utf-8", "PatternSet": "my_pattern_set" } ] } } ] } } ] } }

YAML 格式的模板

--- Type: AWS::ApplicationInsights::Application Properties: ResourceGroupName: my_resource_group ComponentMonitoringSettings: - ComponentName: my_component Tier: DOT_NET_CORE ComponentConfigurationMode: DEFAULT_WITH_OVERWRITE DefaultOverwriteComponentConfiguration: ConfigurationDetails: AlarmMetrics: - AlarmMetricName: StatusCheckFailed SubComponentTypeConfigurations: - SubComponentType: EC2_INSTANCE SubComponentConfigurationDetails: Logs: - LogGroupName: my_log_group LogPath: C:\LogFolder\* LogType: IIS Encoding: utf-8 PatternSet: my_pattern_set