AWS自習 6日目「Lambdaチュートリアル EventBridge」

AWS SAMでEventBridgeを登録してLambdaを定期実行します。

template.yaml

Resources:
  SamSampleFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: sam-sample/
      Handler: app.handler
      Runtime: nodejs14.x
      Policies: AmazonDynamoDBFullAccess
      Events:
        ScheduledFunction:
          Type: Schedule
          Properties:
            # Schedule: 'rate(1 minute)'
            Schedule: 'cron(0 21 16 2 ? 2022)'
            Enabled: true
            Name: TestSchedule
            Description: EventBridge test schedule
・・・

Events

https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-events

EventSource

https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-property-function-eventsource.html

Type

https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-property-function-eventsource.html#sam-function-eventsource-type

Properties

https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-property-function-eventsource.html#sam-function-eventsource-properties

Schedule

https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-property-function-schedule.html

https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-property-function-schedule.html#sam-function-schedule-schedule

EventBridge rule

https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html

Cron

https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html#eb-cron-expressions
'cron(0 21 16 2 ? 2022)' は 2022年2月17日 6:00 に実行します。時刻はUTCで指定します。

Rate

https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html#eb-rate-expressions
'rate(1 minute)' は1分間隔で実行します。

Name

https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-property-function-schedule.html#sam-function-schedule-name

Description

https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-property-function-schedule.html#sam-function-schedule-description

deploy

ビルド、デプロイします。

EventBridge 確認

EventBridgeコンソールでルールが作成されていることを確認します。

DynamoDB確認

DynamoDBコンソール

CloudWatchログ

参考:


いいなと思ったら応援しよう!