{"id":1127,"date":"2019-05-25T15:04:12","date_gmt":"2019-05-25T12:04:12","guid":{"rendered":"https:\/\/artem.services\/?p=1121"},"modified":"2019-05-25T15:13:44","modified_gmt":"2019-05-25T12:13:44","slug":"aws-codecommit-d0-b8-codebuild-d1-81-d0-be-d0-b1-d0-b8-d1-d0-bc-b0-d0-b5-d0-b1-d1-80-d0-b0-d0-b7-d0-b8-d0-bf-d1-83-d1-88-d0-b0-d0-b5-d0-bc-d0-b5-d0-b3-d0-be-d0-b2-ecr","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=1127&lang=en","title":{"rendered":"AWS &#8212; CodeCommit and CodeBuild build Docker image and push it into ECR"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-214\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo.png\" alt=\"\" width=\"975\" height=\"450\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo.png 975w, https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo-300x138.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo-768x354.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo-954x440.png 954w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/><\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span title=\"\">In this example, we will consider creating a repository in <strong>CodeCommit<\/strong> and building a simple <strong>Docker<\/strong> image using <strong>CodeBuild<\/strong> and launching it in <strong>ECR<\/strong>.<\/span><\/p>\n<p><span class=\"\" title=\"\">Create a repository in <strong>CodeCommit<\/strong>.<\/span> <span title=\"\">My repository name is &quot;<strong>artem-test<\/strong>&quot;<\/span><\/span><\/p>\n<blockquote><p><span class=\"tlid-translation translation\" lang=\"en\"><span title=\"\">In order to work with the repository, make sure that your user has an <strong>SSH<\/strong> key loaded.<\/span> <span class=\"\" title=\"\">If it is already loaded, look at its <strong>ID<\/strong>, it will be needed.<\/span><\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">We clone our repository:<\/span><\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ngit clone ssh:\/\/YOUR_ID_FOR_SSH_KEY@git-codecommit.us-east-1.amazonaws.com\/v1\/repos\/artem-test\r\n<\/pre>\n<p>&nbsp;<\/p>\n<blockquote><p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Do not forget to change the region in which the repository is created.<\/span><\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Add to it for the test an example of a simple <\/span><\/span><strong><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Dockerfile<\/span><\/span><\/strong><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">.<\/span><\/span><\/p>\n<h3>Dockerfile:<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nFROM php:7.1-apache-jessie\r\n\r\nRUN apt update &amp;amp;&amp;amp; \\\r\n    apt install curl net-tools &amp;amp;&amp;amp; \\\r\n    apt-get clean\r\n\r\nCMD [&quot;apache2-foreground&quot;]\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">For the build we will use: <\/span><\/span><strong><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">buildspec.yml<\/span><\/span><\/strong><\/p>\n<h3>buildspec.yml:<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nversion: 0.2\r\n\r\nenv:\r\n  variables:\r\n    AWS_ACCOUNT_ID: &quot;XXXXXXXXXXXX&quot;\r\n    AWS_DEFAULT_REGION: &quot;us-east-1&quot;\r\n    IMAGE_REPO_NAME: &quot;artem-test&quot;\r\n    IMAGE_TAG: &quot;latest&quot;\r\n\r\nphases:\r\n  install:\r\n    runtime-versions:\r\n      docker: 18\r\n  pre_build:\r\n    commands:\r\n      - echo Logging in to Amazon ECR...\r\n      - $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)\r\n  build:\r\n    commands:\r\n      - echo Build started on `date`\r\n      - echo Building the Docker image...          \r\n      - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .\r\n      - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com\/$IMAGE_REPO_NAME:$IMAGE_TAG \r\n  post_build:\r\n    commands:\r\n      - echo Build completed on `date`\r\n      - echo Pushing the Docker image...\r\n      - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com\/$IMAGE_REPO_NAME:$IMAGE_TAG\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Send local changes to the server:<\/span><\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ngit add .\r\ngit commit -am &quot;git init&quot;\r\ngit push\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span title=\"\">Create a project in <strong>CodeBuild<\/strong>, specifying as a source repository in <strong>CodeCommit<\/strong>.<\/span><\/p>\n<p><span class=\"\" title=\"\">We give for <strong>CodeBuild<\/strong> rights in <strong>ECR<\/strong><\/span><\/p>\n<p><span class=\"\" title=\"\">Open <strong>IAM<\/strong> -&gt; <strong>CodeBuild<\/strong><\/span><\/p>\n<p><span class=\"\" title=\"\">Looking for &quot;<strong>codebuild-artem-test-service-role<\/strong>&quot;<\/span><\/p>\n<p><span class=\"\" title=\"\">And we add the following <strong>Policy<\/strong> to this role:<\/span><\/span><\/p>\n<pre>AmazonEC2ContainerRegistryPowerUser<\/pre>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">To display the build logs, go to the &quot;<strong>CloudWatch<\/strong>&quot; service and create a group.<\/span> <span class=\"\" title=\"\">You can also create an <strong>S3 Bucket<\/strong> to store log archives.<\/span><\/p>\n<p><span class=\"\" title=\"\">You can try to build an image in <strong>CodeBuild<\/strong>.<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this example, we will consider creating a repository in CodeCommit and building a simple Docker image using CodeBuild and launching it in ECR. Create a repository in CodeCommit. My repository name is &quot;artem-test&quot; In order to work with the repository, make sure that your user has an SSH key loaded. If it is already &hellip; <a href=\"https:\/\/artem.services\/?p=1127&#038;lang=en\" class=\"more-link\">\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0442\u044c<span class=\"screen-reader-text\"> &quot;AWS &#8212; CodeCommit and CodeBuild build Docker image and push it into ECR&quot;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[599,613],"tags":[543,873,875,877,823,879],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1127"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1127"}],"version-history":[{"count":2,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1127\/revisions"}],"predecessor-version":[{"id":1129,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1127\/revisions\/1129"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}