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

如果我们为英文版本指南提供翻译,那么如果存在任何冲突,将以英文版本指南为准。在提供翻译时使用机器翻译。

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

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

本节提供样本 AWS CloudFormation 模板,以帮助您创建和配置 Application Insights 监控。

要查看 Application Insights 资源和属性参考 AWS CloudFormation 用户指南,见 应用程序Insights资源类型参考.

创建 Application Insights 适用于整个 AWS CloudFormation 堆栈

要应用以下模板,您必须创建要从中创建的一个或多个资源组 Application Insights 监控这些资源的应用程序。更多信息,请参阅 开始使用ASM资源组.

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

模板的JNS格式

{ "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参考.

何时 AutoConfigurationEnabled 设置为 true,应用程序的所有组件均配置了推荐的监控设置, DEFAULT 应用程序层。有关这些设置和层级的更多信息,请参阅 描述组件配置建议更新组件配置Amazon CloudWatch Application Insights API参考.

模板的JNS格式

{ "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 包含详细设置的应用程序

以下模板执行这些操作:

  • 创建 Application Insights 应用 CloudWatch Events 通知和OpsCenter启用。更多信息,请参阅 创建应用程序 命令详细信息 Amazon CloudWatch Application Insights API参考.

  • 使用两个标记标记标记应用程序,其中一个标记无标记值。更多信息,请参阅 Tag资源Amazon CloudWatch Application Insights API参考.

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

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

  • 套组 AutoConfigurationEnabledtrue,它使用推荐的监控设置配置应用程序的所有组件, DEFAULT 第级。更多信息,请参阅 描述组件配置建议Amazon CloudWatch Application Insights API参考.

模板的JNS格式

{ "Type": "AWS::ApplicationInsights::Application", "Properties": { "ResourceGroupName": "my_resource_group", "CWEMonitorEnabled": true, "OpsCenterEnabled": true, "OpsItemSNSTopicArn": "arn:aws:sns:us-east-1:123456789012:my_topic", "AutoConfigurationEnabled": true, "Tags": [ { "Key": "key1", "Value": "value1" }, { "Key": "key2", "Value": "" } ], "CustomComponents": [ { "ComponentName": "test_component_1", "ResourceList": [ "arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1234efgh5678i" ] }, { "ComponentName": "test_component_2", "ResourceList": [ "arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1234efgh5678i", "arn:aws:ec2:us-east-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:sns:us-east-1:123456789012:my_topic AutoConfigurationEnabled: true Tags: - Key: key1 Value: value1 - Key: key2 Value: '' CustomComponents: - ComponentName: test_component_1 ResourceList: - arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1234efgh5678i - ComponentName: test_component_2 ResourceList: - arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1234efgh5678i - arn:aws:ec2:us-east-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

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

以下模板执行这些操作:

  • 创建 Application Insights 应用程序。更多信息,请参阅 创建应用程序Amazon CloudWatch Application Insights API参考.

  • 部件 my_componentComponentConfigurationModeCUSTOM,这会导致此组件使用中指定的配置配置 CustomComponentConfiguration。更多信息,请参阅 更新组件配置Amazon CloudWatch Application Insights API参考.

模板的JNS格式

{ "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 ...

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

以下模板执行这些操作:

  • 创建 Application Insights 应用程序。更多信息,请参阅 创建应用程序Amazon CloudWatch Application Insights API参考.

  • 部件 my_componentComponentConfigurationModeDEFAULTTierSQL_SERVER,这会导致此组件配置了以下配置设置: Application Insights 建议 SQL_Server 第级。更多信息,请参阅 描述组件配置更新组件配置Amazon CloudWatch Application Insights API参考.

模板的JNS格式

{ "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

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

以下模板执行这些操作:

  • 创建 Application Insights 应用程序。更多信息,请参阅 创建应用程序Amazon CloudWatch Application Insights API参考.

  • 部件 my_componentComponentConfigurationModeDEFAULT_WITH_OVERWRITEtierDOT_NET_CORE,这会导致此组件配置了以下配置设置: Application Insights 建议 DOT_NET_CORE 第级。覆盖配置设置在 DefaultOverwriteComponentConfiguration:

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

    • 在子组件级别, EC2_Instance 输入子组件, Logs 设置将被覆盖。

    更多信息,请参阅 更新组件配置Amazon CloudWatch Application Insights API参考.

模板的JNS格式

{ "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