{"id":4499,"date":"2024-02-15T22:27:45","date_gmt":"2024-02-15T13:27:45","guid":{"rendered":"https:\/\/blog.since2020.jp\/?p=4499"},"modified":"2024-02-15T22:27:45","modified_gmt":"2024-02-15T13:27:45","slug":"terraform%e3%82%92%e4%bd%bf%e7%94%a8%e3%81%97%e3%81%a6google-cloud-platform%e3%81%a7vm%e3%82%a4%e3%83%b3%e3%82%b9%e3%82%bf%e3%83%b3%e3%82%b9%e3%82%92%e7%ab%8b%e3%81%a1%e4%b8%8a%e3%81%92%e3%82%8b","status":"publish","type":"post","link":"https:\/\/since2020.jp\/media\/terraform%e3%82%92%e4%bd%bf%e7%94%a8%e3%81%97%e3%81%a6google-cloud-platform%e3%81%a7vm%e3%82%a4%e3%83%b3%e3%82%b9%e3%82%bf%e3%83%b3%e3%82%b9%e3%82%92%e7%ab%8b%e3%81%a1%e4%b8%8a%e3%81%92%e3%82%8b\/","title":{"rendered":"Terraform\u3092\u4f7f\u7528\u3057\u3066Google Cloud Platform\u3067VM\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u7acb\u3061\u4e0a\u3052\u308b"},"content":{"rendered":"\n<p>\u30af\u30e9\u30a6\u30c9\u30b3\u30f3\u30d4\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u5229\u70b9\u3092\u6700\u5927\u9650\u306b\u6d3b\u7528\u3059\u308b\u305f\u3081\u306b\u3001\u30a4\u30f3\u30d5\u30e9\u30b9\u30c8\u30e9\u30af\u30c1\u30e3\u306e\u30b3\u30fc\u30c9\u5316\u3068\u81ea\u52d5\u5316\u306f\u4e0d\u53ef\u6b20\u3067\u3059\u3002Terraform\u306f\u3001Google Cloud Platform\uff08GCP\uff09\u3067\u306e\u30a4\u30f3\u30d5\u30e9\u30b9\u30c8\u30e9\u30af\u30c1\u30e3\u306e\u7ba1\u7406\u3092\u7c21\u7d20\u5316\u3059\u308b\u305f\u3081\u306e\u30c4\u30fc\u30eb\u3067\u3042\u308a\u3001\u672c\u8a18\u4e8b\u3067\u306fTerraform\u3092\u4f7f\u7528\u3057\u3066GCP\u3067VM\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u7acb\u3061\u4e0a\u3052\u308b\u65b9\u6cd5\u306b\u7126\u70b9\u3092\u5f53\u3066\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n<h2>\u306f\u3058\u3081\u306b<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u30af\u30e9\u30a6\u30c9\u30b3\u30f3\u30d4\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u5229\u70b9\u3092\u6700\u5927\u9650\u306b\u6d3b\u7528\u3059\u308b\u305f\u3081\u306b\u3001\u30a4\u30f3\u30d5\u30e9\u30b9\u30c8\u30e9\u30af\u30c1\u30e3\u306e\u30b3\u30fc\u30c9\u5316\u3068\u81ea\u52d5\u5316\u306f\u4e0d\u53ef\u6b20\u3067\u3059\u3002Terraform\u306f\u3001Google Cloud Platform\uff08GCP\uff09\u3067\u306e\u30a4\u30f3\u30d5\u30e9\u30b9\u30c8\u30e9\u30af\u30c1\u30e3\u306e\u7ba1\u7406\u3092\u7c21\u7d20\u5316\u3059\u308b\u305f\u3081\u306e\u30c4\u30fc\u30eb\u3067\u3042\u308a\u3001\u672c\u8a18\u4e8b\u3067\u306fTerraform\u3092\u4f7f\u7528\u3057\u3066GCP\u3067VM\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u7acb\u3061\u4e0a\u3052\u308b\u65b9\u6cd5\u306b\u7126\u70b9\u3092\u5f53\u3066\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/span><\/p>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><strong>\u203bTerraform\u306e\u8a73\u7d30\u306a\u74b0\u5883\u69cb\u7bc9\u306b\u3064\u3044\u3066\u306f\u3053\u3061\u3089\u304b\u3089\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/strong><\/span><\/p>\r\n<p><!-- notionvc: 065bebd6-4568-4989-b6b5-c2b4602dd622 --><\/p>\n\n<h2>1. Terraform\u306e\u8a2d\u5b9a<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u307e\u305a\u3001Terraform\u306e\u8a2d\u5b9a\u3092\u884c\u3044\u307e\u3059\u3002\u4ee5\u4e0b\u306eTerraform\u30b3\u30fc\u30c9\u3092\u4efb\u610f\u306e\u74b0\u5883\u306b\u5408\u308f\u305b\u3066\u8a2d\u5b9a\u3057 \u3001<span data-token-index=\"1\" class=\"notion-enable-hover\">main.tf<\/span> \u3068\u3044\u3046\u540d\u524d\u306e\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3057\u307e\u3059\u3002<\/span><!-- notionvc: 01f42775-e263-43cd-b27f-067fb569c0b0 --><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># Terraform\u306e\u8a2d\u5b9a\u3092\u958b\u59cb\u3059\u308b\r\nterraform {\r\n# \u5fc5\u8981\u306a\u30d7\u30ed\u30d0\u30a4\u30c0\u30fc\u3092\u6307\u5b9a\u3057\u307e\u3059\r\nrequired_providers {\r\ngoogle = {\r\nsource = \"hashicorp\/google\"\r\nversion = \"3.72.0\"\r\n}\r\n}\r\n}\r\n\r\n# Google Cloud Platform\u306e\u8a8d\u8a3c\u60c5\u5831\u3092\u6307\u5b9a\u3057\u307e\u3059\r\nprovider \"google\" {\r\ncredentials = file(\"\/path\/to\/your\/credentials.json\") # GCP\u306e\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u30ad\u30fc\u306e\u30d1\u30b9\u3092\u6307\u5b9a\u3057\u307e\u3059\r\n\r\nproject = \"your-project-id\" # GCP\u30d7\u30ed\u30b8\u30a7\u30af\u30c8ID\u3092\u6307\u5b9a\u3057\u307e\u3059\r\nregion = \"your-region\" # \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3059\u308b\u30ea\u30fc\u30b8\u30e7\u30f3\u3092\u6307\u5b9a\u3057\u307e\u3059\r\nzone = \"your-zone\" # \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3059\u308b\u30be\u30fc\u30f3\u3092\u6307\u5b9a\u3057\u307e\u3059\r\n}\r\n\r\n# Compute Engine\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u5b9a\u7fa9\u3057\u307e\u3059\r\nresource \"google_compute_instance\" \"vm_instance\" {\r\nname = \"my-vm-instance\" # \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u540d\u524d\u3092\u6307\u5b9a\u3057\u307e\u3059\r\nmachine_type = \"n1-standard-1\" # \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u30de\u30b7\u30f3\u30bf\u30a4\u30d7\u3092\u6307\u5b9a\u3057\u307e\u3059\r\nzone = \"your-zone\" # \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3059\u308b\u30be\u30fc\u30f3\u3092\u6307\u5b9a\u3057\u307e\u3059\r\n\r\n# \u30d6\u30fc\u30c8\u30c7\u30a3\u30b9\u30af\u306e\u8a2d\u5b9a\u3092\u5b9a\u7fa9\u3057\u307e\u3059\r\nboot_disk {\r\ninitialize_params {\r\nimage = \"debian-cloud\/debian-10\" # \u30d6\u30fc\u30c8\u30c7\u30a3\u30b9\u30af\u306e\u30a4\u30e1\u30fc\u30b8\u3092\u6307\u5b9a\u3057\u307e\u3059\r\n}\r\n}\r\n\r\n# \u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306e\u8a2d\u5b9a\u3092\u5b9a\u7fa9\u3057\u307e\u3059\r\nnetwork_interface {\r\nnetwork = \"default\" # \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u4f7f\u7528\u3059\u308b\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u3092\u6307\u5b9a\u3057\u307e\u3059\uff08\u65e2\u5b9a\u306e\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f \"default\" \u3092\u6307\u5b9a\u3057\u307e\u3059\uff09\r\naccess_config {} # \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u5916\u90e8IP\u30a2\u30c9\u30ec\u30b9\u3092\u8a2d\u5b9a\u3057\u307e\u3059\r\n}\r\n\r\n# \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u8d77\u52d5\u6642\u306b\u5b9f\u884c\u3059\u308b\u30b9\u30bf\u30fc\u30c8\u30a2\u30c3\u30d7\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u6307\u5b9a\u3057\u307e\u3059\r\nmetadata_startup_script = &lt;&lt;-EOF\r\n#!\/bin\/bash\r\n# \u5fc5\u8981\u306a\u4f9d\u5b58\u95a2\u4fc2\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\r\napt-get update\r\napt-get install -y python3-pip\r\npip3 install google-cloud-bigquery\r\n\r\n# \u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u3057\u307e\u3059\r\npython3 \/path\/to\/your\/scraping_script.py\r\nEOF\r\n}<\/code><\/pre>\r\n<\/div>\n\n<h2>2. Terraform\u306e\u5b9f\u884c<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">Terraform\u306e\u521d\u671f\u5316\u3068\u30c7\u30d7\u30ed\u30a4\u30e1\u30f3\u30c8\u3092\u884c\u3044\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u624b\u9806\u306b\u5f93\u3063\u3066\u9032\u3081\u3066\u304f\u3060\u3055\u3044\u3002<\/span><\/p>\r\n<b><span style=\"font-family: arial, helvetica, sans-serif;font-size: 18pt\"><strong>\u521d\u671f\u5316<\/strong><\/span><\/b>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u30bf\u30fc\u30df\u30ca\u30eb\u3067\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u3001Terraform\u306e\u521d\u671f\u5316\u3092\u884c\u3044\u307e\u3059\u3002<\/span><\/p>\r\n<pre><span style=\"font-family: arial, helvetica, sans-serif\"><code class=\"language-bash\"><\/code><\/span><\/pre>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-git\" data-lang=\"Git\"><code>terraform init<\/code><\/pre>\r\n<\/div>\r\n<pre><span style=\"font-family: arial, helvetica, sans-serif\"><code class=\"language-bash\"><\/code><\/span><\/pre>\r\n<b><span style=\"font-family: arial, helvetica, sans-serif\"><strong>\u30d7\u30e9\u30f3\u30cb\u30f3\u30b0<\/strong><\/span><\/b>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u6b21\u306b\u3001\u30d7\u30e9\u30f3\u30cb\u30f3\u30b0\u3092\u884c\u3044\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u3001Terraform\u304c\u5b9f\u884c\u3059\u308b\u5909\u66f4\u306e\u6982\u8981\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/span><\/p>\r\n<pre><span style=\"font-family: arial, helvetica, sans-serif\"><code class=\"language-bash\"><\/code><\/span><\/pre>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-git\" data-lang=\"Git\"><code>terraform plan<\/code><\/pre>\r\n<\/div>\r\n<pre><span style=\"font-family: arial, helvetica, sans-serif\"><code class=\"language-bash\"><\/code><\/span><\/pre>\r\n<b><span style=\"font-family: arial, helvetica, sans-serif\"><strong>\u30c7\u30d7\u30ed\u30a4<\/strong><\/span><\/b>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u6700\u5f8c\u306b\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u3001Terraform\u304c\u30a4\u30f3\u30d5\u30e9\u30b9\u30c8\u30e9\u30af\u30c1\u30e3\u306e\u5909\u66f4\u3092\u9069\u7528\u3057\u307e\u3059\u3002<\/span><\/p>\r\n<pre><span style=\"font-family: arial, helvetica, sans-serif\"><code class=\"language-bash\"><\/code><\/span><\/pre>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-git\" data-lang=\"Git\"><code>terraform apply<\/code><\/pre>\r\n<\/div>\r\n<pre><span style=\"font-family: arial, helvetica, sans-serif\"><code class=\"language-bash\"><\/code><\/span><\/pre>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u9069\u7528\u304c\u5b8c\u4e86\u3059\u308b\u3068\u3001VM\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u6b63\u5e38\u306b\u7acb\u3061\u4e0a\u304c\u308a\u307e\u3059\u3002<\/span><\/p>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-02-13-14.38.37.png\" alt=\"\" width=\"1290\" height=\"102\" class=\"alignnone size-full wp-image-4500\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-02-13-14.38.37.png 1290w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-02-13-14.38.37-300x24.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-02-13-14.38.37-1024x81.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-02-13-14.38.37-768x61.png 768w\" sizes=\"(max-width: 1290px) 100vw, 1290px\" \/><\/p>\r\n<p><!-- notionvc: 992ce364-b84c-4f4f-9bc0-ee09b58157bf --><\/p>\n\n<h2>\u307e\u3068\u3081<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u3053\u308c\u3067\u3001Terraform\u3092\u4f7f\u7528\u3057\u3066GCP\u3067VM\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u7acb\u3061\u4e0a\u3052\u308b\u624b\u9806\u304c\u5b8c\u4e86\u3057\u307e\u3057\u305f\u3002\u63d0\u4f9b\u3055\u308c\u305f\u30b9\u30af\u30ea\u30d7\u30c8\u3092VM\u306b\u914d\u7f6e\u3057\u3001\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3067\u3001\u5fc5\u8981\u306a\u51e6\u7406\u3092\u81ea\u52d5\u5316\u3067\u304d\u307e\u3059\u3002<\/span><\/p>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\">Terraform\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u624b\u52d5\u306e\u8a2d\u5b9a\u4f5c\u696d\u3092\u7701\u7565\u3057\u3001\u30a4\u30f3\u30d5\u30e9\u30b9\u30c8\u30e9\u30af\u30c1\u30e3\u306e\u7ba1\u7406\u3092\u52b9\u7387\u5316\u3067\u304d\u307e\u3059\u3002\u662f\u975e\u3001\u81ea\u52d5\u5316\u306e\u529b\u3092\u6d3b\u7528\u3057\u3066\u3001\u30af\u30e9\u30a6\u30c9\u4e0a\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u52b9\u679c\u7684\u306b\u7ba1\u7406\u3057\u307e\u3057\u3087\u3046\u3002<\/span><\/p>\r\n<p><!-- notionvc: 5e89832c-92d7-467e-89cf-9ea4bbf5e25e --><\/p>","protected":false},"excerpt":{"rendered":"<p>\u30af\u30e9\u30a6\u30c9\u30b3\u30f3\u30d4\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u5229\u70b9\u3092\u6700\u5927\u9650\u306b\u6d3b\u7528\u3059\u308b\u305f\u3081\u306b\u3001\u30a4\u30f3\u30d5\u30e9\u30b9\u30c8\u30e9\u30af\u30c1\u30e3\u306e\u30b3\u30fc\u30c9\u5316\u3068\u81ea\u52d5\u5316\u306f\u4e0d\u53ef\u6b20\u3067\u3059\u3002Terraform\u306f\u3001Google Cloud Platform\uff08GCP\uff09\u3067\u306e\u30a4\u30f3\u30d5\u30e9\u30b9\u30c8\u30e9\u30af\u30c1\u30e3\u306e\u7ba1\u7406\u3092\u7c21\u7d20 [&hellip;]<\/p>\n","protected":false},"author":87,"featured_media":4484,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","swell_btn_cv_data":"","footnotes":"","_wp_rev_ctl_limit":""},"categories":[1249],"tags":[143,556,555,554,504,151],"class_list":["post-4499","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledge","tag-gcp","tag-iac","tag-infrastructure-as-code","tag-terraform","tag-vm","tag-151"],"_links":{"self":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/comments?post=4499"}],"version-history":[{"count":0,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4499\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media\/4484"}],"wp:attachment":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media?parent=4499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/categories?post=4499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/tags?post=4499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}