26
17

More than 5 years have passed since last update.

SAM で API Gateway の CloudWatch Logs を有効にしたい

Last updated at Posted at 2018-02-17

この記事は、AWS Serverless Application Model (AWS SAM) で API Gateway の CloudWatch Logs を有効化しようとした際の記録です。

AWS Serverless Application Model (AWS SAM) とは

API Gateway の「CloudWatch Logs の有効化」とは

有効にすることで、API Gateway のログを CloudWatch Logs の以下の場所に保存されるようになります。

API-Gateway-Execution-Logs_<10桁のID>/<ステージ名>

apig-log.png

API Gateway の「CloudWatch Logs の有効化」の設定

SAM の AWS::Serverless::Api の設定を確認してみた

  • SAM の AWS::Serverless::Api ではログの設定ができなかった。。。

SAM の Issues を探してみた

さらに、SAM の Issues を探してみた

1. AWS Management Console で手動で設定する

  • SAM の AWS::Serverless::Api でリソースを作成する度にログの設定が元に戻ってしまった。。。

2. API の deploy 後に AWS CLI で設定を更新する

  • SAM の AWS::Serverless::Api でリソース作成後に、rest-api-id を取得できればできそう。
  • AWS CLI で rest-api-id を取得後、以下でログの設定に成功
aws apigateway update-stage \
  --rest-api-id xxx \
  --stage-name prod \
  --patch-operations \
      "op=replace,path=/*/*/logging/dataTrace,value=true" \
      "op=replace,path=/*/*/logging/loglevel,value=INFO"

3. AWS CloudFormation の AWS::ApiGateway::Stage の MethodSettings で設定する

4. (おまけ)AWS CloudFormation の AWS::ApiGateway::Deployment の StageDescription で設定する

  • AWS::Serverless::Api と AWS::ApiGateway::Deployment のリソース作成仕様とするとエラーが・・・

CREATE_FAILED AWS::ApiGateway::Deployment Deployment StageDescription cannot be specified when stage referenced by StageName already exists

template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  SampleApi: 
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      DefinitionUri: swagger.yml

  SampleApiDeployment:
    Type: AWS::ApiGateway::Deployment
    Properties:
      RestApiId: !Ref SampleApi
      StageName: prod
      StageDescription: StageDescription
        - DataTraceEnabled: true
          LoggingLevel: INFO
          MetricsEnabled: true

まとめ

以下の手順で、SAM の AWS::Serverless::Api のデプロイ後、AWS CLI で API Gateway の CloudWatch Logs の有効化ができた。

deploy.sh
aws cloudformation package \
  --template-file /path_to_template/template.yml \
  --output-template-file /path_to_template/template_output.yml \
  --s3-bucket my_deploy_bucket \
  --region aws_region

aws cloudformation deploy \
  --template-file /path_to_template/template_output.yml \
  --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
  --parameter-overrides \
      Environment=prod \
  --stack-name my_stack_name \
  --region aws_region

aws cloudformation list-stack-resources \
  --stack-name my_stack_name \
  --region aws_region \
  | jq -r '.StackResourceSummaries[] | select(.ResourceType == "AWS::ApiGateway::RestApi") | .PhysicalResourceId' > rest-api-id.txt

while read api_id
do
  aws apigateway update-stage \
    --rest-api-id ${api_id} \
    --stage-name prod \
    --patch-operations \
      "op=replace,path=/*/*/logging/dataTrace,value=true" \
      "op=replace,path=/*/*/logging/loglevel,value=INFO" \
    --region aws_region
done < rest-api-id.txt

SAM で API Gateway の CloudWatch Logs の設定ができるようになっていました(2018/05/13 追記)

2018/03/13 にリリースされた 1.4.0 から SAM で設定できるようになっていました。

SAM の AWS::Serverless::Api で設定する

template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  SampleApi: 
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      DefinitionUri: path_to_swagger/swagger.yml
      MethodSettings:
        - DataTraceEnabled: true
          LoggingLevel: 'ERROR'
          ResourcePath: '/*'
          HttpMethod: '*'
26
17
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
17