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}
建立虛擬機器
建立 VM。
resource"google_compute_instance""default"{project=var.project_idname="${var.basename}-instance"machine_type="f1-micro"zone=var.zoneboot_disk{initialize_params{image="centos-cloud/centos-8-v20210817"}}labels={env="prod"app="myproduct"created_by="terraform"}network_interface{network="default"access_config{ // Include this section to give the VM an external ip address}}service_account{ // Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles. // This non production example uses the default compute service account.email=local.sacomputescopes=["cloud-platform"]}}
[[["容易理解","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-07-29 (世界標準時間)。"],[],[],null,["# Ops Agent\n\n+\n\n[](./images/arch-ops-agent.svg)\n\nOps Agent creates a virtual machine and installs the Ops Agent for\nintegrating with Monitoring and Logging services. It uses:\n\n- Compute Engine\n- Monitoring\n- Logging\n\nExample sets up a simple single virtual machine and wires it up to report to\nLogging.\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-ops-agent&shellonly=true&cloudshell_image=gcr.io/ds-artifacts-cloudshell/deploystack_custom_image)\n\n[View source code on GitHub](https://github.com/googlecloudplatform/deploystack-ops-agent)\n\n*** ** * ** ***\n\nOps Agent components\n--------------------\n\nThe Ops Agent architecture makes use of several products. The following lists the components, along with more information on the components, 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 Ops Agent. *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 a virtual machine\n\nCreates a VM. \n\n resource \"google_compute_instance\" \"default\" {\n project = var.project_id\n name = \"${var.basename}-instance\"\n machine_type = \"f1-micro\"\n zone = var.zone\n\n boot_disk {\n initialize_params {\n image = \"centos-cloud/centos-8-v20210817\"\n }\n }\n labels = {\n env = \"prod\"\n app = \"myproduct\"\n created_by = \"terraform\"\n }\n\n network_interface {\n network = \"default\"\n\n access_config {\n // Include this section to give the VM an external ip address\n }\n }\n service_account {\n // Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.\n // This non production example uses the default compute service account.\n email = local.sacompute\n scopes = [\"cloud-platform\"]\n }\n }\n\n#### Install Ops Agent\n\nThe Ops Agent is the primary agent for collecting telemetry from your Compute Engine instances. This will show you how to install it, using terraform. \n\n module \"agent_policy\" {\n source = \"terraform-google-modules/cloud-operations/google//modules/agent-policy\"\n version = \"~\u003e 0.1.0\"\n \n project_id = var.project_id\n policy_id = \"ops-agents-example-policy\"\n agent_rules = [\n {\n type = \"ops-agent\"\n version = \"current-major\"\n package_state = \"installed\"\n enable_autoupgrade = true\n },\n ]\n group_labels = [\n {\n env = \"prod\"\n app = \"myproduct\"\n created_by = \"terraform\"\n }\n ]\n \n os_types = [\n {\n short_name = \"centos\"\n version = \"8\"\n },\n ]\n }\n\n*** ** * ** ***\n\nConclusion\n----------\n\nOnce run, you should now have a VM that's been properly configured to log events\nto Google Cloud Logging. Additionally you should have all of the code to modify\nor extend this solution to fit your environment."]]