

# CloudFormation テンプレートを使用して CloudWatch Application Insights モニターリングを作成および設定する
<a name="appinsights-cloudformation"></a>

AWS CloudFormation テンプレートから、主要メトリクスやテレメトリを含む Application Insights モニターリングをアプリケーション、データベース、ウェブサーバーに追加できます。

このセクションでは、Application Insights モニターリングの作成および設定に役立つサンプル CloudFormation テンプレートを JSON 形式と YAML 形式の両方で提供しています。

*CloudFormation ユーザーガイド*の Application Insights リソースとプロパティのリファレンスを表示するには、[ApplicationInsights リソースタイプのリファレンス](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/AWS_ApplicationInsights.html)を参照してください。

**Topics**
+ [CloudFormation スタック全体の Application Insights アプリケーションを作成する](#appinsights-cloudformation-apply-to-stack)
+ [詳細な設定を使用した Application Insights アプリケーションの作成](#appinsights-cloudformation-apply-detailed)
+ [`CUSTOM` モードのコンポーネント設定を使用した Application Insights アプリケーションの作成](#appinsights-cloudformation-custom)
+ [`DEFAULT` モードのコンポーネント設定を使用した Application Insights アプリケーションの作成](#appinsights-cloudformation-default)
+ [`DEFAULT_WITH_OVERWRITE` モードのコンポーネント設定を使用した Application Insights アプリケーションの作成](#appinsights-cloudformation-default-with-overwrite)

## CloudFormation スタック全体の Application Insights アプリケーションを作成する
<a name="appinsights-cloudformation-apply-to-stack"></a>

次のテンプレートを適用するには、AWS リソースと、それらのリソースをモニターリングする Application Insights アプリケーションの作成元となる 1 つ以上の Resource Groups を作成する必要があります。詳細については、「[AWS Resource Groups の開始方法](https://docs.aws.amazon.com/ARG/latest/userguide/gettingstarted.html)」を参照してください。

次のテンプレートの最初の 2 つの部分は、リソースとリソースグループを指定します。テンプレートの最後の部分では、リソースグループの Application Insights アプリケーションを作成しますが、アプリケーションの設定やモニターリングの適用は行いません。詳細については、*Amazon CloudWatch Application Insights API リファレンス*の [CreateApplication](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_CreateApplication.html) コマンドの詳細を参照してください。

**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](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_CreateApplication.html) コマンドの詳細を参照してください。

`AutoConfigurationEnabled` を `true` に設定すると、アプリケーションのすべてのコンポーネントが、`DEFAULT` アプリケーション層の推奨モニターリング設定を使用して設定されます。これらの設定と層の詳細については、*Amazon CloudWatch Application Insights API リファレンス*の「[DescribeComponentConfigurationRecommendation](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_DescribeComponentConfigurationRecommendation.html)」と「[UpdateComponentConfiguration](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_UpdateComponentConfiguration.html)」を参照してください。

**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 アプリケーションの作成
<a name="appinsights-cloudformation-apply-detailed"></a>

次のテンプレートは、これらのアクションを実行します。
+ CloudWatch Events 通知と OpsCenter が有効な Application Insights アプリケーションを作成します。詳細については、*Amazon CloudWatch Application Insights API リファレンス*の [CreateApplication](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_CreateApplication.html) コマンドの詳細を参照してください。
+ アプリケーションに 2 つのタグを付けます。そのうちの 1 つにはタグ値がありません。詳細については、*Amazon CloudWatch Application Insights API リファレンス*の「[TagResource](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_TagResource.html)」を参照してください。
+ 2 つのカスタムインスタンスグループコンポーネントを作成します。詳細については、*Amazon CloudWatch Application Insights API リファレンス*の「[CreateComponent](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_CreateComponent.html)」を参照してください。
+ 2 つのログパターンセットを作成します。詳細については、*Amazon CloudWatch Application Insights API リファレンス*の「[CreateLogPattern](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_CreateLogPattern.html)」を参照してください。
+ `AutoConfigurationEnabled` を `true` に設定します。これにより、アプリケーションのすべてのコンポーネントが、`DEFAULT` 層で推奨されるモニターリング設定で構成されます。詳細については、*Amazon CloudWatch Application Insights API リファレンス*の「[DescribeComponentConfigurationRecommendation](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_DescribeComponentConfigurationRecommendation.html)」を参照してください。

**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 アプリケーションの作成
<a name="appinsights-cloudformation-custom"></a>

次のテンプレートは、これらのアクションを実行します。
+ Application Insights アプリケーションを作成します。詳細については、*Amazon CloudWatch Application Insights API リファレンス*の「[CreateApplication](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_CreateApplication.html)」を参照してください。
+ Component `my_component` は `ComponentConfigurationMode` を `CUSTOM` に設定します。これにより、このコンポーネントは `CustomComponentConfiguration` で指定された設定で設定されます。詳細については、*Amazon CloudWatch Application Insights API リファレンス*の「[UpdateComponentConfiguration](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_UpdateComponentConfiguration.html)」を参照してください。

**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 アプリケーションの作成
<a name="appinsights-cloudformation-default"></a>

次のテンプレートは、これらのアクションを実行します。
+ Application Insights アプリケーションを作成します。詳細については、*Amazon CloudWatch Application Insights API リファレンス*の「[CreateApplication](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_CreateApplication.html)」を参照してください。
+ コンポーネント `my_component` は、`ComponentConfigurationMode` を `DEFAULT` に設定し、`Tier` を `SQL_SERVER` に設定します。これにより、このコンポーネントは、Application Insights が `SQL_Server` 層の推奨される構成設定を使用して構成されます。詳細については、*Amazon CloudWatch Application Insights API リファレンス*の「[DescribeComponentConfiguration](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_DescribeComponentConfiguration.html)」と「[UpdateComponentConfiguration](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_UpdateComponentConfiguration.html)」を参照してください。

**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 アプリケーションの作成
<a name="appinsights-cloudformation-default-with-overwrite"></a>

次のテンプレートは、これらのアクションを実行します。
+ Application Insights アプリケーションを作成します。詳細については、*Amazon CloudWatch Application Insights API リファレンス*の「[CreateApplication](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_CreateApplication.html)」を参照してください。
+ コンポーネント `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](https://docs.aws.amazon.com/cloudwatch/latest/APIReference/API_UpdateComponentConfiguration.html)」を参照してください。

**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
```