0
votes

I know I can create a Scheduled Cloud Watch event by means of AWS Console:

https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/Create-CloudWatch-Events-Scheduled-Rule.html

Is there a way to declare the similar event in Cloud Formation template?

3

3 Answers

2
votes

Below is the example to create a scheduled event in cloudwatch, It creates a rule that invokes the specified Lambda function every 10 minutes. The PermissionForEventsToInvokeLambda resource grants EventBridge permission to invoke the associated function.

"ScheduledRule": {
  "Type": "AWS::Events::Rule",
  "Properties": {
    "Description": "ScheduledRule",
    "ScheduleExpression": "rate(10 minutes)",
    "State": "ENABLED",
    "Targets": [{
      "Arn": { "Fn::GetAtt": ["LambdaFunction", "Arn"] },
      "Id": "TargetFunctionV1"
    }]
  }
},
"PermissionForEventsToInvokeLambda": {
  "Type": "AWS::Lambda::Permission",
  "Properties": {
    "FunctionName": { "Ref": "LambdaFunction" },
    "Action": "lambda:InvokeFunction",
    "Principal": "events.amazonaws.com",
    "SourceArn": { "Fn::GetAtt": ["ScheduledRule", "Arn"] }
  }
}

The example is referenced from AWS official documentation.

1
votes

Yes, it's possible.

The AWS::Events::Rule resource creates a rule that matches incoming Amazon CloudWatch Events (CloudWatch Events) events and routes them to one or more targets for processing.

Here's the sample CloudFormation Snippet:

Type: AWS::Events::Rule
Properties: 
  Description: String
  EventPattern: JSON object
  Name: String
  ScheduleExpression: String
  State: String
  Targets:
    - Target

Here's the official documentation, if you have more questions.

1
votes

Yes, It's possible as share by @bhalothia. Please find an article which will give you a deep dive into it.

Practical Implementation:

http://marcelog.github.io/articles/serverless_cloudwatch_event_cloudformation_template.html

Detail dodcumentation:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-events-rule-target.html

I hope this helps you.