本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 CloudWatch Application Insights 監控無 AWS SAM 伺服器應用程式
Amazon CloudWatch Application Insights 可協助您監控應用程式中 AWS 的資源,以協助識別潛在問題。它可以分析 AWS 資源資料是否有問題的跡象,並建置自動化儀表板以視覺化它們。您可以設定 CloudWatch Application Insights 搭配您的 AWS Serverless Application Model (AWS SAM) 應用程式使用。若要進一步了解 CloudWatch Application Insights,請參閱《Amazon CloudWatch 使用者指南》中的 Amazon CloudWatch Application Insights。 Amazon CloudWatch
使用 設定 CloudWatch Application Insights AWS SAM
透過 AWS SAM 命令列界面 (AWS SAMCLI) 或 範本為您的 AWS SAM 應用程式設定 CloudWatch Application Insights AWS SAM 。
透過 設定 AWS SAMCLI
使用 初始化應用程式時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::ApplicationInsights::Application
中的 AWS::ResourceGroups::Group
和資源來啟用 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 《 使用者指南AWS::ResourceGroups::Group
》中的 。 -
AWS::ApplicationInsights::Application
– 設定資源群組的 CloudWatch Application Insights。如需此資源類型的詳細資訊,請參閱AWS CloudFormation 《 使用者指南AWS::ApplicationInsights::Application
》中的 。
這兩個資源都會在應用程式部署 AWS CloudFormation 時自動傳遞到 。您可以使用 AWS SAM 範本中的 AWS CloudFormation 語法進一步設定 CloudWatch Application 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
後續步驟
設定 CloudWatch Application Insights 之後,請使用 sam build建置您的應用程式sam deploy,以及部署您的應用程式。所有 CloudWatch Application Insights 支援的資源都會設定為監控。
-
如需支援的資源清單,請參閱《Amazon CloudWatch 使用者指南》中的支援日誌和指標。
-
若要了解如何存取 CloudWatch Application Insights,請參閱《Amazon CloudWatch 使用者指南》中的存取 CloudWatch Application Insights。 Amazon CloudWatch