CodeCommit

  • Version control is the ability to understand the various changes that
    happened to the code over time (and possibly roll back).
  • All these are enabled by using a version control system such as Git
  • A Git repository can live on one’s machine, but it usually lives on a
    central online repository
  • Benefits are:
    • Collaborate with other developers
    • Make sure the code is backed-up somewhere
    • Make sure it’s fully viewable and auditable
  • Git repositories can be expensive.
  • The industry includes:
    • GitHub: free public repositories, paid private ones
    • BitBucket
    • Etc…
  • And AWS CodeCommit:
    • private Git repositories
    • No size limit on repositories (scale seamlessly)
    • Fully managed, highly available
    • Code only in AWS Cloud account => increased security and compliance
    • Secure (encrypted, access control, etc…)
    • Integrated with Jenkins / CodeBuild / other CI tools

Credentials for connecting to the CodeCommit we can find at:

IAM -> users -> user_name -> Security Credentials -> HTTPS Git credentials for AWS CodeCommit -> Generate credentials

To create a repository we go to:

CodeCommit -> Create Repository -> repository name -> Create

To get an URL to repo we click on Clone HTTPS.

On linux instance install git:

Configure git

First commit

 

W need to change index.html file and then second commit

Changing the index.html and changing a branch to “my-feature”.

If we want to put some new features to the master branch we should merge branches on AWS console by pull request:

CodeCommit -> pull request -> create pull request -> Destination -> Source -> Compare -> Title -> Create pull request -> Merge (if we happy with the changes) -> Merge pull request

New branch with new features will be merged with master branch and new branch will be deleted from CodeCommit.

Junior developers should not have ability to push to the master branch.

In AWS console:

1. Create a group junior-devs
2. Add inline policy for this group
Copy from
https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-conditional-branch.html
And paste to:
IAM -> groups -> junior-devs -> inline policies -> click here ->
-> custom policy -> select -> paste to Policy Dokument -> policy name = CannotPushToMasterInCodecommit -> ApplyPolicy

Modify the policy:

3. Add user to the junior-devs group

After user is added to the junior-devs group with attached policy he can’t push to the master branch:
# switch to master branch

 

Triggers and notifications

To create a notification we click on:

Repostitory_name -> Settings -> Notifications -> Create Notification rule -> Notification name -> Events that trigger notification -> Create target -> Tarhet type (SNS Topic) -> Topic Name -> Create -> Submmit

To create trigger:

Repostitory_name -> Settings -> Triggers -> Create trigger -> Trigger name -> Events -> Push to existing branch -> Service details -> choose Amazon SNS -> SNS Topic (choose name) -> Create trigger

After creating a trigger we can see our rule in the CloudWatch -> Events -> Rules.This one CloudWatch notification rule (awscodestarnotifications-rule) is what allows for all notification rules in CodeCommit.

To create a rrule in CloudWatch:

Events->Rules -> Create rule -> Service Name (CodeCommit) -> Event Type (for ex. Repository State Change)

Targets -> SNS Topic -> Topic -> name

By creating a notification rules in CodeCommit we can automate whatever is happening in our repository stright to automation pipeline which is SNS, SQS, Lambda etc. From CodeCommit you are able to setup notification, triggers and cloud watch events rules to build some automation directly to SNS, Lambda etc.

 

Lambda

We will create a function from scratch:

Lambda -> Create function -> Function name (lambda-codecommit) -> Runtime (Python 2.7) -> Change default execution role -> Execution role -> Create a new role with basic Lambda permissions -> Create function

Creating a trigger:

Lambda -> Functions -> lambda-codecommit -> Add trigger -> TriggerConfiguration -> CodeCommit -> Repository name (my-webpage) -> Trigger name (MyLambdaTrigger) -> Events (All repository events) -> Branch names (All branches) -> Add

Let’s add some code. To do that refresh the lambda page.

From refrence link (6) copy code of lambda for python:

Now click on Deploy and then Test.

Event template (AWS Code Commit Repository) -> Event name -> MySampleCodeCommit -> Create

Now click again Test. There is an error in test but everything is ok.

 

 

Reference links: