Deployment of Customized EventBridge Rules Using Serverless Application Model and CloudFormation Template from CodePipeline
Tags: awseventbridge, codepipeline, codecommit, cf
Published: 2022-07-23 17:00:58

“ I have checked the documents of AWS to deploy customized eventbridge rules using SAM and Cloudformation template . Issue in creation of template as per requirement but after giving parameters in different ways and IAM permissions the problem is resolved . In terms of cost, the solution is cheaper and secure.”

Amazon EventBridge is a serverless event bus service that you can use to connect your applications with data from a variety of sources. EventBridge delivers a stream of real-time data from your applications, software as a service (SaaS) applications, and AWS services to targets such as AWS Lambda functions, HTTP invocation endpoints using API destinations, or event buses in other AWS accounts.

A rule matches incoming [events](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-events.html) and sends them to [targets](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-targets.html) for processing. A single rule can send an event to multiple targets, which then run in parallel. Rules are based either on an [event pattern](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html) or a schedule. An event pattern defines the event structure and the fields that a rule matches. Rules that are based on a schedule perform an action at regular intervals.

In this post, you will experience the deployment of customized eventbridge rules using serverless application model and cloudformation template from codepipeline. Here I have created a s3, kms, codecommit repo and codepipeline for deployment of eventbridge rules which is created using sam and cloudformation template parameters. Also created lambda and step function for target purpose.

#Prerequisites
You’ll need an AWS Step Functions for this post. [Getting started with AWS Step Functions](https://aws.amazon.com/step-functions/getting-started/) provides instructions on how to create a step function with multiple service inputs for flow perspective.

You’ll need an AWS Lambda for this post. [Getting started with aws Lambda](https://aws.amazon.com/lambda/getting-started/) provides instructions on how to create a lambda function. For this blog, I assume that I have a lambda function and a step function.

#Architecture Overview
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/50odxtww8whjqg9rrjdl.png)
The architecture diagram shows the overall deployment architecture with data flow, two aws account, code commit, code build, code deploy, code pipeline, s3, cloudformation, artifact, lambda, kms, step function and eventbridge rule.

#Solution overview
The blog post consists of the following phases:
1. In Account A, Create of S3, KMS, IAM, CodeCommit Repo and CodePipeline
2. In Account A, Create of BuildSpec.yml and Template.yaml File Which Contains Configuration Details for EventBridge Rules Using Serverless Application Model Template and Cloudformation Template Parameters
3. In Account B, Creation of EventBridge Rules via Cloudformation Stack from Pipeline of Account A and its ChangeSet Parameter

In Account B, I have a lambda function and a step function created as below →
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/czmz825aum5ozqnz59jh.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzlynnlalou4sw7y9qmc.png)

##Phase 1: In Account A, Create of S3, KMS, IAM, CodeCommit Repo and CodePipeline
1. Open the console for S3, KMS, IAM, CodeCommit Repo and CodePipeline manually or using cloudformation stack.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/go2z483an8z2mipaqno3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e4j5k3wnlwkwi2e8x7hc.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqcex437h1gb5k8hpbgs.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2iqranrwspxu5ubbbabt.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmqa1a2rf6h168uq4ole.png)

##Phase 2: In Account A, Create of BuildSpec.yml and Template.yaml File Which Contains Configuration Details for EventBridge Rules Using Serverless Application Model Template and Cloudformation Template Parameters 
1. Open the CodeCommit console, Create a buildspec file and template file. Template file contains customized parameters for creating event bridge rules of s3 as source and step function as target where lambda invoke in step function.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7euia6dicqedji2xhx1.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35vi1372iape9rfsptsj.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iuxynap2tpmpfrd6sy7o.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z8i7ewo7ttfwu0dlklx6.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r540yabipsl03zke8cdb.png)

##Phase 3: In Account B, Creation of EventBridge Rules via Cloudformation Stack from Pipeline of Account A and its ChangeSet Parameter
1. Once the changes deploy in the main branch of repo, the pipeline gets triggered and creates the eventbridge rules for step function as a target with lambda invoke.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qm6jmiqkh32unxwj1v11.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wqn4l1328jghk3kmkwqy.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mwmlfpdeafmv5ec7isnq.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odn0adi0o7alzuh4rb83.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6xppb1vvmymrgbvrn9w.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/243495z8ragxgsfm95pc.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qaru8qyj906vyjvbvsm.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1tqtpj1bdb508kdwfdl.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/apf9y30jsfpslcpvwnup.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usznxxzrnz4yu4zvnbnd.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjxmgttpm5xavw8ygqkl.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vegth5barqlrqixf8jh.png)

#Clean-up
In Account B: Delete of cloudformation stack, IAM, lambda and step function. In Account A: Delete of S3, KMS, IAM, codecommit repo and codepipeline.

#Pricing
I review the pricing and estimated cost of this example. 

Cost of CodeBuild = $0.0

Cost of CodeCommit = $0.0

Cost of Lambda = $0.0

Cost of Key Management Service = $0.01

Cost of Simple Storage Service = $1.0

Cost of Step Functions = $0.0

Cost of EventBridge Rules = $0.0

Total Cost = $(0.0 + 0.0 + 0.0 + 0.01 + 1.0 + 0.0 + 0.0) = $1.01

#Summary
In this post, I showed “how to deploy customized eventbridge rules using serverless application model and cloudformation template from codepipeline”.

For more details on Amazon EventBridge Rules, Checkout Get started Amazon EventBridge Rules, open the [Amazon EventBridge console](https://us-east-1.console.aws.amazon.com/events/home?region=us-east-1#/). To learn more, read the [Amazon EventBridge documentation](https://docs.aws.amazon.com/eventbridge/?id=docs_gateway).

Thanks for reading!

Connect with me: [Linkedin](https://www.linkedin.com/in/gargee-bhatnagar-6b7223114)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38sdi4nsp89fnui8h7dk.png)