{"id":12030,"date":"2025-02-17T04:57:07","date_gmt":"2025-02-17T04:57:07","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=12030"},"modified":"2025-02-17T04:59:48","modified_gmt":"2025-02-17T04:59:48","slug":"use-terraform-to-define-cloudwatch-event-rules","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/cloud\/use-terraform-to-define-cloudwatch-event-rules","title":{"rendered":"How to Use Terraform to Define Cloundwatch Event Rules to Trigger StepFunction Statemachine"},"content":{"rendered":"<p>For this, we have to do following Terraform Configuration:<\/p>\n<h3>1. Define the rule for CloudWatch event:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"dart\">\r\nresource \"aws_cloudwatch_event_rule\" \"example\" {\r\n  name        = \"trigger-stepfunction\"\r\n  description = \"Rule to trigger Step Functions state machine\"\r\n  event_pattern = jsonencode({\r\n    \"source\" : [\"aws.s3\"],\r\n    \"detail-type\" : [\"Object Created\"],\r\n    \"detail\" : {\r\n      \"bucket-name\" : [\"your-bucket-name\"]\r\n    }\r\n  })\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>2. Create the IAM role for Cloudwatch event:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"dart\">\r\nresource \"aws_iam_role\" \"event_to_stepfunction\" {\r\n  name = \"event-to-stepfunction-role\"\r\n\r\n  assume_role_policy = jsonencode({\r\n    \"Version\": \"2012-10-17\",\r\n    \"Statement\": [\r\n      {\r\n        \"Action\": \"sts:AssumeRole\",\r\n        \"Effect\": \"Allow\",\r\n        \"Principal\": {\r\n          \"Service\": \"events.amazonaws.com\"\r\n        }\r\n      }\r\n    ]\r\n  })\r\n}\r\n\r\nresource \"aws_iam_policy\" \"stepfunction_policy\" {\r\n  name        = \"stepfunction-trigger-policy\"\r\n  description = \"Policy to allow CloudWatch Events to trigger Step Functions\"\r\n  policy      = jsonencode({\r\n    \"Version\": \"2012-10-17\",\r\n    \"Statement\": [\r\n      {\r\n        \"Effect\": \"Allow\",\r\n        \"Action\": [\r\n          \"states:StartExecution\"\r\n        ],\r\n        \"Resource\": \"arn:aws:states:REGION:ACCOUNT_ID:stateMachine:YOUR_STATE_MACHINE_NAME\"\r\n      }\r\n    ]\r\n  })\r\n}\r\n\r\nresource \"aws_iam_role_policy_attachment\" \"attach_policy\" {\r\n  role       = aws_iam_role.event_to_stepfunction.name\r\n  policy_arn = aws_iam_policy.stepfunction_policy.arn\r\n}\r\n<\/pre>\n<h3>3. Create Target for the cloudwatch event<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"dart\">\r\nresource \"aws_cloudwatch_event_target\" \"example\" {\r\n  rule      = aws_cloudwatch_event_rule.example.name\r\n  target_id = \"stepfunction-target\"\r\n  arn       = \"arn:aws:states:REGION:ACCOUNT_ID:stateMachine:YOUR_STATE_MACHINE_NAME\"\r\n\r\n  role_arn = aws_iam_role.event_to_stepfunction.arn\r\n}\r\n<\/pre>\n<h3>4. Apply the above Terraform Configuration <\/h3>\n<ul>\n<li>terraform init<\/li>\n<li>terraform plan<\/li>\n<li>terraform apply<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>For this, we have to do following Terraform Configuration: 1. Define the rule for CloudWatch event: resource &#8220;aws_cloudwatch_event_rule&#8221; &#8220;example&#8221; { name = &#8220;trigger-stepfunction&#8221; description = &#8220;Rule to trigger Step Functions state machine&#8221; event_pattern = jsonencode({ &#8220;source&#8221; : [&#8220;aws.s3&#8221;], &#8220;detail-type&#8221; : [&#8220;Object Created&#8221;], &#8220;detail&#8221; : { &#8220;bucket-name&#8221; : [&#8220;your-bucket-name&#8221;] } }) } &nbsp; 2. Create the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12031,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-12030","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12030"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=12030"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12030\/revisions"}],"predecessor-version":[{"id":12033,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12030\/revisions\/12033"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/12031"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=12030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=12030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=12030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}