LoginSignup
0
0

More than 1 year has passed since last update.

Serverless Frameworkでevent scheduleの起動時間をステージ毎に切り替える

Posted at

きっかけ

dev/prodの2環境をdeployして並行して動かすようにした。
event scheduleの時間起動をrate: cron(30 /3 * * ? *)のようにしていたため、2環境ともに同時刻に起動してしまい不都合があったりもした。

対応策

以下のようにcronの起動時間の部分を外部ファイルでdev用、prod用にわけて、deployするようにしました。
同じような記事があったため、ほぼパクリです。

serverless.yml
service: scraping
custom:
  dev: ${file(./dev.yml)}
  prod: ${file(./prod.yml)}
provider:
  name: aws
  runtime: python3.7
  region: ap-northeast-1
  stage: ${opt:stage, 'dev'}
functions:
  func1:
    name: crawler-${self:provider.stage}
    handler: handler.crawler
    events:
      - schedule:
          rate: ${self:custom.${self:provider.stage}.cron}
          enabled: true
          name: sc-crawler-${self:provider.stage}
          input:
            body:
              target: xxxxxxxxx
              url : https://yyyy.zzzzz/xxxxxx

devの時間を15分早く起動するようにした。

dev.yml
  cron: cron(15 /3 * * ? *)
prod.yml
  cron: cron(30 /3 * * ? *)
handler.py
def crawler(event, context):
    url = event['body']['url']

こちらは、devのdeploy。--stageの指定なしの場合、devがデフォルトとなっています。

sls deploy -v

こちらはprodのdeployです。

sls deploy -v --stage prod

参考

0
0
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
0
0