{"id":226,"date":"2019-09-03T20:05:07","date_gmt":"2019-09-03T13:05:07","guid":{"rendered":"http:\/\/geeksops.com\/?p=226"},"modified":"2021-02-20T19:51:12","modified_gmt":"2021-02-20T12:51:12","slug":"build-infrastructure-gcp-using-teraform-in-gitlab-ci","status":"publish","type":"post","link":"https:\/\/rotreein.com\/?p=226","title":{"rendered":"Build infrastructure in GCP with Terraform and Gitlab-CI"},"content":{"rendered":"\n<p style=\"text-align:left\">I will share a little bit about how to integration terraform with gitlab CI. I think many automation tool for deployment infrastructure starting from building, changing, and versioning infrastructure safely and efficiently.  Straight to point in this case we will give an example of how to deploy compute instances in environment GCP with terraform &amp; Gitlab-CI<\/p>\n\n\n\n<p><br>Create service account<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"934\" height=\"632\" src=\"https:\/\/geeksops.com\/wp-content\/uploads\/2019\/09\/image-1.png\" alt=\"\" class=\"wp-image-249\" srcset=\"https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-1.png 934w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-1-300x203.png 300w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-1-768x520.png 768w\" sizes=\"(max-width: 934px) 100vw, 934px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Choose action -&gt; Create key then export to JSON file<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"279\" src=\"https:\/\/geeksops.com\/wp-content\/uploads\/2019\/09\/image-10-1024x279.png\" alt=\"\" class=\"wp-image-283\" srcset=\"https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-10-1024x279.png 1024w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-10-300x82.png 300w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-10-768x209.png 768w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-10.png 1025w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"582\" height=\"373\" src=\"https:\/\/geeksops.com\/wp-content\/uploads\/2019\/09\/image-4.png\" alt=\"\" class=\"wp-image-255\" srcset=\"https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-4.png 582w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-4-300x192.png 300w\" sizes=\"(max-width: 582px) 100vw, 582px\" \/><\/figure>\n\n\n\n<p><br>Create Google cloud storage, choose options storage -> browser -> create a bucket. please continue until finished<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"863\" height=\"651\" src=\"https:\/\/geeksops.com\/wp-content\/uploads\/2019\/09\/image-11.png\" alt=\"\" class=\"wp-image-293\" srcset=\"https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-11.png 863w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-11-300x226.png 300w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-11-768x579.png 768w\" sizes=\"(max-width: 863px) 100vw, 863px\" \/><\/figure>\n\n\n\n<p><br><br>In this scenario, we use gitlab internal to integrate our environment. example we created simple-gcp  the project a repository.  <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"931\" height=\"650\" src=\"https:\/\/geeksops.com\/wp-content\/uploads\/2019\/09\/image-9.png\" alt=\"\" class=\"wp-image-275\" srcset=\"https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-9.png 931w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-9-300x209.png 300w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/09\/image-9-768x536.png 768w\" sizes=\"(max-width: 931px) 100vw, 931px\" \/><\/figure>\n\n\n\n<p>in the repository please to make sure have file <strong>.gitlab-ci.yml<\/strong> as pipeline configuration &amp; every commit to repo will trigger to run the pipeline.<br><\/p>\n\n\n\n<p>Below there&#8217;s several file as mandatory that will be commit &amp; push to the repo that you created. Exclude the file <strong>.gitignore &amp; creds<\/strong> the folder have you created. Please remember once you created the service account with name <strong>terraform_lab<\/strong> on the previous step, please copy-paste the service account containt into <strong>creds\/serviceaccount.json<\/strong> file, because the gitlab runner need to authenticate Google API.<\/p>\n\n\n\n<p class=\"has-text-color has-background has-small-font-size has-vivid-green-cyan-color has-very-dark-gray-background-color\">backend.tf<br>creds\/serviceaccount.json<br>.gitignore<br>.gitlab-ci.yml<br>main.tf<br>provider.tf<\/p>\n\n\n\n<p>backend.tf<\/p>\n\n\n\n<pre data-mode=\"text\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">terraform {\n    required_version = \"~> 0.11.11\"\n    backend \"gcs\" {\n         credentials = \".\/creds\/serviceaccount.json\"\n         bucket      = \"geeksops_bucket\"\n    }\n}<\/pre>\n\n\n\n<p>provider.tf<\/p>\n\n\n\n<pre data-mode=\"php\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">provider \"google\" {\n   version = \"1.20.0\"\n   credentials = \"${file(\".\/creds\/serviceaccount.json\")}\"\n   project     = \"project-devops-xxxx\" # REPLACE WITH YOUR PROJECT ID\n   region      = \"asia-east1\"\n }\n <\/pre>\n\n\n\n<p><br>Main.tf<\/p>\n\n\n\n<pre data-mode=\"php\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">resource \"google_compute_instance\" \"compute-apps1\" {\n   name          = \"compute-apps1\"\n   machine_type  = \"n1-standard-1\"\n   project    = \"project-devops-XXXX\"\n   zone          = \"asia-east1-a\" \n   boot_disk {\n     initialize_params {\n       image = \"ubuntu-1604-lts\"\n     }\n   }\nnetwork_interface {\n     network = \"default\"\n     access_config {\n     }\n   } \n   tags = [\"apps1\"]\n }\nresource \"google_compute_instance\" \"compute-apps2\" {\n   name          = \"compute-apps2\"\n   machine_type  = \"n1-standard-1\"\n   project       = \"project-devops-XXXX\"\n   zone          = \"asia-east1-a\"\n   boot_disk {\n     initialize_params {\n       image = \"ubuntu-1604-lts\"\n     }\n   }\nnetwork_interface {\n     network = \"default\"\n     access_config {\n     }\n   }\n   tags = [\"apps2\"]\n }\n\nnetwork_interface {\n     network = \"default\"\n     access_config {\n     }\n   }\n   tags = [\"apps2\"]\n }<\/pre>\n\n\n\n<p>The following a feature from gitlab to provide credentials to our Runner when the pipeline executes. In Gitlab navigate <strong><em>SETTING -&gt; CI\/CD<\/em><\/strong> and expand variable. there are 2 columns , left column create a SERVICEACCOUNT and right column put your variable which has been encode below<\/p>\n\n\n\n<p class=\"has-text-color has-background has-small-font-size has-vivid-green-cyan-color has-very-dark-gray-background-color\">cat creds\/serviceaccount.json | base64 -w0<\/p>\n\n\n\n<p>put the result of this command into the value of the variable and click&nbsp;<strong>Save Variable<\/strong>. GitLab has a concept of protected variables to limit their use to specific git branches, but that\u2019s beyond the scope of this post.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Below configuration .gitlab-ci.yml. this file there are several parameter will be execute terraform file using extension .tf,<\/p>\n\n\n\n<pre data-mode=\"text\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">image:\n   name: hashicorp\/terraform:light\n   entrypoint:\n     - '\/usr\/bin\/env'\n     - 'PATH=\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin'\n\nbefore_script\n     -  rm -rf .terraform\n     -  terraform --version\n     -  mkdir -p .\/creds\n     -  echo $SERVICEACCOUNT | base64 -d > .\/creds\/serviceaccount.json\n     -  terraform init\n\nstages:\n      - validate\n      - plan\n      - apply\n\nvalidate:\n   stage: validate\n   script:\n     - terraform validate\n\nplan:\n   stage: plan\n   script:\n     - terraform plan -out \"planfile\"\n   dependencies:\n     - validate\n   artifacts:\n     paths:\n       - planfile\n\napply:\n   stage: apply\n   script:\n     - terraform apply -input=false \"planfile\"\n   dependencies:\n     - plan\n   when: manual<\/pre>\n\n\n\n<p><br>After all files already prepared, please commit and then push to the repository<\/p>\n\n\n\n<p class=\"has-text-color has-background has-small-font-size has-vivid-green-cyan-color has-very-dark-gray-background-color\">$ git checkout -b dev-fitur-baru<br>$ git add .<br>$ git commit -m &#8220;initial commit&#8221;<br>$ git push origin dev-fitur-baru<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"763\" height=\"505\" src=\"https:\/\/geeksops.com\/wp-content\/uploads\/2019\/10\/image-7.png\" alt=\"\" class=\"wp-image-471\" srcset=\"https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-7.png 763w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-7-300x199.png 300w\" sizes=\"(max-width: 763px) 100vw, 763px\" \/><\/figure>\n\n\n\n<p><br>The following 3 Jobs have defined in file <em><strong>.gitlab-ci.yml<\/strong><\/em> such as <strong><em>validate, plan &amp; deploy<\/em><\/strong>, the result will appear like this after your file push &amp; merge to the repository<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"923\" height=\"769\" src=\"https:\/\/geeksops.com\/wp-content\/uploads\/2019\/10\/image-3.png\" alt=\"\" class=\"wp-image-447\" srcset=\"https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-3.png 923w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-3-300x250.png 300w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-3-768x640.png 768w\" sizes=\"(max-width: 923px) 100vw, 923px\" \/><\/figure>\n\n\n\n<p><br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"898\" height=\"829\" src=\"https:\/\/geeksops.com\/wp-content\/uploads\/2019\/10\/image-4.png\" alt=\"\" class=\"wp-image-452\" srcset=\"https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-4.png 898w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-4-300x277.png 300w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-4-768x709.png 768w\" sizes=\"(max-width: 898px) 100vw, 898px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"796\" height=\"182\" src=\"https:\/\/geeksops.com\/wp-content\/uploads\/2019\/10\/image-6.png\" alt=\"\" class=\"wp-image-456\" srcset=\"https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-6.png 796w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-6-300x69.png 300w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-6-768x176.png 768w\" sizes=\"(max-width: 796px) 100vw, 796px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"892\" height=\"798\" src=\"https:\/\/geeksops.com\/wp-content\/uploads\/2019\/10\/image-5.png\" alt=\"\" class=\"wp-image-453\" srcset=\"https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-5.png 892w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-5-300x268.png 300w, https:\/\/rotreein.com\/wp-content\/uploads\/2019\/10\/image-5-768x687.png 768w\" sizes=\"(max-width: 892px) 100vw, 892px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I will share a little bit about how to integration terraform with gitlab CI. I think many automation tool for deployment infrastructure starting from building, changing, and versioning infrastructure safely and efficiently. Straight to point in this case we will give an example of how to deploy compute instances in environment GCP with terraform &amp;&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,2],"tags":[],"_links":{"self":[{"href":"https:\/\/rotreein.com\/index.php?rest_route=\/wp\/v2\/posts\/226"}],"collection":[{"href":"https:\/\/rotreein.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rotreein.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rotreein.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rotreein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=226"}],"version-history":[{"count":79,"href":"https:\/\/rotreein.com\/index.php?rest_route=\/wp\/v2\/posts\/226\/revisions"}],"predecessor-version":[{"id":655,"href":"https:\/\/rotreein.com\/index.php?rest_route=\/wp\/v2\/posts\/226\/revisions\/655"}],"wp:attachment":[{"href":"https:\/\/rotreein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rotreein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rotreein.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}