

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# CloudWatch Application Insights を使用して AWS SAM サーバーレスアプリケーションをモニタリングする
<a name="monitor-app-insights"></a>

 Amazon CloudWatch Application Insights は、アプリケーション内の AWS リソースをモニタリングして潜在的な問題を特定するのに役立ちます。 AWS リソースデータを分析して問題の兆候がないか確認し、自動ダッシュボードを構築して可視化できます。 AWS Serverless Application Model (AWS SAM) アプリケーションで使用するように CloudWatch Application Insights を設定できます。CloudWatch Application Insights の詳細については、「Amazon CloudWatch ユーザーガイド」の「[Amazon CloudWatch Application Insights](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html)」を参照してください。

**Topics**
+ [を使用した CloudWatch Application Insights の設定 AWS SAM](#monitor-app-insights-configure)
+ [次の手順](#monitor-app-insights-next)

## を使用した CloudWatch Application Insights の設定 AWS SAM
<a name="monitor-app-insights-configure"></a>

 コマンドラインインターフェイス (AWS SAM CLI) AWS SAM または AWS SAM テンプレートを使用して、 AWS SAM アプリケーションの CloudWatch Application Insights を設定します。

### AWS SAM CLI による設定
<a name="monitor-app-insights-configure-cli"></a>

 **sam init** でアプリケーションを初期化するときは、インタラクティブフローまたは **--application-insights** オプションを使用して CloudWatch Application Insights を有効にします。

 AWS SAM CLI のインタラクティブフローで CloudWatch Application Insights を有効にするには、プロンプトが表示されたら **y** と入力します。

```
Would you like to enable monitoring using CloudWatch Application Insights?
For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]:
```

 **--application-insights** オプションで CloudWatch Application Insights を有効にするには、次の操作を行います。

```
sam init --application-insights
```

 **sam init** コマンドを使用する場合の詳細については、「[sam init](sam-cli-command-reference-sam-init.md)」を参照してください。

### AWS SAM テンプレートを使用して を設定する
<a name="monitor-app-insights-configure-template"></a>

 AWS SAM テンプレートで `AWS::ResourceGroups::Group`および `AWS::ApplicationInsights::Application`リソースを定義して、CloudWatch Application Insights を有効にします。

```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31 
...
Resources:
  ApplicationResourceGroup:
    Type: AWS::ResourceGroups::Group
    Properties:
      Name:
        Fn::Join:
        - ''
        - - ApplicationInsights-SAM-
        - Ref: AWS::StackName
      ResourceQuery:
        Type: CLOUDFORMATION_STACK_1_0
  ApplicationInsightsMonitoring:
    Type: AWS::ApplicationInsights::Application
    Properties:
      ResourceGroupName:
        Fn::Join:
          - ''
          - - ApplicationInsights-SAM-
          - Ref: AWS::StackName
        AutoConfigurationEnabled: 'true'
    DependsOn: ApplicationResourceGroup
```
+  `AWS::ResourceGroups::Group` – 多数の AWS リソースのタスクを一度に管理および自動化するために、リソースを整理するグループを作成します。ここでは、CloudWatch Application Insights で使用するリソースグループを作成します。このリソースタイプの詳細については、「AWS CloudFormation ユーザーガイド」の「[https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-resourcegroups-group.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-resourcegroups-group.html)」を参照してください。
+  `AWS::ApplicationInsights::Application` — リソースグループの CloudWatch Application Insights を設定します。このリソースタイプの詳細については、「AWS CloudFormation ユーザーガイド」の「[https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-applicationinsights-application.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-applicationinsights-application.html)」を参照してください。

 両方のリソースは、アプリケーションのデプロイ CloudFormation 時に に自動的に渡されます。 AWS SAM テンプレートの CloudFormation 構文を使用して、CloudWatch Application Insights をさらに設定できます。詳細については、*Amazon CloudWatch * [ユーザーガイド」の CloudFormation 「テンプレート](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/appinsights-cloudformation.html)の使用」を参照してください。

 **sam init --application-insights** コマンドを使用すると、これらのリソースの両方が AWS SAM テンプレートで自動的に生成されます。生成されたテンプレートの例を示します。

```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sam-app-test
  
  Sample SAM Template for sam-app-test

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3
    MemorySize: 128

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
      - x86_64
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

  ApplicationResourceGroup:
    Type: AWS::ResourceGroups::Group
    Properties:
      Name:
        Fn::Join:
        - ''
        - - ApplicationInsights-SAM-
        - Ref: AWS::StackName
      ResourceQuery:
      	Type: CLOUDFORMATION_STACK_1_0
  ApplicationInsightsMonitoring:
    Type: AWS::ApplicationInsights::Application
    Properties:
      ResourceGroupName:
        Fn::Join:
        - ''
        - - ApplicationInsights-SAM-
        - Ref: AWS::StackName
      AutoConfigurationEnabled: 'true'
    DependsOn: ApplicationResourceGroup
    
Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  HelloWorldApi:
    Description: API Gateway endpoint URL for Prod stage for Hello World function
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: Hello World Lambda Function ARN
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: Implicit IAM Role created for Hello World function
    Value: !GetAtt HelloWorldFunctionRole.Arn
```

## 次の手順
<a name="monitor-app-insights-next"></a>

 CloudWatch Application Insights を設定したら、**sam build** を使用してアプリケーションを構築し、**sam deploy** を使用してデプロイします。CloudWatch Application Insights がサポートするリソースはすべてモニタリング対象に設定されます。
+  サポートされているリソースのリストについては、「Amazon CloudWatch ユーザーガイド」の「[サポートされているログとメトリクス](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/appinsights-logs-and-metrics.html)」を参照してください。
+  CloudWatch Application Insights へのアクセス方法の詳細については、「Amazon CloudWatch ユーザーガイド」の「[CloudWatch Application Insights へのアクセス](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/appinsights-accessing.html)」を参照してください。