{"id":4495,"date":"2022-11-19T00:47:00","date_gmt":"2022-11-19T06:47:00","guid":{"rendered":"https:\/\/www.supportpro.com\/blog\/?p=4495"},"modified":"2022-11-21T11:18:02","modified_gmt":"2022-11-21T17:18:02","slug":"local-caching-in-aws-codebuild","status":"publish","type":"post","link":"https:\/\/www.supportpro.com\/blog\/local-caching-in-aws-codebuild\/","title":{"rendered":"Local Caching in AWS CodeBuild"},"content":{"rendered":"\n<p><span style=\"font-weight: 400;\"><strong>AWS CodeBuild <\/strong>is a completely overseen persistent incorporation administration. You simply indicate the area of your source code, pick your construct settings, and CodeBuild will run manufacture contents for accumulating, testing, and bundling your code. There are no servers for provisioning and scaling purposes, or software for installation, configuration, and operation.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Build Caching&nbsp;<\/b><b>in<\/b><b>&nbsp;CodeBuild<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">A cache can store reusable bits of your build environment and use them over various build. By using a cache, you can save more time in the project build setup.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">AWS CodeBuild now enables you to store antiques locally on the build with little cost as well as quicker reuse. You could just utilize Amazon S3 as the cache to store your ancient rarities between builds. Despite the fact that S3 caching permits the reuse of relics, it accompanies a network cost and inactivity because of the related file transfer.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Now AWS CodeBuild supports Local caching, which makes it feasible for you to continue intermediate build artifacts locally on the build host so that they are accessible for reuse resulting in build runs.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Two types of caching<\/b><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><b>1) Amazon S3 Caching<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">It stores the cache in an <a href=\"https:\/\/www.supportpro.com\/blog\/amazon-s3-glacier\/\">Amazon S3 <\/a>bucket that is accessible over different build hosts. This is a decent choice for small intermediate build vestige that is more costly to build than to download. This isn&#8217;t the best alternative for enormous build artifacts since they can set aside a long time to transfer over your network, which is depended on the build performance.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><b>2) Local Caching<\/b><span style=\"font-weight: 400;\">&nbsp;<\/span><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Local caching stores a cache locally on a host that is accessible to the build host only. This is a perfect and alternative option for build artifacts since the cache is immediately available on the build host. This implies that build performance is not impacted by network transfer time. Also, do note that if you choose to build, performance is not impacted by network transfer time. While choosing local caching, you have to choose one or more of the following cache modes.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Local caching increases build performance for:<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;Projects with a large, monolithic source code repository.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;Projects that create and reuse many intermediate build artifacts.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;Projects that build large Docker images.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;Projects with many source dependencies.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>How to use Codebuild local caching<\/b><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild.jpg\" data-rel=\"penci-gallery-image-content\" ><img fetchpriority=\"high\" decoding=\"async\" width=\"1000\" height=\"1000\" src=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild.jpg\" alt=\"\" class=\"wp-image-4508\" srcset=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild.jpg 1000w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild-150x150.jpg 150w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild-300x300.jpg 300w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild-768x768.jpg 768w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild-80x80.jpg 80w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild-144x144.jpg 144w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/a><\/figure>\n\n\n\n<ol class=\"wp-block-list\"><li><span style=\"font-weight: 400;\"> Open AWS CodeBuild console at https:\/\/console.aws.amazon.com\/codesuite\/codebuild\/home.<\/span><\/li><li><span style=\"font-weight: 400;\"> Select Create project.<\/span><\/li><li><span style=\"font-weight: 400;\"> In Project configuration, enter a name and description for the build project.<\/span><\/li><li><span style=\"font-weight: 400;\"> In Source, for Source provider, select the source code provider type. We use an AWS CodeCommit repository name in this example.<\/span><\/li><li><span style=\"font-weight: 400;\"> For Environment image, choose the Managed image or Custom image, as appropriate. For environment type, choose Linux or Windows Server. Specify a runtime, runtime version, and service role in relation to your project.<\/span><\/li><li><span style=\"font-weight: 400;\"> Configure the build spec file for your project.<\/span><\/li><li><span style=\"font-weight: 400;\"> In Artifacts, expand Additional Configuration. For Cache type, choose Local, as shown here.<\/span><\/li><\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a class=\"lightbox\" href=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild.png\" data-rel=\"penci-gallery-image-content\" ><img decoding=\"async\" width=\"512\" height=\"297\" src=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild.png\" alt=\"\" class=\"wp-image-4497\" srcset=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild.png 512w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild-300x174.png 300w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><span style=\"font-weight: 400;\">Local caching supports the below mentioned caching modes:<\/span><\/p>\n\n\n\n<p><b>a) Source cache mode<\/b><span style=\"font-weight: 400;\"> caches Git metadata for primary and secondary sources. After the cache has been created, subsequent builds pull just the change between commits. This mode is a decent decision for projects with a perfect working directory and a source that is a huge Git repository. If you are choosing this option and your project does not use a Git repository (GitHub, GitHub Enterprise, or Bitbucket), the option is ignored.&nbsp;<\/span><\/p>\n\n\n\n<p><b>b) Docker layer cache mode<\/b><span style=\"font-weight: 400;\"> caches subsist Docker layers. This mode is a perfect selection for projects that build or pull large Docker images. It can avoid the performance matter caused by pulling large Docker images down from the network.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Note:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><span style=\"font-weight: 400;\">Docker layer cache can be used only in the Linux environment.&nbsp;<\/span><\/li><li><span style=\"font-weight: 400;\">The privileged flag should be set so that your project has the appropriate Docker permissions.<\/span><\/li><li><span style=\"font-weight: 400;\">The security implication should be considered before you use a Docker layer cache.<\/span><\/li><\/ul>\n\n\n\n<p><b>c) Custom cache mode<\/b><span style=\"font-weight: 400;\"> caches directories that specify under the build spec file. This mode is also a perfect choice if your build structure is not suitable for one of the other two local cache modes. If you use a custom cache:<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">*Only the directories can be specified for caching rather than specifying individual files.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">*Symlinks are used for referencing cached directories.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">*Cached directories are merged to your build before it downloads its project sources. Cached items will be overridden if a source item has an identical name. Directories are defined using cache paths in the build spec file.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><b>To use source cache mode<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">In the configuration for build project, under Artifacts, expand the Additional Configuration. Choose Local for Cache type, choose Local. and select Source cache, as per the below image.<\/span><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a class=\"lightbox\" href=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild1.png\" data-rel=\"penci-gallery-image-content\" ><img decoding=\"async\" width=\"512\" height=\"272\" src=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild1.png\" alt=\"\" class=\"wp-image-4498\" srcset=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild1.png 512w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild1-300x159.png 300w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><b>To use Docker layer cache mode<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Under the build project configuration, in Artifacts, enlarge Additional Configuration. For Cache type, click Local. Select <a href=\"https:\/\/www.supportpro.com\/blog\/docker-compose\/\">Docker<\/a> layer cache, as shown here.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">In Privileged, choose the &#8216;Enable this flag if you want to build Docker images or want your builds to get elevated privileges&#8217;. This allows elevated privileges to the Docker process running on the build host.<\/span><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a class=\"lightbox\" href=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild2.png\" data-rel=\"penci-gallery-image-content\" ><img loading=\"lazy\" decoding=\"async\" width=\"813\" height=\"444\" src=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild2.png\" alt=\"\" class=\"wp-image-4500\" srcset=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild2.png 813w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild2-300x164.png 300w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild2-768x419.png 768w\" sizes=\"(max-width: 813px) 100vw, 813px\" \/><\/a><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a class=\"lightbox\" href=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild3.png\" data-rel=\"penci-gallery-image-content\" ><img loading=\"lazy\" decoding=\"async\" width=\"512\" height=\"328\" src=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild3.png\" alt=\"\" class=\"wp-image-4501\" srcset=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild3.png 512w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild3-300x192.png 300w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><b>To use custom cache mode<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Under your build spec file, need to add the cache path as mentioned below.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Under the build project configuration, in Artifacts, expand Additional Configuration. For Cache type, choose Local. Select Custom cache, as shown here.<\/span><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a class=\"lightbox\" href=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild4.png\" data-rel=\"penci-gallery-image-content\" ><img loading=\"lazy\" decoding=\"async\" width=\"512\" height=\"284\" src=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild4.png\" alt=\"\" class=\"wp-image-4502\" srcset=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild4.png 512w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2019\/11\/aws-codebuild4-300x166.png 300w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n\n\n<pre class=\"wp-block-preformatted\">YAML\nversion: 0.2\nphases:\n pre_build:\n commands:\n - echo \"Enter pre_build commands\"\n build:\n commands:\n - echo \"Enter build commands\"\n \ncache:\n paths:\n - '\/root\/.m2\/**\/*'\n - '\/root\/.npm\/**\/*'\n - 'build\/**\/*'<\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">This article clearly explains in detail on how to enable and configure the cache setting for <a href=\"https:\/\/www.supportpro.com\/blog\/category\/aws\/\">AWS<\/a> CodeBuild. These things can save significant build time and improves flexibility by avoiding external network connections to an artifact repository as well.<\/span><\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide has-media-on-the-right is-stacked-on-mobile is-vertically-aligned-center has-white-background-color has-background\"><div class=\"wp-block-media-text__content\">\n<p class=\"has-large-font-size\">Facing issues? <\/p>\n\n\n\n<p class=\"has-large-font-size\">Our technical support<br>engineers can solve it. <\/p>\n\n\n\n<!--HubSpot Call-to-Action Code --><span class=\"hs-cta-wrapper\" id=\"hs-cta-wrapper-3350a795-db50-482f-9911-301930d1b1be\"><span class=\"hs-cta-node hs-cta-3350a795-db50-482f-9911-301930d1b1be\" id=\"hs-cta-3350a795-db50-482f-9911-301930d1b1be\"><!--[if lte IE 8]><div id=\"hs-cta-ie-element\"><\/div><![endif]--><a href=\"https:\/\/cta-redirect.hubspot.com\/cta\/redirect\/2725694\/3350a795-db50-482f-9911-301930d1b1be\" ><img decoding=\"async\" class=\"hs-cta-img\" id=\"hs-cta-img-3350a795-db50-482f-9911-301930d1b1be\" style=\"border-width:0px;\" src=\"https:\/\/no-cache.hubspot.com\/cta\/default\/2725694\/3350a795-db50-482f-9911-301930d1b1be.png\"  alt=\"Contact Us today!\"\/><\/a><\/span><script charset=\"utf-8\" src=\"https:\/\/js.hscta.net\/cta\/current.js\"><\/script><script type=\"text\/javascript\"> hbspt.cta.load(2725694, '3350a795-db50-482f-9911-301930d1b1be', {\"useNewLoader\":\"true\",\"region\":\"na1\"}); <\/script><\/span><!-- end HubSpot Call-to-Action Code -->\n<\/div><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"904\" height=\"931\" src=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup.png\" alt=\"guy server checkup\" class=\"wp-image-12943 size-full\" srcset=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup.png 904w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-291x300.png 291w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-768x791.png 768w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-585x602.png 585w\" sizes=\"(max-width: 904px) 100vw, 904px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>AWS CodeBuild is a completely overseen persistent incorporation administration. You simply indicate the area of your source code, pick your construct settings, and CodeBuild will run manufacture contents for accumulating,&hellip;<\/p>\n","protected":false},"author":4,"featured_media":13015,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[69],"tags":[53,102,106],"class_list":["post-4495","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","tag-cloud","tag-devops","tag-server"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/4495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/comments?post=4495"}],"version-history":[{"count":14,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/4495\/revisions"}],"predecessor-version":[{"id":13017,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/4495\/revisions\/13017"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media\/13015"}],"wp:attachment":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media?parent=4495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/categories?post=4495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/tags?post=4495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}