使用 CloudWatch Application Insights 监控您的 Amazon SAM 无服务器应用程序 - Amazon Serverless Application Model
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 CloudWatch Application Insights 监控您的 Amazon SAM 无服务器应用程序

Amazon CloudWatch Application Insights 可帮助您监控应用程序中的 Amazon 资源,以帮助识别潜在问题。它可以分析 Amazon 资源数据以寻找问题迹象,并构建自动化控制面板以将其可视化。您可以配置 CloudWatch Application Insights 以与 Amazon Serverless Application Model (Amazon SAM) 应用程序一起使用。有关 CloudWatch Application Insights 的更多信息,请参阅《Amazon CloudWatch 用户指南》中的 Amazon CloudWatch Application Insights

使用 Amazon SAM 配置 CloudWatch Application Insights

通过 Amazon SAM 命令行界面 (Amazon SAM CLI) 或 Amazon SAM 模板为您的 Amazon SAM 应用程序配置 CloudWatch Application Insights。

通过 Amazon SAM CLI 进行配置

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

要通过 Amazon 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

通过 Amazon SAM 模板进行配置

通过在 Amazon SAM 模板中定义 AWS::ResourceGroups::GroupAWS::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 – 创建组来组织您的 Amazon 资源,以便同时管理和自动执行针对大量资源的任务。在这里,您可以创建一个资源组以与 CloudWatch Application Insights 一起使用。有关此资源类型的更多信息,请参阅《Amazon CloudFormation 用户指南》中的 AWS::ResourceGroups::Group

  • AWS::ApplicationInsights::Application – 为资源组配置 CloudWatch Application Insights。有关此资源类型的更多信息,请参阅《Amazon CloudFormation 用户指南》中的 AWS::ApplicationInsights::Application

在应用程序部署时,两种资源都会自动传递给 Amazon CloudFormation。您可以使用 Amazon SAM 模板中的 Amazon CloudFormation 语法进一步配置 CloudWatch Application Insights。有关更多信息,请参阅《Amazon CloudWatch 用户指南》中的使用 Amazon CloudFormation 模板

使用 sam init --application-insights 命令时,这两种资源都将在您的 Amazon 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

后续步骤

配置 CloudWatch Application Insights 后,使用 sam build 构建应用程序并使用 sam deploy 部署应用程序。所有 CloudWatch Application Insights 支持的资源都将配置为监控。