3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Serverless FrameworkでLambdaのログ保存期間を環境によって変える

Last updated at Posted at 2019-10-07

Serverless FrameworkでLambdaのログ保存期間を環境によって変える

AWSのLambdaをServerless Frameworkを利用してデプロイするときに、CloudWatch Logsのログ保存期間を環境によって変更する設定。

環境は以下の三個とする。

  • dev(開発)
  • stg(試験)
  • prod(本番)

Serverless Frameworkの設定ファイル

stageオプションを指定し、デプロイする環境を指定することにする。
customを利用することでコマンドオプションを指定することができる。

$ sls deploy --stage dev
  • 設定ファイルの例
service: serverlessTest

provider:
  name: aws
  runtime: python3.7
  stage: dev  # デフォルトの環境はdev
  logRetentionInDays: 14  # デフォルトのログ保存期間は14日とする

custom:
  stage: ${opt:stage, self:provider.stage}  # デフォルトの環境はdev
  logRetentionInDays:
    dev: "14"
    stg: "60"
    prod: "90"

functions:
  hello:
    handler: handler.hello

resources:
  Resources:
    HelloLogGroup:
      Type: AWS::Logs::LogGroup
      Properties:
        RetentionInDays: ${self:custom.logRetentionInDays.${opt:stage, self:provider.stage}}

Serverless Frameworkは、functionsで指定したhello関数に対して、HelloLogGroupという名前でLogs::LogGroupというAWSリソースを作成する。

Resourcesに指定する項目の注意事項は、公式サイトに記載されている。

When you override basic resources, there are two things to keep in mind when it comes to normalizedFunctionName:

・It should start with an uppercase character
・The - will be changed to Dash, _ will be changed to Underscore

上記のように、Resources に指定するものには、以下の二点に注意すれば良いみたい。

  • 大文字で始まること。
  • -Dashという文字列に変換され、_Underscoreという文字列に変換される。
3
1
0

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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?