Creating an AWS CodeCommit repository that triggers email notifications via Amazon EventBridge and AWS Simple Notification Service
Introduction
AWS CodeCommit is a fully managed source control service that enables teams to host secure and scalable Git repositories. In many scenarios, teams need real-time notifications when changes occur in a repository. AWS Simple Notification Service (SNS) provides a seamless way to send email notifications when events like commits, merges, or branch deletions happen in CodeCommit.
In this guide, we will walk through the process of setting up an AWS CodeCommit repository that triggers email notifications via AWS SNS.
Prerequisites
Before starting, ensure you have:
An AWS account with necessary permissions.
AWS CLI installed and configured.
A valid email address to receive notifications.
Step 1: Create an AWS CodeCommit Repository
Navigate to the AWS Management Console.
Open AWS CodeCommit.
Click Create Repository and provide:
Repository name (e.g.,
CodeCommitNotify
).Description (optional).
Click Create to complete the setup.
Step 2: Create an AWS SNS Topic for Notifications
Navigate to AWS SNS in the AWS Console.
Click Create Topic and select Standard as the type.
Provide a name (e.g.,
CodeCommitNotify
).Click Create Topic.
Step 3: Subscribe an Email Address to the SNS Topic
In the SNS console, go to the CodeCommitNotify topic.
Click Create Subscription.
Set Protocol to Email.
Enter the recipient email address for notifications.
Click Create Subscription.
Open your email inbox and confirm the subscription from the AWS verification email.
Step 4: Configure your event and target with Amazon EventBridge
Your active subscription is going to send the notifications through an event. So let us not create ourselves an event in the AWS EventBridge. Let us now navigate to AWS EventBridge.
Go to Amazon EventBridge >> Create an EventBridge Rule >> Create Rule.
Name = CodeCommitRule, Event Bus type = Default, Rule Type = Rule with an event pattern.
Select Event Source as AWS events or EventBridge partner events.
Select the Creation method as Use pattern form.
Event Source = AWS Services, AWS Service = CodeCommit.
Event type = All events, Click Next.
Select Target type as AWS Service, Select Target as SNS topic.
Select the SNS Topic that you created in the earlies steps "CodeCommitNotify".
Click Next >> Next >> Create Rule.
Step 5: Test the Setup
We are now set to test the Notification email trigger for a commit event to the CodeCommit repository. Clone the CodeCommit repository locally:
git clone https://git-codecommit.<region>.amazonaws.com/v1/repos/CodeCommitNotify
Navigate into the repository folder and create a test file:
cd CodeCommitNotify echo "Hello AWS CodeCommit" > test.txt git add test.txt git commit -m "Initial commit" git push origin main
Check your email inbox for a notification from AWS SNS.
You may also commit changes to repository by uploading simple test file.
Click Add File >> Upload file >> Choose file Select your test file >> open
In the commit changes to main section, enter your name for the Author and an email address.
You may also enter your commit message for documnetation purpose >> Click Commit changes.
Conclusion
By integrating AWS CodeCommit with SNS and Amazon EventBridge, you can efficiently monitor repository changes and receive real-time notifications. This setup is especially useful for DevOps teams looking to track commits, merges, and branch changes in their AWS development workflow.
If you have enjoyed reading this, please share with the community! ๐