How To Create A Trigger In AWS CodeCommit Repository Using AWS CLI
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed how to create an AWS CodeCommit approver rule template using AWS CLI.
https://cloudaffaire.com/how-to-create-an-aws-codecommit-approver-rule-template-using-aws-cli/
In this blog post, we will discuss how to create a trigger in AWS CodeCommit repository using AWS CLI.
What Is Trigger In AWS CodeCommit Repository:
You can configure a CodeCommit repository so that code pushes or other events trigger actions, such as sending a notification from Amazon Simple Notification Service (SNS) or invoking a function in AWS Lambda. You can create up to 10 triggers for each CodeCommit repository. You can configure an SNS notification trigger to get an email notification every time someone pushes to the repository. You can also create a lambda trigger to notify an external build system to start a build after someone pushes to the main branch of the repository. In this demo, we will configure the SNS trigger using AWS CLI.
How To Create A Trigger In AWS CodeCommit Repository Using AWS CLI:
Requirements:
AWS CLI v2 installed and configured.
https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html
Git installed and configured.
https://www.atlassian.com/git/tutorials/install-git
Step 1: Create a new AWS CodeCommit repository.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
########################################################### ## How To Create A Trigger For AWS CodeCommit Repository ## ########################################################### ## I am using a Linux shell to execute AWS CLI commands ## ---------------------------------- ## Create a new CodeCommit repository ## ---------------------------------- ## create a directory for this demo mkdir codecmitdemo && cd codecmitdemo ## create a codecommit repository and upload a file aws codecommit create-repository \ --repository-name "my_aws_repo" \ --repository-description "CloudAffaire test repository" \ --tags "Key=Name,Value=MYAPP" ## upload a file in the codecommit repository echo "aws" > myfile.txt && AWS_CODECMIT_COMMIT_ID1=$(aws codecommit put-file \ --repository-name "my_aws_repo" \ --branch-name "master" \ --file-content fileb://myfile.txt \ --file-path myfile.txt \ --name "Debjeet" \ --email "debjeet@cloudaffaire.com" \ --commit-message "debjeet added myfile.txt" \ --query 'commitId' \ --output text) |
Step 2: Create a new SNS topic with an email subscription.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
## ---------------------------------------------- ## Create an SNS topic with an email subscription ## ---------------------------------------------- ## create a sns topic AWS_SNS_TOPIC_ARN=$(aws sns create-topic \ --name "my_sns_topic" \ --tags "Key=Name,Value=MYAPP" \ --query 'TopicArn' \ --output text) ## create a sns topic email subscription aws sns subscribe \ --topic-arn $AWS_SNS_TOPIC_ARN \ --protocol email \ --notification-endpoint cloudaffaire@gmail.com ## an email will be sent to the given email id for subscription confirmation ## confirm the same by clicking on the link given in the mail |
You will get an email notification to confirm your subscription. Click on the ‘Confirm subscription’.
Step 3: Create an SNS trigger for your CodeCommit repository.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
## ------------------------------- ## Create a CodeCommit SNS trigger ## ------------------------------- ## create the trigger definition cat < { "repositoryName": "my_aws_repo", "triggers": [ { "destinationArn": "$AWS_SNS_TOPIC_ARN", "branches": [ "master" ], "name": "MySNSTrigger", "customData": "SNS Trigger For CodeCommit", "events": [ "all" ] } ] } EOF ## create the trigger aws codecommit put-repository-triggers \ --repository-name "my_aws_repo" \ --cli-input-json file://mytriggerdef.json ## get trigger details aws codecommit get-repository-triggers \ --repository-name "my_aws_repo" ## do a test trigger aws codecommit test-repository-triggers \ --repository-name "my_aws_repo" \ --cli-input-json file://mytriggerdef.json |
Step 4: Test if the trigger is working by pushing a change in your repository.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## ----------------------------------- ## Test if the trigger is working fine ## ----------------------------------- ## upload a file in the codecommit repository echo "gcp" >> myfile.txt && AWS_CODECMIT_COMMIT_ID2=$(aws codecommit put-file \ --repository-name "my_aws_repo" \ --branch-name "master" \ --file-content fileb://myfile.txt \ --file-path myfile.txt \ --name "Debjeet" \ --email "debjeet@cloudaffaire.com" \ --commit-message "debjeet updated myfile.txt" \ --parent-commit-id "$AWS_CODECMIT_COMMIT_ID1" \ --query 'commitId' \ --output text) |
You will get an email notification in your email.
Step 5: Cleanup.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
## ------- ## cleanup ## ------- ## delete your codecommit repository aws codecommit delete-repository \ --repository-name "my_aws_repo" ## delete sns subscription AWS_SNS_SUS_ARN=$(aws sns list-subscriptions-by-topic \ --topic-arn "$AWS_SNS_TOPIC_ARN" \ --query 'Subscriptions[0].SubscriptionArn' \ --output text) && aws sns unsubscribe \ --subscription-arn $AWS_SNS_SUS_ARN ## delete sns topic aws sns delete-topic \ --topic-arn $AWS_SNS_TOPIC_ARN ## delete demo directory cd .. && rm -rf codecmitdemo |
Hope you have enjoyed this blog post. To get more details on AWS CodeCommit, please refer below AWS documentation
https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html
https://docs.aws.amazon.com/cli/latest/reference/codecommit/index.html
CodeCommit is a Git based version control system and you need to have basic understanding of Git in order to work with CodeCommit. You can follow below link to get a basic understanding of Git.
https://cloudaffaire.com/category/devops/git/