使用 “ CloudWatch 应用程序见解” 监控您的 AWS SAM 无服务器应用程序 - AWS Serverless Application Model

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

使用 “ CloudWatch 应用程序见解” 监控您的 AWS SAM 无服务器应用程序

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

使用配置 CloudWatch 应用程序见解 AWS SAM

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

通过 AWS SAM CLI 进行配置

使用初始化应用程序时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

通过 AWS SAM 模板进行配置

通过在 AWS SAM 模板中定义AWS::ResourceGroups::GroupAWS::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 用户指南》中的 AWS::ResourceGroups::Group

  • AWS::ApplicationInsights::Application— 为资源组配置 “ CloudWatch 应用程序见解”。有关此资源类型的更多信息,请参阅《AWS CloudFormation 用户指南》中的 AWS::ApplicationInsights::Application

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

使用该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

后续步骤

配置 App CloudWatch lication Insights 后,使用sam buildsam deploy来构建应用程序和部署应用程序。所有支持 CloudWatch 应用程序见解的资源都将配置为用于监控。