{"id":3839,"date":"2021-02-03T17:15:16","date_gmt":"2021-02-03T16:15:16","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=3839"},"modified":"2021-05-20T16:11:36","modified_gmt":"2021-05-20T14:11:36","slug":"aws-codecommit","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2021\/02\/03\/aws-codecommit\/","title":{"rendered":"CodeCommit"},"content":{"rendered":"<p><!--more--><\/p>\n<ul>\n<li>Version control is the ability to understand the various changes that<br \/>\nhappened to the code over time (and possibly roll back).<\/li>\n<li>All these are enabled by using a version control system such as Git<\/li>\n<li>A Git repository can live on one\u2019s machine, but it usually lives on a<br \/>\ncentral online repository<\/li>\n<li>Benefits are:\n<ul>\n<li>Collaborate with other developers<\/li>\n<li>Make sure the code is backed-up somewhere<\/li>\n<li>Make sure it\u2019s fully viewable and auditable<\/li>\n<\/ul>\n<\/li>\n<li>Git repositories can be expensive.<\/li>\n<li>The industry includes:\n<ul>\n<li>GitHub: free public repositories, paid private ones<\/li>\n<li>BitBucket<\/li>\n<li>Etc&#8230;<\/li>\n<\/ul>\n<\/li>\n<li>And AWS CodeCommit:\n<ul>\n<li>private Git repositories<\/li>\n<li>No size limit on repositories (scale seamlessly)<\/li>\n<li>Fully managed, highly available<\/li>\n<li>Code only in AWS Cloud account =&gt; increased security and compliance<\/li>\n<li>Secure (encrypted, access control, etc\u2026)<\/li>\n<li>Integrated with Jenkins \/ CodeBuild \/ other CI tools<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Credentials for connecting to the CodeCommit we can find at:<\/p>\n<p><code>IAM -&gt; users -&gt; user_name -&gt; Security Credentials -&gt; <\/code><span class=\"ng-binding ng-scope\"><code>HTTPS Git credentials for AWS CodeCommit -&gt; Generate credentials<\/code><br \/>\n<\/span><\/p>\n<p>To create a repository we go to:<\/p>\n<p><code>CodeCommit -&gt; Create Repository -&gt; repository name -&gt; Create<\/code><\/p>\n<p>To get an URL to repo we click on <code>Clone HTTPS<\/code>.<\/p>\n<p>On linux instance install git:<\/p>\n<pre class=\"lang:default decode:true \">yum -y install git<\/pre>\n<p>Configure git<\/p>\n<pre class=\"lang:default decode:true\">git config --global user.email your@email<\/pre>\n<p>First commit<\/p>\n<pre class=\"lang:default decode:true\">git status\r\ngit add .\r\ngit commit -m \"first commit\"\r\ngit push origin master<\/pre>\n<p>&nbsp;<\/p>\n<p>W need to change<code> index.html<\/code> file and then second commit<\/p>\n<pre class=\"lang:default decode:true \">git status\r\ngit add index.html\r\ngit commit -m \"Modified index.html to v2\"\r\ngit config --global user.email mborodziuk@gmail.com\r\ngit push origin master<\/pre>\n<p>Changing the <code>index.html<\/code> and changing a branch to &#8220;my-feature&#8221;.<\/p>\n<pre class=\"lang:default decode:true \">git checkout -b my-feature\r\ngit status\r\n# On branch master\r\n# Changes not staged for commit:\r\n# (use \"git add &lt;file&gt;...\" to update what will be committed)\r\n# (use \"git checkout -- &lt;file&gt;...\" to discard changes in working directory)\r\n#\r\n# modified: index.html\r\n#\r\nno changes added to commit (use \"git add\" and\/or \"git commit -a\")\r\n\r\ngit add .\r\ngit commit -m \"modified index.html to v3\"\r\ngit push --set-upstream origin my-feature\r\nUsername for 'https:\/\/git-codecommit.eu-central-1.amazonaws.com': stephane-at-840037588702\r\nPassword for 'https:\/\/stephane-at-840037588702@git-codecommit.eu-central-1.amazonaws.com':<\/pre>\n<p>If we want to put some new features to the master branch we should merge branches on AWS console by pull request:<\/p>\n<p><code>CodeCommit -&gt; pull request -&gt; create pull request -&gt; Destination -&gt; Source -&gt; Compare -&gt; Title -&gt; Create pull request -&gt; Merge (if we happy with the changes) -&gt; Merge pull request<\/code><\/p>\n<p>New branch with new features will be merged with master branch and new branch will be deleted from CodeCommit.<\/p>\n<div class=\"main-content-wrapper\">\n<div class=\"main-content\">\n<div class=\"ud-app-loader ud-component--course-taking--app w100p h100p ud-app-loaded\" data-module-id=\"course-taking\" data-module-args=\"{&quot;courseId&quot;:2533568,&quot;isUserInstructor&quot;:false,&quot;useCache&quot;:true,&quot;isPreviewingAsStudent&quot;:false,&quot;hasDismissedReviewPrompt&quot;:false,&quot;translationLocale&quot;:null,&quot;initialCurriculumItemId&quot;:16049778,&quot;canUserEditCourse&quot;:false,&quot;initialCurriculumItemType&quot;:&quot;lecture&quot;,&quot;availableFeatures&quot;:[&quot;q_and_a_enabled&quot;,&quot;certificate&quot;],&quot;canManageCourseQA&quot;:false}\">\n<div class=\"has-sidebar \">\n<div class=\"app--column-container--3AItG\">\n<div class=\"app--content-column--HC_i1\">\n<div class=\"app--sidebar-column--2t0E8\">\n<div data-purpose=\"sidebar\">\n<div class=\"sidebar--content---4z0-\">\n<div data-purpose=\"curriculum-section-container\">\n<div class=\"section--section--BukKG\" aria-expanded=\"true\" data-purpose=\"section-panel-2\">\n<div class=\"item-link item-link--common--RP3fp item-link--default-theme--YqsPR\" tabindex=\"0\" role=\"link\" aria-label=\"Zatrzymaj wyk\u0142ad 10. CodeCommit - Securing the Repository and Branches. Czas trwania 6 min\" data-purpose=\"curriculum-item-2-6\">\n<div class=\"curriculum-item-link--item-container--1ptOz\">\n<div><\/div>\n<div><\/div>\n<div class=\"curriculum-item-link--title--zI5QT\"><span style=\"color: #3366ff;\">Securing the Repository and Branches<\/span><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Junior developers should not have ability to push to the master branch.<\/p>\n<p>In AWS console:<\/p>\n<p>1. Create a group<code> junior-devs<\/code><br \/>\n2. Add inline policy for this group<br \/>\nCopy from<br \/>\n<a href=\"https:\/\/docs.aws.amazon.com\/codecommit\/latest\/userguide\/how-to-conditional-branch.html\">https:\/\/docs.aws.amazon.com\/codecommit\/latest\/userguide\/how-to-conditional-branch.html<\/a><br \/>\nAnd paste to:<br \/>\n<code>IAM -&gt; groups -&gt; junior-devs -&gt; inline policies -&gt; click here -&gt;<\/code><br \/>\n<code>-&gt; custom policy -&gt; select -&gt; paste to Policy Dokument -&gt; policy name = CannotPushToMasterInCodecommit -&gt; ApplyPolicy<\/code><\/p>\n<p>Modify the policy:<\/p>\n<pre class=\"lang:default decode:true \">{\r\n\"Version\": \"2012-10-17\",\r\n\"Statement\": [\r\n{\r\n\"Effect\": \"Deny\",\r\n\"Action\": [\r\n\"codecommit:GitPush\",\r\n\"codecommit:DeleteBranch\",\r\n\"codecommit:PutFile\",\r\n\"codecommit:MergeBranchesByFastForward\",\r\n\"codecommit:MergeBranchesBySquash\",\r\n\"codecommit:MergeBranchesByThreeWay\",\r\n\"codecommit:MergePullRequestByFastForward\",\r\n\"codecommit:MergePullRequestBySquash\",\r\n\"codecommit:MergePullRequestByThreeWay\"\r\n],\r\n\"Resource\": \"arn:aws:codecommit:*:*:*\",\r\n\"Condition\": {\r\n\"StringEqualsIfExists\": {\r\n\"codecommit:References\": [\r\n\"refs\/heads\/master\"\r\n]\r\n},\r\n\"Null\": {\r\n\"codecommit:References\": false\r\n}\r\n}\r\n}\r\n]\r\n}<\/pre>\n<p>3. Add user to the junior-devs group<\/p>\n<p>After user is added to the junior-devs group with attached policy he can&#8217;t push to the master branch:<br \/>\n# switch to master branch<\/p>\n<pre class=\"lang:default decode:true \">git checkout -f master\r\n\r\nmc (editing index.html file)\r\n\r\ngit status\r\n# On branch master\r\n# Changes not staged for commit:\r\n# (use \"git add &lt;file&gt;...\" to update what will be committed)\r\n# (use \"git checkout -- &lt;file&gt;...\" to discard changes in working directory)\r\n#\r\n# modified: index.html\r\n#\r\nno changes added to commit (use \"git add\" and\/or \"git commit -a\")\r\n\r\n[root@miro my-webpage]# git add index.html\r\n\r\n[root@miro my-webpage]# git commit -m \"modified index to 4\"\r\n[master eccfeda] modified index to 4\r\n1 file changed, 1 insertion(+), 1 deletion(-)\r\n[root@miro my-webpage]# git push origin master\r\nUsername for 'https:\/\/git-codecommit.eu-central-1.amazonaws.com': stephane-at-840037588702\r\nPassword for 'https:\/\/stephane-at-840037588702@git-codecommit.eu-central-1.amazonaws.com':\r\nCounting objects: 5, done.\r\nDelta compression using up to 2 threads.\r\nCompressing objects: 100% (3\/3), done.\r\nWriting objects: 100% (3\/3), 289 bytes | 0 bytes\/s, done.\r\nTotal 3 (delta 2), reused 0 (delta 0)\r\nTo https:\/\/git-codecommit.eu-central-1.amazonaws.com\/v1\/repos\/my-webpage\r\n! [remote rejected] master -&gt; master (You don't have permission to push changes to this branch.)\r\nerror: failed to push some refs to 'https:\/\/git-codecommit.eu-central-1.amazonaws.com\/v1\/repos\/my-webpage'<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #3366ff;\">Triggers and notifications<\/span><\/p>\n<p>To create a notification we click on:<\/p>\n<p><code>Repostitory_name -&gt; Settings -&gt; Notifications -&gt; Create Notification rule -&gt; Notification name -&gt; Events that trigger notification -&gt; Create target -&gt; Tarhet type (SNS Topic) -&gt; Topic Name -&gt; Create -&gt; Submmit<\/code><\/p>\n<p>To create trigger:<\/p>\n<p><code>Repostitory_name -&gt; Settings -&gt; Triggers -&gt; Create trigger -&gt; Trigger name -&gt; Events -&gt; Push to existing branch -&gt; Service details -&gt; choose Amazon SNS -&gt; SNS Topic (choose name) -&gt; Create trigger<\/code><\/p>\n<p>After creating a trigger we can see our rule in the <code>CloudWatch -&gt; Events -&gt; Rules.<\/code>This one CloudWatch notification rule <code>(<\/code><code>awscodestarnotifications-rule<\/code>) is what allows for all notification rules in CodeCommit.<\/p>\n<p>To create a rrule in CloudWatch:<\/p>\n<p><code>Events-&gt;Rules -&gt; Create rule -&gt; Service Name (CodeCommit) -&gt; Event Type (for ex. Repository State Change)<\/code><\/p>\n<p><code>Targets -&gt; SNS Topic -&gt; Topic -&gt; name<\/code><\/p>\n<p>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.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #3366ff;\">Lambda<\/span><\/p>\n<p>We will create a function from scratch:<\/p>\n<p><code>Lambda -&gt; Create function -&gt; Function name (lambda-codecommit) -&gt; Runtime (Python 2.7) -&gt; Change default execution role -&gt; Execution role -&gt; Create a new role with basic Lambda permissions -&gt; Create function<\/code><\/p>\n<p>Creating a trigger:<\/p>\n<p><code>Lambda -&gt; Functions -&gt; <span class=\"awsui-breadcrumb-link-text\">lambda-codecommit -&gt; Add trigger -&gt; TriggerConfiguration -&gt; CodeCommit -&gt; Repository name (my-webpage) -&gt; Trigger name (MyLambdaTrigger) -&gt; Events (All repository events) -&gt; Branch names (All branches) -&gt; Add<\/span><\/code><\/p>\n<p>Let&#8217;s add some code. To do that refresh the lambda page.<\/p>\n<p>From refrence link (6) copy code of lambda for python:<\/p>\n<pre class=\"lang:default decode:true\">import json\r\nimport boto3\r\n\r\ncodecommit = boto3.client('codecommit')\r\n\r\ndef lambda_handler(event, context):\r\n#Log the updated references from the event\r\nreferences = { reference['ref'] for reference in event['Records'][0]['codecommit']['references'] }\r\nprint(\"References: \" + str(references))\r\n\r\n#Get the repository from the event and show its git clone URL\r\nrepository = event['Records'][0]['eventSourceARN'].split(':')[5]\r\ntry:\r\nresponse = codecommit.get_repository(repositoryName=repository)\r\nprint(\"Clone URL: \" +response['repositoryMetadata']['cloneUrlHttp'])\r\nreturn response['repositoryMetadata']['cloneUrlHttp']\r\nexcept Exception as e:\r\nprint(e)\r\nprint('Error getting repository {}. Make sure it exists and that your repository is in the same region as this function.'.format(repository))\r\nraise e<\/pre>\n<p>Now click on Deploy and then Test.<\/p>\n<p><code>Event template (AWS Code Commit Repository) -&gt; Event name -&gt; MySampleCodeCommit -&gt; Create<\/code><\/p>\n<p>Now click again Test. There is an error in test but everything is ok.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Reference links:<\/strong><\/p>\n<div class=\"main-content-wrapper\">\n<div class=\"main-content\">\n<div class=\"ud-app-loader ud-component--course-taking--app w100p h100p ud-app-loaded\" data-module-id=\"course-taking\" data-module-args=\"{&quot;courseId&quot;:2533568,&quot;isUserInstructor&quot;:false,&quot;useCache&quot;:true,&quot;isPreviewingAsStudent&quot;:false,&quot;hasDismissedReviewPrompt&quot;:false,&quot;translationLocale&quot;:null,&quot;initialCurriculumItemId&quot;:16049778,&quot;canUserEditCourse&quot;:false,&quot;initialCurriculumItemType&quot;:&quot;lecture&quot;,&quot;availableFeatures&quot;:[&quot;q_and_a_enabled&quot;,&quot;certificate&quot;],&quot;canManageCourseQA&quot;:false}\">\n<div class=\"has-sidebar \">\n<div class=\"app--column-container--3AItG\">\n<div class=\"app--content-column--HC_i1\">\n<div class=\"app--row--1ydzX app--body-container--10gJo\">\n<div class=\"app--row-content--1lH7B\">\n<div class=\"app--curriculum-item--2GBGE\">\n<div class=\"curriculum-item-view--scaled-height-limiter--1j3Pp\">\n<div class=\"curriculum-item-view--absolute-height-limiter--1SMqE\">\n<div class=\"curriculum-item-view--aspect-ratio-container--2tJ-p\">\n<div class=\"curriculum-item-view--content-container--2MIL1\">\n<div class=\"curriculum-item-view--scaled-height-limiter--1j3Pp\">\n<div class=\"curriculum-item-view--absolute-height-limiter--1SMqE\">\n<div class=\"curriculum-item-view--content--3ABmp\" data-purpose=\"curriculum-item-viewer-content\">\n<div class=\"lecture-view--container--pL22J\" tabindex=\"0\" role=\"region\" aria-label=\"Sekcja: 3: SDLC Automation (Domain 1), Wyk\u0142ad: 5: Reference Links for Domain 1 - AWS Certified DevOps Engineer Professional\">\n<div class=\"text-viewer--scroll-container--1iy0Z\">\n<div class=\"text-viewer--container--18Ayx\">\n<div class=\"text-viewer--content--3hoqQ\">\n<div class=\"p-space-md\">\n<div class=\"article-asset--content--1dAQ9\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ol>\n<li><a href=\"https:\/\/www.atlassian.com\/git\/tutorials\/using-branches\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.atlassian.com\/git\/tutorials\/using-branches<\/a><\/li>\n<li><a href=\"https:\/\/docs.aws.amazon.com\/codecommit\/latest\/userguide\/auth-and-access-control-iam-identity-based-access-control.html\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/docs.aws.amazon.com\/codecommit\/latest\/userguide\/auth-and-access-control-iam-identity-based-access-control.html<\/a><\/li>\n<li><a href=\"https:\/\/aws.amazon.com\/blogs\/devops\/refining-access-to-branches-in-aws-codecommit\/\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/aws.amazon.com\/blogs\/devops\/refining-access-to-branches-in-aws-codecommit\/<\/a><\/li>\n<li><a href=\"https:\/\/docs.aws.amazon.com\/codecommit\/latest\/userguide\/how-to-notify.html\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/docs.aws.amazon.com\/codecommit\/latest\/userguide\/how-to-notify.html<\/a><\/li>\n<li><a href=\"https:\/\/docs.aws.amazon.com\/codecommit\/latest\/userguide\/how-to-repository-email.html\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/docs.aws.amazon.com\/codecommit\/latest\/userguide\/how-to-repository-email.html<\/a> )<\/li>\n<li><a href=\"https:\/\/docs.aws.amazon.com\/codecommit\/latest\/userguide\/how-to-notify-lambda.html\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/docs.aws.amazon.com\/codecommit\/latest\/userguide\/how-to-notify-lambda.html<\/a><\/li>\n<li><a href=\"https:\/\/docs.aws.amazon.com\/codecommit\/latest\/userguide\/how-to-migrate-repository-existing.html\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/docs.aws.amazon.com\/codecommit\/latest\/userguide\/how-to-migrate-repository-existing.html<\/a><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":3840,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/3839"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=3839"}],"version-history":[{"count":25,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/3839\/revisions"}],"predecessor-version":[{"id":4022,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/3839\/revisions\/4022"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media\/3840"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=3839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=3839"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=3839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}