

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 “ CloudWatch 应用程序见解” 监控您的 AWS SAM 无服务器应用程序
<a name="monitor-app-insights"></a>

 Amazon App CloudWatch lication Insights 可帮助您监控应用程序中的 AWS 资源，以帮助识别潜在问题。它可以分析 AWS 资源数据以寻找问题迹象，并构建自动仪表板以将其可视化。您可以将 “ CloudWatch 应用程序见解” 配置为与您的 AWS Serverless Application Model (AWS SAM) 应用程序一起使用。要了解有关 CloudWatch 应用程序见解的更多信息，请参阅《[亚马逊* CloudWatch 用户指南》中的 “亚马逊 CloudWatch *应用程序见解](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html)”。

**Topics**
+ [使用配置 CloudWatch 应用程序见解 AWS SAM](#monitor-app-insights-configure)
+ [后续步骤](#monitor-app-insights-next)

## 使用配置 CloudWatch 应用程序见解 AWS SAM
<a name="monitor-app-insights-configure"></a>

 通过 AWS SAM 命令行界面 (AWS SAMCLI) 或 AWS SAM 模板为您的 CloudWatch AWS SAM 应用程序配置应用程序见解。

### 通过 AWS SAM CLI 进行配置
<a name="monitor-app-insights-configure-cli"></a>

 使用初始化应用程序时**sam init**，请通过交互式流程或使用**--application-insights**选项激活 CloudWatch Application Insights。

 要通过 AWS SAMCLI互动流程激活 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`资源来激活 App CloudWatch lication 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 应用见解》配合使用。有关此资源类型的更多信息，请参阅*《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 应用程序见解”。有关此资源类型的更多信息，请参阅*《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 语法进一步配置 App CloudWatch lication 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>

 配置 App CloudWatch lication Insights 后，使用**sam build****sam deploy**来构建应用程序和部署应用程序。所有支持 CloudWatch 应用程序见解的资源都将配置为用于监控。
+  有关支持的资源列表，请参阅 *Amazon CloudWatch 用户指南*中的[支持的日志和指标](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/appinsights-logs-and-metrics.html)。
+  要了解如何访问 CloudWatch 应用程序见解，请参阅 *Amazon CloudWatch 用户指南*中的[访问 CloudWatch 应用程序见解](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/appinsights-accessing.html)。