{"id":298,"date":"2018-12-03T18:30:05","date_gmt":"2018-12-03T15:30:05","guid":{"rendered":"https:\/\/artem.services\/?p=298"},"modified":"2019-02-26T13:52:05","modified_gmt":"2019-02-26T10:52:05","slug":"terraform-google-cloud-platform-gcn-gcf-instance","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=298","title":{"rendered":"Terraform (Google Cloud Platform) &#8212; GCN, GCF, Instance"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"size-full wp-image-99 aligncenter\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/terraform.png\" alt=\"\" width=\"1210\" height=\"418\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/terraform.png 1210w, https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/terraform-300x104.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/terraform-768x265.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/terraform-1024x354.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/terraform-954x330.png 954w\" sizes=\"(max-width: 1210px) 100vw, 1210px\" \/><\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 <strong>Terraform<\/strong>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 <strong>GCN<\/strong> (Google Compute Network), <strong>GCF<\/strong> (Google Compute Firewall), <strong>Instance<\/strong>. \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f &quot;<strong>metadata_startup_script<\/strong>&quot; \u0432 <strong>Instance<\/strong><\/p>\n<h3>main.tf<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nprovider &quot;google&quot; {\r\n  credentials = &quot;${file(&quot;terraform-account.json&quot;)}&quot;\r\n  project = &quot;artem-terraform&quot;\r\n  region = &quot;us-central1&quot;\r\n}\r\n<\/pre>\n<h3>variables.tf<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nvariable &quot;PROJECT_NAME&quot; {\r\n  default = &quot;web-server&quot;\r\n}\r\n\r\nvariable &quot;BITBUCKET_USER&quot; {\r\n  default = &quot;terraform-git-website&quot;\r\n}\r\n\r\nvariable &quot;BITBUCKET_PASS&quot; {\r\n  default = &quot;MyTempPass1234&quot;\r\n}\r\n<\/pre>\n<p><!--more--><\/p>\n<h3>gcf.tf<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nresource &quot;google_compute_firewall&quot; &quot;default&quot; {\r\n  name    = &quot;${var.PROJECT_NAME}-firewall&quot;\r\n  network = &quot;${google_compute_network.default.name}&quot;\r\n\r\n  allow {\r\n    protocol = &quot;icmp&quot;\r\n  }\r\n\r\n  allow {\r\n    protocol = &quot;tcp&quot;\r\n    ports    = [&quot;22&quot;, &quot;80&quot;, &quot;443&quot;]\r\n  }\r\n\r\n}\r\n\r\nresource &quot;google_compute_network&quot; &quot;default&quot; {\r\n  name = &quot;${var.PROJECT_NAME}-network&quot;\r\n  auto_create_subnetworks = &quot;true&quot;\r\n}\r\n<\/pre>\n<h3>instance.tf<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nresource &quot;google_compute_instance&quot; &quot;web&quot; {\r\n  name         = &quot;${var.PROJECT_NAME}-instance&quot;\r\n  machine_type = &quot;f1-micro&quot;\r\n  zone         = &quot;us-west1-a&quot;\r\n\r\n  tags = [&quot;web&quot;]\r\n\r\n  boot_disk {\r\n    initialize_params {\r\n      image = &quot;ubuntu-minimal-1804-bionic-v20181120&quot;\r\n    }\r\n  }\r\n\r\n  network_interface {\r\n    network = &quot;${google_compute_network.default.name}&quot;\r\n\r\n    access_config {\r\n      \/\/ Ephemeral IP\r\n    }\r\n  }\r\n\r\n  metadata_startup_script = &lt;&lt;SCRIPT\r\n  apt update\r\n  apt install -y apache2 git\r\n  rm -r \/var\/www\/html\/*\r\n  cd \/var\/www\/html\/ &amp;&amp; git clone https:\/\/&quot;${var.BITBUCKET_USER}&quot;:&quot;${var.BITBUCKET_PASS}&quot;@bitbucket.org\/terraform-git-website\/html.git .\r\n  systemctl enable apache2\r\n  systemctl start apache2\r\n  SCRIPT\r\n\r\n  service_account {\r\n    scopes = [&quot;userinfo-email&quot;, &quot;compute-ro&quot;, &quot;storage-ro&quot;]\r\n  }\r\n}\r\n<\/pre>\n<h3>output.tf<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\noutput &quot;public_ip&quot; {\r\n  value = &quot;${google_compute_instance.web.network_interface.0.access_config.0.nat_ip}&quot;\r\n}\r\n<\/pre>\n<p>\u0421\u043a\u0430\u0447\u0430\u0442\u044c <a href=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/12\/GCP-Example-Instance-with-script.zip\">ZIP \u0430\u0440\u0445\u0438\u0432<\/a> \u0441 Terraform \u0444\u0430\u0439\u043b\u0430\u043c\u0438.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 Terraform, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 GCN (Google Compute Network), GCF (Google Compute Firewall), Instance. \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f &quot;metadata_startup_script&quot; \u0432 Instance main.tf variables.tf<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[71,72,70,69,73,75,76,74,14],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/298"}],"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=298"}],"version-history":[{"count":5,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/298\/revisions"}],"predecessor-version":[{"id":788,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/298\/revisions\/788"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}