Google Cloud サービスは、デフォルトではプロジェクトで無効になっています。ソリューションを使用するには、以下を有効にする必要があります。
Compute Engine — 仮想マシンとネットワーク
variable"gcp_service_list"{description="The list of apis necessary for the project"type=list(string)default=["compute.googleapis.com",]}resource"google_project_service""all"{for_each=toset(var.gcp_service_list)project=var.project_numberservice=each.keydisable_dependent_services=falsedisable_on_destroy=false}
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-01-14 UTC。"],[],[],null,["# NoSQL Client Server\n\n+\n\n[](./images/arch-nosql-client-server.svg)\n\nNoSQL Client Server will configure and create two Compute Engine instances:\n\n- **Server** --- which will run MongoDB\n- **Client** --- which will run a custom go application that talks to MongoDB and then exposes an API where you consume data from MongoDB\n\n*** ** * ** ***\n\nGet Started\n-----------\n\nClick on the following link to a copy of the source code in Cloud Shell. Once\nthere, a single command will spin up a working copy of the application in your\nproject..\n| **Key Point:** Clicking on the following link will open a Cloud Shell session and download code from GitHub. You will have to authorize DeployStack to run. No actions will be taken until you do so.\n\n[](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https://github.com/googlecloudplatform/deploystack-nosql-client-server&shellonly=true&cloudshell_image=gcr.io/ds-artifacts-cloudshell/deploystack_custom_image)\n\n[View source code on GitHub](https://github.com/googlecloudplatform/deploystack-nosql-client-server)\n\n*** ** * ** ***\n\nNoSQL Client Server components\n------------------------------\n\nThe NoSQL Client Server architecture makes use of one key product. The following highlights that product, along with more information, including links to related videos, product documentation, and interactive walkthroughs.\n\n*** ** * ** ***\n\nScripts\n-------\n\nThe install script uses an executable written in `go` and Terraform CLI tools to\ntake an empty project and install the application in it. The output should be a\nworking application and a url for the load balancing IP address.\n| **Note:** The following is a breakdown of the commands that run when deploying NoSQL Client Server. *You don't need to run each command individually; Click the Open in Cloud Shell button in the Getting Started section and run all commands.*\n\n### `./main.tf`\n\n#### Enable Services\n\nGoogle Cloud Services are disabled in a project by default. In order to use any\nof the solutions here, we have to activate the following:\n\n- **Compute Engine** --- virtual machines and networking\n\n variable \"gcp_service_list\" {\n description = \"The list of apis necessary for the project\"\n type = list(string)\n default = [\n \"compute.googleapis.com\",\n ]\n }\n\n resource \"google_project_service\" \"all\" {\n for_each = toset(var.gcp_service_list)\n project = var.project_number\n service = each.key\n disable_dependent_services = false\n disable_on_destroy = false\n }\n\n#### Create Firewall rule\n\nMakes a rule that opens up port 80 on the firewall and then applies it to any machine labeled `http-server` \n\n resource \"google_compute_firewall\" \"default-allow-http\" {\n name = \"deploystack-allow-http\"\n project = var.project_number\n network = \"projects/${var.project_id}/global/networks/default\"\n\n allow {\n protocol = \"tcp\"\n ports = [\"80\"]\n }\n\n source_ranges = [\"0.0.0.0/0\"]\n\n target_tags = [\"http-server\"]\n }\n\n#### Create Server Instance\n\nSpins up a VM that will act as the server, and serve up MongoDB \n\n # Create Instances\n resource \"google_compute_instance\" \"server\" {\n name = \"server\"\n zone = var.zone\n project = var.project_id\n machine_type = \"e2-standard-2\"\n tags = [\"http-server\"]\n\n\n boot_disk {\n auto_delete = true\n device_name = \"server\"\n initialize_params {\n image = \"family/ubuntu-1804-lts\"\n size = 10\n type = \"pd-standard\"\n }\n }\n\n network_interface {\n network = \"default\"\n access_config {\n // Ephemeral public IP\n }\n }\n\n metadata_startup_script = \u003c\u003cSCRIPT\n apt-get update\n apt-get install -y mongodb\n service mongodb stop\n sed -i 's/bind_ip = 127.0.0.1/bind_ip = 0.0.0.0/' /etc/mongodb.conf\n iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 27017\n service mongodb start\n SCRIPT\n depends_on = [google_project_service.all]\n }\n\n#### Create Client Instance\n\nSpins up a VM that will act as the client, and consume data from MongoDB \n\n resource \"google_compute_instance\" \"client\" {\n name = \"client\"\n zone = var.zone\n project = var.project_id\n machine_type = \"e2-standard-2\"\n tags = [\"http-server\", \"https-server\"]\n\n boot_disk {\n auto_delete = true\n device_name = \"client\"\n initialize_params {\n image = \"family/ubuntu-1804-lts\"\n size = 10\n type = \"pd-standard\"\n }\n }\n\n network_interface {\n network = \"default\"\n\n access_config {\n // Ephemeral public IP\n }\n }\n\n metadata_startup_script = \u003c\u003cSCRIPT\n add-apt-repository ppa:longsleep/golang-backports -y && \\\n apt update -y && \\\n apt install golang-go -y\n mkdir /modcache\n mkdir /go\n mkdir /app && cd /app\n curl https://raw.githubusercontent.com/GoogleCloudPlatform/golang-samples/main/compute/quickstart/compute_quickstart_sample.go --output main.go\n go mod init exec\n GOPATH=/go GOMODCACHE=/modcache GOCACHE=/modcache go mod tidy\n GOPATH=/go GOMODCACHE=/modcache GOCACHE=/modcache go get -u \n sed -i 's/mongoport = \"80\"/mongoport = \"27017\"/' /app/main.go\n echo \"GOPATH=/go GOMODCACHE=/modcache GOCACHE=/modcache HOST=${google_compute_instance.server.network_interface.0.network_ip} go run main.go\"\n GOPATH=/go GOMODCACHE=/modcache GOCACHE=/modcache HOST=${google_compute_instance.server.network_interface.0.network_ip} go run main.go &\n SCRIPT\n\n depends_on = [google_project_service.all]\n }\n\n*** ** * ** ***\n\nConclusion\n----------\n\nOnce run you should now have a client server api using MongoDB running on two Compute Engine instances. Additionally you should have all of the code to modify or extend this solution to fit your environment."]]