AWS Lambda scheduled event source via cloudformation

grosser picture grosser · Dec 18, 2015 · Viewed 22.2k times · Source

I already have my lambda / roles defined in cloudformation and would love to also use it to add a scheduled eventsources ... are there any docs or examples around ?

Answer

grosser picture grosser · Apr 21, 2016

Use Aws::Event::Rule with a ScheduleExpression and a AWS::Lambda::Permission

// rule to periodically call the lambda
"TagWatcherRule": {
  "Type": "AWS::Events::Rule",
  "Properties": {
    "ScheduleExpression": "rate(10 minutes)",
    "Targets": [
      {
        "Id": "TagWatcherScheduler",
        "Arn": {
          "Fn::GetAtt": [
            "TagWatcherFunction",
            "Arn"
          ]
        }
      }
    ]
  }
},
// role may call the lambda
"InvokeLambdaPermission": {
  "Type": "AWS::Lambda::Permission",
  "Properties": {
    "FunctionName": {
      "Fn::GetAtt": [
        "TagWatcherFunction",
        "Arn"
      ]
    },
    "Action": "lambda:InvokeFunction",
    "Principal": "events.amazonaws.com",
    "SourceArn": {
      "Fn::GetAtt": [
        "TagWatcherRule",
        "Arn"
      ]
    }
  }
}