CloudWatch Application Insights を使用したサーバーレスアプリケーションのモニタリング - AWS Serverless Application Model

CloudWatch Application Insights を使用したサーバーレスアプリケーションのモニタリング

Amazon CloudWatch Application Insights は、アプリケーション内の AWS リソースを監視して潜在的な問題を特定するのに役立ちます。AWS リソースデータを分析して問題の兆候を探し、それらを視覚化する自動ダッシュボードを構築できます。CloudWatch Application Insights を AWS Serverless Application Model (AWS SAM) アプリケーションで使用するように設定できます。CloudWatch Application Insights の詳細については、「Amazon CloudWatch ユーザーガイド」の「Amazon CloudWatch Application Insights」を参照してください。

Configuring CloudWatch Application Insights と AWS SAM

AWS SAM コマンドラインインターフェイス (AWS SAM CLI) または AWS SAM テンプレートを使用して、AWS SAM アプリケーションの CloudWatch Application Insights を設定します。

AWS SAM CLI による設定

sam init でアプリケーションを初期化するときは、インタラクティブフローまたは --application-insights オプションを使用して CloudWatch Application Insights を有効にします。

AWS 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」を参照してください。

AWS SAM テンプレートによる設定

AWS 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 — 大量のリソースのタスクを一度に管理および自動化するために、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 がサポートするリソースはすべてモニタリング対象に設定されます。