本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用应用程序洞察监控您的无服务器 CloudWatch 应用程序
Amazon A CloudWatch pplication Insights 可帮助您监控应用程序中的Amazon资源,以帮助识别潜在问题。它可以分析Amazon资源数据以寻找问题迹象,并构建自动仪表板以将其可视化。您可以将 Applicati CloudWatch on Insights 配置为与您的Amazon Serverless Application Model (Amazon SAM) 应用程序一起使用。要了解有关 CloudWatch 应用程序见解的更多信息,请参阅亚马逊 CloudWatch 用户指南中的亚马逊 CloudWatch 应用程序见解。
使用以下 CloudWatch 方式配置应用程序见解Amazon SAM
通过Amazon SAM命令行界面 (Amazon SAMCLI) 或Amazon SAM模板为您的 CloudWatch Amazon SAM应用程序配置 Application Insights。
通过Amazon SAM CLI 进行配置
使用初始化应用程序时sam init,通过交互式流程或使用--application-insights选项激活 CloudWatch Application Insights。
要通过Amazon SAM CLI 交互式流程激活 CloudWatch 应用程序见解,请在出现提示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选项激活 App CloudWatch lication Insights,请执行以下操作。
sam init --application-insights
要了解有关sam init命令的更多信息,请参阅sam init。
通过Amazon SAM模板配置
通过在Amazon 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— 通过使用 ResourAmazon ce Groups,同时管理和自动完成针对大量资源的任务。在这里,您可以创建一个资源组以与 Application Insights 一起 CloudWatch 使用。有关此资源类型的更多信息,请参阅AWS::ResourceGroups::GroupAmazon CloudFormation用户指南中的。 -
AWS::ApplicationInsights::Application— 为资源组配置 CloudWatch 应用程序见解。有关此资源类型的更多信息,请参阅AWS::ApplicationInsights::ApplicationAmazon CloudFormation用户指南中的。
这两种资源都会Amazon CloudFormation在应用程序部署时自动传递给。您可以使用Amazon SAM模板中的Amazon CloudFormation语法进一步配置 Application CloudWatch Insights。有关更多信息,请参阅亚马逊 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
后续步骤
配置 Appl CloudWatch ication Insights 后sam deploy,用于sam build构建应用程序和部署应用程序。Appl CloudWatch ication Insights 支持的所有资源都将配置为进行监控
-
有关支持的资源列表,请参阅亚马逊 CloudWatch 用户指南中的支持的日志和指标。
-
要了解如何访问 CloudWatch 应用程序见解,请参阅亚马逊 CloudWatch 用户指南中的访问 CloudWatch 应用程序见解。