Last updated on June 5, 2023
Amazon Web Services, with its ever-growing breadth of services, offers a fully-managed version control system where developers can privately store their application source code like Github or Bitbucket. CodeCommit can be used as a staging ground coupled with CodeDeploy and CodePipeline to seamlessly deploy code to Amazon EC2 instances.
In this article, I will discuss how we can leverage Amazon SNS to send notifications whenever there are events in our repository. This is particularly useful when we need to keep our work colleagues up-to-date if there are events in our repository.
Triggers are used for two things. The first is for notifying users of an event that occurs in our repository by using a simple email notification. Second is by triggering a function to allow us to interact with third-party applications such as Jenkins or other continuous integration and deployment services.
To get started, we need to create an SNS topic (TutorialsDojoTopic) and subscription for the email notification. AWS will send a confirmation email to the subscribed email address.
Create a topic:
Amazon SNS > Topics > Create topic
In order for Codecommit Notifications to have the necessary permissions to publish emails, you need to modify the Access Policy with the following:
{ "Version": "2008-10-17", "Statement": [ { "Sid": "CodeNotification_publish", "Effect": "Allow", "Principal": { "Service": "codestar-notifications.amazonaws.com" }, "Action": "SNS:Publish", "Resource": "arn:aws:sns:us-east-1:<ACCOUNT_ID>:<SNS_TOPIC_NAME>" } ] }
Do not forget to change the account ID and SNS topic name. Remove the < and > characters once done.
After creating our SNS topic, we need a subscription. The purpose of the subscription is it specifies what the SNS topic will do when it is triggered to a specified endpoint like Lambda, SQS, or an email address.
Create a subscription, navigate to:
Amazon SNS > Subscriptions > Create Subscriptions
After you have created an SNS topic and subscription, head over to Codecommit. Here we have already created the TutorialsDojoRepo repository with a file named TutorialsDojo.txt. You can manually create or upload a file to our repository by heading over to the “Add File” section.
To create our Notifications, go to Settings > Notifications > Create Notifications
To determine if the connection between CodeCommit and the SNS topic works, head over to the Notification Rule that you just created then navigate at the bottom. Under Notification Rule Targets, the status must be Active.
Now let us create a scenario wherein a colleague accidentally deletes a branch. To achieve our goal, we must receive an email from AWS stating that a branch has been deleted.
Sources:
https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-notify.html
https://docs.aws.amazon.com/sns/latest/dg/sns-tutorial-create-topic.html
https://docs.aws.amazon.com/sns/latest/dg/sns-tutorial-create-subscribe-endpoint-to-topic.html