使用 CloudFormation 模板创建和配置 CloudWatch Application Insights 监控
您可以直接从 AWS CloudFormation 模板中将 Application Insights 监控(包括关键指标和遥测)添加到应用程序、数据库和 Web 服务器。
此部分提供 JSON 和 YAML 格式的示例 AWS CloudFormation 模板,以帮助您创建和配置 Application Insights 监控。
要查看 AWS CloudFormation 用户指南中的 Application Insights 资源和属性参考,请参阅 ApplicationInsights 资源类型参考。
示例 模板
为整个 AWS CloudFormation 堆栈创建 Application Insights 应用程序
要应用以下模板,必须创建 AWS 资源和一个或多个资源组,用于创建 Application Insights 应用程序以监控这些资源。有关更多信息,请参阅 AWS 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 参考中的 DescribeComponentConfigurationRecommendation 和 UpdateComponentConfiguration。
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: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
使用 CUSTOM
模式组件配置创建 Application Insights 应用程序
以下模板执行以下操作:
-
创建 Application Insights 应用程序。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考中的 CreateApplication。
-
组件
my_component
将ComponentConfigurationMode
设置为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" }, ... ], "processes" : [ { "processName" : "my_process", "alarmMetrics" : [ { "alarmMetricName" : "procstat cpu_usage", "monitor" : true }, { "alarmMetricName" : "procstat memory_rss", "monitor" : true } ] } ], "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 ... Processes: - ProcessName: my_process AlarmMetrics: - AlarmMetricName: procstat cpu_usage ... ... 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_component
将ComponentConfigurationMode
设置为DEFAULT
,并将Tier
设置为SQL_SERVER
,这将导致使用 Application Insights 为SQL_Server
层推荐的配置设置来配置此组件。有关更多信息,请参阅 Amazon CloudWatch Application Insights API 参考中的 DescribeComponentConfiguration 和 UpdateComponentConfiguration。
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_component
将ComponentConfigurationMode
设置为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