### Compute Packet Mirroring Full Example Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_packet_mirroring This example demonstrates a full setup for Compute Packet Mirroring, including creating a VM instance, a network, a subnetwork, a backend service, a health check, and a forwarding rule configured for mirroring. ```Terraform resource "google_compute_instance" "mirror" { name = "my-instance" machine_type = "e2-medium" boot_disk { initialize_params { image = "debian-cloud/debian-11" } } network_interface { network = google_compute_network.default.id access_config { } } } resource "google_compute_network" "default" { name = "my-network" } resource "google_compute_subnetwork" "default" { name = "my-subnetwork" network = google_compute_network.default.id ip_cidr_range = "10.2.0.0/16" } resource "google_compute_region_backend_service" "default" { name = "my-service" health_checks = [google_compute_health_check.default.id] } resource "google_compute_health_check" "default" { name = "my-healthcheck" check_interval_sec = 1 timeout_sec = 1 tcp_health_check { port = "80" } } resource "google_compute_forwarding_rule" "default" { depends_on = [google_compute_subnetwork.default] name = "my-ilb" is_mirroring_collector = true ip_protocol = "TCP" load_balancing_scheme = "INTERNAL" backend_service = google_compute_region_backend_service.default.id all_ports = true network = google_compute_network.default.id subnetwork = google_compute_subnetwork.default.id network_tier = "PREMIUM" } ``` -------------------------------- ### BigQuery Routine Basic Example Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/bigquery_routine A basic example demonstrating the creation of a BigQuery Routine. ```terraform resource "google_bigquery_routine" "basic" { dataset_id = "my_dataset" routine_id = "my_basic_routine" routine_type = "SCALAR" language = "SQL" return_type = "STRING" definition = "SELECT "hello world"" } ``` -------------------------------- ### Google Compute Instance Template with Startup Script Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_global_forwarding_rule Defines an instance template for creating virtual machine instances. Includes network configuration, disk setup, and a startup script to install Apache, configure SSL, and serve instance metadata. ```terraform resource "google_compute_instance_template" "default" { name = "ssl-proxy-xlb-mig-template" provider = google machine_type = "e2-small" tags = ["allow-health-check"] network_interface { network = google_compute_network.default.id subnetwork = google_compute_subnetwork.default.id access_config { # add external ip to fetch packages } } disk { source_image = "debian-cloud/debian-12" auto_delete = true boot = true } # install nginx and serve a simple web page metadata = { startup-script = <<-EOF1 #! /bin/bash set -euo pipefail export DEBIAN_FRONTEND=noninteractive sudo apt-get update sudo apt-get install -y apache2 jq sudo a2ensite default-ssl sudo a2enmod ssl sudo service apache2 restart NAME=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/hostname") IP=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip") METADATA=$(curl -f -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/?recursive=True" | jq 'del(.["startup-script"])') cat < /var/www/html/index.html

SSL Load Balancer

      Name: $NAME
      IP: $IP
      Metadata: $METADATA
      
EOF EOF1 } lifecycle { create_before_destroy = true } } ``` -------------------------------- ### Example Usage - External VPN Gateway Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_external_vpn_gateway This example demonstrates how to configure an external VPN gateway, an HA VPN gateway, associated network and subnetwork resources, a router, and VPN tunnels to establish a connection between GCP and an external network. It includes the setup for two VPN tunnels for redundancy. ```terraform resource "google_compute_ha_vpn_gateway" "ha_gateway" { region = "us-central1" name = "ha-vpn" network = google_compute_network.network.id } resource "google_compute_external_vpn_gateway" "external_gateway" { name = "external-gateway" redundancy_type = "SINGLE_IP_INTERNALLY_REDUNDANT" description = "An externally managed VPN gateway" interface { id = 0 ip_address = "8.8.8.8" } } resource "google_compute_network" "network" { name = "network-1" routing_mode = "GLOBAL" auto_create_subnetworks = false } resource "google_compute_subnetwork" "network_subnet1" { name = "ha-vpn-subnet-1" ip_cidr_range = "10.0.1.0/24" region = "us-central1" network = google_compute_network.network.id } resource "google_compute_subnetwork" "network_subnet2" { name = "ha-vpn-subnet-2" ip_cidr_range = "10.0.2.0/24" region = "us-west1" network = google_compute_network.network.id } resource "google_compute_router" "router1" { name = "ha-vpn-router1" network = google_compute_network.network.name bgp { asn = 64514 } } resource "google_compute_vpn_tunnel" "tunnel1" { name = "ha-vpn-tunnel1" region = "us-central1" vpn_gateway = google_compute_ha_vpn_gateway.ha_gateway.id peer_external_gateway = google_compute_external_vpn_gateway.external_gateway.id peer_external_gateway_interface = 0 shared_secret = "a secret message" router = google_compute_router.router1.id vpn_gateway_interface = 0 } resource "google_compute_vpn_tunnel" "tunnel2" { name = "ha-vpn-tunnel2" region = "us-central1" vpn_gateway = google_compute_ha_vpn_gateway.ha_gateway.id peer_external_gateway = google_compute_external_vpn_gateway.external_gateway.id peer_external_gateway_interface = 0 shared_secret = "a secret message" router = " ${google_compute_router.router1.id}" vpn_gateway_interface = 1 } resource "google_compute_router_interface" "router1_interface1" { name = "router1-interface1" router = google_compute_router.router1.name region = "us-central1" ip_range = "169.254.0.1/30" vpn_tunnel = google_compute_vpn_tunnel.tunnel1.name } resource "google_compute_router_peer" "router1_peer1" { name = "router1-peer1" router = google_compute_router.router1.name region = "us-central1" peer_ip_address = "169.254.0.2" peer_asn = 64515 advertised_route_priority = 100 interface = google_compute_router_interface.router1_interface1.name } resource "google_compute_router_interface" "router1_interface2" { name = "router1-interface2" router = google_compute_router.router1.name region = "us-central1" ip_range = "169.254.1.1/30" vpn_tunnel = google_compute_vpn_tunnel.tunnel2.name } ``` -------------------------------- ### Terraform CLI Import Examples Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_bulk_per_instance_config These examples show how to import a BulkPerInstanceConfig resource using the `terraform import` command with different accepted formats. ```bash $ terraform import google_compute_bulk_per_instance_config.default projects/{{project}}/zones/{{zone}}/instanceGroupManagers/{{instance_group_manager}} $ terraform import google_compute_bulk_per_instance_config.default {{project}}/{{zone}}/{{instance_group_manager}} $ terraform import google_compute_bulk_per_instance_config.default {{zone}}/{{instance_group_manager}} $ terraform import google_compute_bulk_per_instance_config.default {{instance_group_manager}} ``` -------------------------------- ### App Engine Flexible App Version Example Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/app_engine_flexible_app_version This snippet demonstrates a complete App Engine Flexible application version configuration, including project setup, service account creation, IAM bindings, storage bucket configuration, and the App Engine Flexible App Version resource itself. It covers runtime, deployment, health checks, environment variables, handlers, and scaling. ```terraform resource "google_project" "my_project" { name = "appeng-flex" project_id = "appeng-flex" org_id = "123456789" billing_account = "000000-0000000-0000000-000000" deletion_policy = "DELETE" } resource "google_app_engine_application" "app" { project = google_project.my_project.project_id location_id = "us-central" } resource "google_project_service" "service" { project = google_project.my_project.project_id service = "appengineflex.googleapis.com" disable_dependent_services = false } resource "google_service_account" "custom_service_account" { project = google_project_service.service.project account_id = "my-account" display_name = "Custom Service Account" } resource "google_project_iam_member" "gae_api" { project = google_project_service.service.project role = "roles/compute.networkUser" member = "serviceAccount:${google_service_account.custom_service_account.email}" } resource "google_project_iam_member" "logs_writer" { project = google_project_service.service.project role = "roles/logging.logWriter" member = "serviceAccount:${google_service_account.custom_service_account.email}" } resource "google_project_iam_member" "storage_viewer" { project = google_project_service.service.project role = "roles/storage.objectViewer" member = "serviceAccount:${google_service_account.custom_service_account.email}" } resource "google_app_engine_flexible_app_version" "myapp_v1" { version_id = "v1" project = google_project_iam_member.gae_api.project service = "default" runtime = "nodejs" flexible_runtime_settings { operating_system = "ubuntu24" runtime_version = "24" } entrypoint { shell = "node ./app.js" } deployment { zip { source_url = "https://storage.googleapis.com/${google_storage_bucket.bucket.name}/${google_storage_bucket_object.object.name}" } } liveness_check { path = "/" } readiness_check { path = "/" } env_variables = { port = "8080" } handlers { url_regex = ".*\/my-path\/*" security_level = "SECURE_ALWAYS" login = "LOGIN_REQUIRED" auth_fail_action = "AUTH_FAIL_ACTION_REDIRECT" static_files { path = "my-other-path" upload_path_regex = ".*\/my-path\/*" } } automatic_scaling { cool_down_period = "120s" cpu_utilization { target_utilization = 0.5 } } noop_on_destroy = true service_account = google_service_account.custom_service_account.email } resource "google_storage_bucket" "bucket" { project = google_project.my_project.project_id name = "appengine-static-content" location = "US" } resource "google_storage_bucket_object" "object" { name = "hello-world.zip" bucket = google_storage_bucket.bucket.name source = "./test-fixtures/hello-world.zip" } ``` -------------------------------- ### Terraform CLI Import Examples Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_zone_vm_extension_policy These examples illustrate how to import a ZoneVmExtensionPolicy resource using the Terraform CLI with various accepted formats. ```bash $ terraform import google_compute_zone_vm_extension_policy.default projects/{{project}}/zones/{{zone}}/vmExtensionPolicies/{{name}} $ terraform import google_compute_zone_vm_extension_policy.default {{project}}/{{zone}}/{{name}} $ terraform import google_compute_zone_vm_extension_policy.default {{zone}}/{{name}} $ terraform import google_compute_zone_vm_extension_policy.default {{name}} ``` -------------------------------- ### Compute Interconnect Basic Example Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_interconnect This example demonstrates the basic usage of the google_compute_interconnect resource to create a dedicated interconnect. ```APIDOC ## google_compute_interconnect Represents an Interconnect resource. The Interconnect resource is a dedicated connection between Google's network and your on-premises network. ### Example Usage - Compute Interconnect Basic ```hcl data "google_project" "project" {} resource "google_compute_interconnect" "example-interconnect" { name = "example-interconnect" customer_name = "example_customer" interconnect_type = "DEDICATED" link_type = "LINK_TYPE_ETHERNET_10G_LR" location = "https://www.googleapis.com/compute/v1/${data.google_project.project.id}/global/interconnectLocations/iad-zone1-1" requested_link_count = 1 } ``` ### Argument Reference * `name` - (Required) Name of the resource. Provided by the client when the resource is created. The name must be 1-63 characters long, and comply with RFC1035. Specifically, the name must be 1-63 characters long and match the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?` which means the first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash. * `location` - (Required) URL of the InterconnectLocation object that represents where this connection is to be provisioned. Specifies the location inside Google's Networks. * `link_type` - (Required) Type of link requested. Note that this field indicates the speed of each of the links in the bundle, not the speed of the entire bundle. Can take one of the following values: * `LINK_TYPE_ETHERNET_10G_LR`: A 10G Ethernet with LR optics. * `LINK_TYPE_ETHERNET_100G_LR`: A 100G Ethernet with LR optics. * `LINK_TYPE_ETHERNET_400G_LR4`: A 400G Ethernet with LR4 optics Possible values are: `LINK_TYPE_ETHERNET_10G_LR`, `LINK_TYPE_ETHERNET_100G_LR`, `LINK_TYPE_ETHERNET_400G_LR4`. * `requested_link_count` - (Required) Target number of physical links in the link bundle, as requested by the customer. * `interconnect_type` - (Required) Type of interconnect. Note that a value IT_PRIVATE has been deprecated in favor of DEDICATED. Can take one of the following values: * `PARTNER`: A partner-managed interconnection shared between customers though a partner. * `DEDICATED`: A dedicated physical interconnection with the customer. Possible values are: `DEDICATED`, `PARTNER`, `IT_PRIVATE`. * `description` - (Optional) An optional description of this resource. Provide this property when you create the resource. * `admin_enabled` - (Optional) Administrative status of the interconnect. When this is set to true, the Interconnect is functional and can carry traffic. When set to false, no packets can be carried over the interconnect and no BGP routes are exchanged over it. By default, the status is set to true. * `params` - (Optional) Additional params passed with the request, but not persisted as part of resource payload Structure is documented below. * `noc_contact_email` - (Optional) Email address to contact the customer NOC for operations and maintenance notifications regarding this Interconnect. If specified, this will be used for notifications in addition to all other forms described, such as Cloud Monitoring logs alerting and Cloud Notifications. This field is required for users who sign up for Cloud Interconnect using workforce identity federation. * `customer_name` - (Optional) Customer name, to put in the Letter of Authorization as the party authorized to request a crossconnect. This field is required for Dedicated and Partner Interconnect, should not be specified for cross-cloud interconnect. * `labels` - (Optional) Labels for this resource. These can only be added or modified by the setLabels method. Each label key/value pair must comply with RFC1035. Label values may be empty. **Note** : This field is non-authoritative, and will only manage the labels present in your configuration. Please refer to the field `effective_labels` for all of the labels present on the resource. ``` -------------------------------- ### Terraform Import Command Examples Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_network_firewall_policy_with_rules Examples of importing NetworkFirewallPolicyWithRules using the `terraform import` command with different accepted formats. ```bash $ terraform import google_compute_network_firewall_policy_with_rules.default projects/{{project}}/global/firewallPolicies/{{name}} $ terraform import google_compute_network_firewall_policy_with_rules.default {{project}}/{{name}} $ terraform import google_compute_network_firewall_policy_with_rules.default {{name}} ``` -------------------------------- ### Region Network Firewall Policy Full Example Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_region_network_firewall_policy This example demonstrates the creation of a full region network firewall policy with basic configuration. ```APIDOC ## google_compute_region_network_firewall_policy ### Description Manages a regional network firewall policy. ### Argument Reference * `name` - (Required) User-provided name of the Network firewall policy. The name should be unique in the project in which the firewall policy is created. The name must be 1-63 characters long, and comply with RFC1035. Specifically, the name must be 1-63 characters long and match the regular expression `a-z?` which means the first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash. * `description` - (Optional) An optional description of this resource. Provide this property when you create the resource. * `policy_type` - (Optional) Policy type is used to determine which resources (networks) the policy can be associated with. A policy can be associated with a network only if the network has the matching policyType in its network profile. Different policy types may support some of the Firewall Rules features. Possible values are: `VPC_POLICY`, `RDMA_ROCE_POLICY`, `RDMA_FALCON_POLICY`, `ULL_POLICY`. * `region` - (Optional) The region of this resource. * `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used. * `deletion_policy` - (Optional) Whether Terraform will be prevented from destroying the resource. Defaults to DELETE. When a 'terraform destroy' or 'terraform apply' would delete the resource, the command will fail if this field is set to "PREVENT" in Terraform state. When set to "ABANDON", the command will remove the resource from Terraform management without updating or deleting the resource in the API. When set to "DELETE", deleting the resource is allowed. ### Example Usage - Region Network Firewall Policy Full ```terraform resource "google_compute_region_network_firewall_policy" "policy" { name = "tf-test-policy" description = "Terraform test" } ``` ``` -------------------------------- ### App Engine Standard App Version - Basic Example Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/app_engine_standard_app_version This example demonstrates how to create a new version for a standard App Engine application using a zip deployment. It includes basic configuration for runtime, entrypoint, and environment variables. ```APIDOC ## google_app_engine_standard_app_version ### Description Creates a new version of a standard App Engine application. Supports Zip and File Containers. ### Resource `google_app_engine_standard_app_version` ### Example Usage ```hcl resource "google_app_engine_standard_app_version" "myapp_v1" { version_id = "v1" service = "myapp" runtime = "nodejs22" entrypoint { shell = "node ./app.js" } deployment { zip { source_url = "https://storage.googleapis.com/${google_storage_bucket.bucket.name}/${google_storage_bucket_object.object.name}" } } env_variables = { port = "8080" } automatic_scaling { max_concurrent_requests = 10 min_idle_instances = 1 max_idle_instances = 3 min_pending_latency = "1s" max_pending_latency = "5s" standard_scheduler_settings { target_cpu_utilization = 0.5 target_throughput_utilization = 0.75 min_instances = 2 max_instances = 10 } } delete_service_on_destroy = true service_account = google_service_account.custom_service_account.email } ``` ``` -------------------------------- ### google_compute_instance_group Data Source Example Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_instance_group Example of how to use the google_compute_instance_group data source to get an instance group by name and zone. ```APIDOC ## Data Source: google_compute_instance_group ### Description Use this data source to get a Compute Instance Group within GCE. ### Method Not applicable (Data Source) ### Endpoint Not applicable (Data Source) ### Parameters #### Argument Reference - `name` (Optional) - The name of the instance group. Either `name` or `self_link` must be provided. - `project` (Optional) - The ID of the project in which the resource belongs. If it is not provided, the provider project is used. - `self_link` (Optional) - The self link of the instance group. Either `name` or `self_link` must be provided. - `zone` (Optional) - The zone of the instance group. If referencing the instance group by name and `zone` is not provided, the provider zone is used. ### Request Example ```terraform data "google_compute_instance_group" "all" { name = "instance-group-name" zone = "us-central1-a" } ``` ### Response #### Attributes Reference - `description` - Textual description of the instance group. - `instances` - List of instances in the group. - `named_port` - List of named ports in the group. - `network` - The URL of the network the instance group is in. - `self_link` - The URI of the resource. - `size` - The number of instances in the group. ``` -------------------------------- ### Basic google_compute_disk_async_replication Example Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_disk_async_replication This example demonstrates how to set up asynchronous replication between two Compute Engine disks. Ensure the primary and secondary disks are defined before configuring the replication resource. ```terraform resource "google_compute_disk" "primary-disk" { name = "primary-disk" type = "pd-ssd" zone = "europe-west4-a" physical_block_size_bytes = 4096 } resource "google_compute_disk" "secondary-disk" { name = "secondary-disk" type = "pd-ssd" zone = "europe-west3-a" async_primary_disk { disk = google_compute_disk.primary-disk.id } physical_block_size_bytes = 4096 } resource "google_compute_disk_async_replication" "replication" { primary_disk = google_compute_disk.primary-disk.id secondary_disk { disk = google_compute_disk.secondary-disk.id } } ``` -------------------------------- ### google_compute_health_check Data Source Example Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_health_check Example usage of the google_compute_health_check data source to get information about a health check named 'my-hc'. ```APIDOC ## google_compute_health_check Get information about a HealthCheck. ### Example Usage ``` data "google_compute_health_check" "health_check" { name = "my-hc" } ``` ### Argument Reference The following arguments are supported: * `name` - (Required) Name of the resource. * `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used. ### Attributes Reference See google_compute_health_check resource for details of the available attributes. ``` -------------------------------- ### Sql Database Basic Usage Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database Example of how to create a basic SQL database instance. ```APIDOC ## google_sql_database Provides a Cloud SQL database resource. ### Argument Reference The following arguments are supported: * `name` - (Required) The name of the database in the Cloud SQL instance. This does not include the project ID or instance name. * `instance` - (Required) The name of the Cloud SQL instance. This does not include the project ID. * `charset` - (Optional) The charset value. See MySQL's Supported Character Sets and Collations and Postgres' Character Set Support for more details and supported values. Postgres databases only support a value of `UTF8` at creation time. * `collation` - (Optional) The collation value. See MySQL's Supported Character Sets and Collations and Postgres' Collation Support for more details and supported values. Postgres databases only support a value of `en_US.UTF8` at creation time. * `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used. * `deletion_policy` - (Optional) Whether Terraform will be prevented from destroying the resource. Defaults to DELETE. When a 'terraform destroy' or 'terraform apply' would delete the resource, the command will fail if this field is set to "PREVENT" in Terraform state. When set to "ABANDON", the command will remove the resource from Terraform management without updating or deleting the resource in the API. When set to "DELETE", deleting the resource is allowed. ### Attributes Reference In addition to the arguments listed above, the following computed attributes are exported: * `id` - an identifier for the resource with format `projects/{{project}}/instances/{{instance}}/databases/{{name}}` * `self_link` - The URI of the created resource. ### Timeouts This resource provides the following Timeouts configuration options: * `create` - Default is 20 minutes. * `update` - Default is 20 minutes. * `delete` - Default is 20 minutes. ### Import Database can be imported using any of these accepted formats: * `projects/{{project}}/instances/{{instance}}/databases/{{name}}` * `instances/{{instance}}/databases/{{name}}` * `{{project}}/{{instance}}/{{name}}` * `{{instance}}/{{name}}` In Terraform v1.12.0 and later, use an `identity` block to import Database using identity values. For example: ```terraform import { identity = { name = "<-required value->" instance = "<-required value->" project = "<-optional value->" } to = google_sql_database.default } ``` In Terraform v1.5.0 and later, use an `import` block to import Database using one of the formats above. For example: ```terraform import { id = "projects/{{project}}/instances/{{instance}}/databases/{{name}}" to = google_sql_database.default } ``` When using the `terraform import` command, Database can be imported using one of the formats above. For example: ```bash $ terraform import google_sql_database.default projects/{{project}}/instances/{{instance}}/databases/{{name}} $ terraform import google_sql_database.default instances/{{instance}}/databases/{{name}} $ terraform import google_sql_database.default {{project}}/{{instance}}/{{name}} $ terraform import google_sql_database.default {{instance}}/{{name}} ``` ### User Project Overrides This resource supports User Project Overrides. ``` -------------------------------- ### Get a specific variable Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_instance_guest_attributes This example shows how to retrieve the value of a specific guest attribute variable using its key. ```APIDOC ## Get a specific variable ### Description Retrieves the value of a specific guest attribute variable from a Compute Engine instance using its unique key. ### Method `data "google_compute_instance_guest_attributes"` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Arguments * `name` - (Optional) The name or self_link of the instance. * `project` - (Optional) The ID of the project in which the resource belongs. If `self_link` is provided, this value is ignored. If neither `self_link` nor `project` are provided, the provider project is used. * `zone` - (Optional) The zone of the instance. If `self_link` is provided, this value is ignored. If neither `self_link` nor `zone` are provided, the provider zone is used. * `variable_key` - (Optional) Key of a variable to get the value of. Consists of `namespace` name and `key` name for the variable separated by a `/`. ### Request Example ```hcl data "google_compute_instance_guest_attributes" "appserver_ga" { name = "primary-application-server" zone = "us-central1-a" variable_key = "variables/key1" } ``` ### Response #### Success Response (200) * `variable_value` - Value of the queried guest_attribute. #### Response Example ```json { "variable_value": "some_value" } ``` ``` -------------------------------- ### Get a forwarding rule Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_forwarding_rule This example demonstrates how to retrieve a forwarding rule by its name using the google_compute_forwarding_rule data source. ```APIDOC ## google_compute_forwarding_rule Get a forwarding rule within GCE from its name. ### Example Usage ``` data "google_compute_forwarding_rule" "my-forwarding-rule" { name = "forwarding-rule-us-east1" } ``` ### Argument Reference The following arguments are supported: * `name` - (Required) The name of the forwarding rule. * `project` - (Optional) The project in which the resource belongs. If it is not provided, the provider project is used. * `region` - (Optional) The region in which the resource belongs. If it is not provided, the project region is used. ### Attributes Reference See google_compute_forwarding_rule resource for details of the available attributes. ``` -------------------------------- ### Example: VM Instance Template with Machine Family Preference Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_machine_types Create an instance template, preferring a specific machine family (`c3`) if available, and falling back to other families (`c2`, `n2`) if the preferred one is not found. This allows for flexible instance provisioning based on availability and preference. ```terraform data "google_compute_machine_types" "example" { filter = "memoryMb = 16384 AND guestCpus = 4" zone = var.zone } resource "google_compute_instance_template" "example" { machine_type = coalescelist( [for mt in data.google_compute_machine_types.example.machine_types: mt.name if startswith(mt.name, "c3-")], [for mt in data.google_compute_machine_types.example.machine_types: mt.name if startswith(mt.name, "c2-")], [for mt in data.google_compute_machine_types.example.machine_types: mt.name if startswith(mt.name, "n2-")], )[0] disk { source_image = "debian-cloud/debian-11" auto_delete = true boot = true } } ``` -------------------------------- ### Example Usage - Compute Bulk Per Instance Config Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_bulk_per_instance_config This example demonstrates how to set up a compute bulk per-instance configuration for an instance group manager. It includes creating an instance template, an instance group manager, and then defining per-instance configurations for specific instances. ```terraform data "google_compute_image" "my_image" { family = "debian-12" project = "debian-cloud" } resource "google_compute_instance_template" "bulk-igm" { name = "bulk-igm-template" machine_type = "e2-medium" disk { source_image = data.google_compute_image.my_image.self_link auto_delete = true boot = true } network_interface { network = "default" } } resource "google_compute_instance_group_manager" "bulk-igm" { description = "Terraform test bulk instance group manager" name = "bulk-igm" zone = "us-central1-a" base_instance_name = "bulk-igm" version { name = "prod" instance_template = google_compute_instance_template.bulk-igm.self_link } lifecycle { # Bulk per-instance configs manage the number of instances, so ignore target_size changes. ignore_changes = [target_size] } } resource "google_compute_bulk_per_instance_config" "bulk-igm-per-instance-config" { zone = google_compute_instance_group_manager.bulk-igm.zone instance_group_manager = google_compute_instance_group_manager.bulk-igm.name instances { name = "per-instance-config-instance-1" } instances { name = "per-instance-config-instance-2" } } ``` -------------------------------- ### Example Usage of google_compute_network Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_network Use this data source to get a network within GCE by its name. The 'name' argument is required. ```terraform data "google_compute_network" "my-network" { name = "default-us-east1" } ``` -------------------------------- ### Example: VM Instance Template by Memory and CPU Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_machine_types Create a VM instance template for each machine type that has specific memory and CPU available in a given zone. This is useful for provisioning instances with precise resource requirements. ```terraform data "google_compute_machine_types" "example" { filter = "memoryMb = 16384 AND guestCpus = 8" zone = var.zone } resource "google_compute_instance_template" "example" { for_each = toset(data.google_compute_machine_types.example.machine_types[*].name) machine_type = each.value disk { source_image = "debian-cloud/debian-11" auto_delete = true boot = true } } ``` -------------------------------- ### Get all attributes from a single namespace Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_instance_guest_attributes This example demonstrates how to fetch all guest attributes within a specified namespace for a Compute Engine instance. ```APIDOC ## Get all attributes from a single namespace ### Description Retrieves all guest attributes within a specified namespace for a given Compute Engine instance. ### Method `data "google_compute_instance_guest_attributes"` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Arguments * `name` - (Optional) The name or self_link of the instance. * `project` - (Optional) The ID of the project in which the resource belongs. If `self_link` is provided, this value is ignored. If neither `self_link` nor `project` are provided, the provider project is used. * `zone` - (Optional) The zone of the instance. If `self_link` is provided, this value is ignored. If neither `self_link` nor `zone` are provided, the provider zone is used. * `query_path` - (Optional) Path to query for the guest attributes. Consists of `namespace` name for the attributes followed with a `/`. ### Request Example ```hcl data "google_compute_instance_guest_attributes" "appserver_ga" { name = "primary-application-server" zone = "us-central1-a" query_path = "variables/" } ``` ### Response #### Success Response (200) * `query_value` - Structure is documented below. #### Response Example ```json { "query_value": [ { "key": "key1", "namespace": "variables", "value": "value1" } ] } ``` The `query_value` block supports: * `key` - Key of the guest_attribute. * `namespace` - Namespace of the guest_attribute. * `value` - Value of the guest_attribute. ``` -------------------------------- ### Get and Search Google Cloud Folders Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/folder Use this data source to retrieve information about a Google Cloud Folder. You can get a folder by its ID or search for it using specific fields. The example also shows how to output exported attributes like organization and parent. ```terraform data "google_folder" "my_folder_1" { folder = "folders/12345" lookup_organization = true } data "google_folder" "my_folder_2" { folder = "folders/23456" } output "my_folder_1_organization" { value = data.google_folder.my_folder_1.organization } output "my_folder_2_parent" { value = data.google_folder.my_folder_2.parent } ``` -------------------------------- ### Example List Resource Configuration for Service Accounts Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/using_list_resources_with_terraform_query This example demonstrates how to define a `list` block for querying all Google Service Accounts. The `provider` argument specifies the Google provider configuration to use, and the `config` block can contain provider-specific arguments. ```hcl list "google_service_account" "all" { provider = google config { # Provider-specific arguments; see each list resource's documentation. } } ``` -------------------------------- ### Example Usage of google_cloudfunctions_function Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/cloudfunctions_function Use this data source to get information about a specific Google Cloud Function. The `name` argument is required to identify the function. ```terraform data "google_cloudfunctions_function" "my-function" { name = "function" } ``` -------------------------------- ### Instance Template with Startup Script Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_global_forwarding_rule Creates an instance template for the managed instance group. The startup script installs Nginx and configures a basic web page with instance metadata. ```terraform # instance template resource "google_compute_instance_template" "instance_template" { name = "l7-gilb-mig-template" provider = google-beta machine_type = "e2-small" tags = ["http-server"] network_interface { network = google_compute_network.gilb_network.id subnetwork = google_compute_subnetwork.gilb_subnet.id access_config { # add external ip to fetch packages } } disk { source_image = "debian-cloud/debian-12" auto_delete = true boot = true } # install nginx and serve a simple web page metadata = { startup-script = <<-EOF1 #! /bin/bash set -euo pipefail export DEBIAN_FRONTEND=noninteractive apt-get update apt-get install -y nginx-light jq NAME=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/hostname") IP=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip") METADATA=$(curl -f -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/?recursive=True" | jq 'del(.["startup-script"])') cat < /var/www/html/index.html
      Name: $NAME
      IP: $IP
      Metadata: $METADATA
      
EOF EOF1 } lifecycle { create_before_destroy = true } } ``` -------------------------------- ### Create a Compute Image with Guest OS Features Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_image This example demonstrates creating a compute image and specifying various guest OS features. This is useful for images that require specific OS capabilities or configurations. ```Terraform data "google_compute_image" "debian" { family = "debian-12" project = "debian-cloud" } resource "google_compute_disk" "persistent" { name = "example-disk" image = data.google_compute_image.debian.self_link size = 10 type = "pd-ssd" zone = "us-central1-a" } resource "google_compute_image" "example" { name = "example-image" source_disk = google_compute_disk.persistent.id guest_os_features { type = "UEFI_COMPATIBLE" } guest_os_features { type = "VIRTIO_SCSI_MULTIQUEUE" } guest_os_features { type = "GVNIC" } guest_os_features { type = "SEV_CAPABLE" } guest_os_features { type = "SEV_LIVE_MIGRATABLE_V2" } } ``` -------------------------------- ### Example Usage of google_compute_ssl_policy Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_ssl_policy Use this data source to get an SSL Policy within GCE by its name. This is useful for configuring Target HTTPS and Target SSL Proxies. ```terraform data "google_compute_ssl_policy" "my-ssl-policy" { name = "production-ssl-policy" } ``` -------------------------------- ### Example Usage of google_compute_region_ssl_policy Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_region_ssl_policy Use this data source to get the details of a regional SSL policy by its name. The policy can then be referenced in other resources like Target HTTPS Proxies. ```terraform data "google_compute_region_ssl_policy" "my-ssl-policy" { name = "production-ssl-policy" } ``` -------------------------------- ### Manage Cloud SQL MySQL Instance with Provision Script Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_provision_script This example demonstrates managing a Cloud SQL MySQL instance, including setting up IAM authentication and executing a SQL script for database provisioning. The script can be sourced from a file. ```terraform resource "google_sql_database_instance" "instance" { name = "my-instance" database_version = "MYSQL_8_4" settings { tier = "db-perf-optimized-N-2" data_api_access = "ALLOW_DATA_API" database_flags { name = "cloudsql_iam_authentication" value = "on" } } } ``` ```terraform # Create a database user for your account and grant roles so it has privilege to access the database. # Set the type to "CLOUD_IAM_USER" for huamn account or "CLOUD_IAM_SERVICE_ACCOUNT" for service account. # If a service account is used and the instance is Postgres, please trim the ".gserviceaccount.com" suffix # to avoid exceeding the username length limit. resource "google_sql_user" "iam_user" { name = "account-used-to-apply-this-config@example.com" instance = google_sql_database_instance.instance.name type = "CLOUD_IAM_USER" # Roles granted to the user. Smaller roles are preferred, if exist. database_roles = ["cloudsqlsuperuser"] # This field doesn't support MySQL 5.6 and 5.7. } ``` ```terraform resource "google_sql_provision_script" "script" { instance = google_sql_database_instance.instance.name script = file("${path.module}/script.sql") # Some of your queries may require a database. You can create and use a database in the script or explicitly reference a google_sql_database. # database = google_sql_database.database.name description = "sql script to create DBs and tables" depends_on = [google_sql_user.iam_user] } ``` -------------------------------- ### Example Usage of google_compute_subnetwork Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_subnetwork Use this data source to get details about a specific subnetwork by providing its name and region. The `self_link` argument can be used as an alternative to `name`, `project`, and `region`. ```terraform data "google_compute_subnetwork" "my-subnetwork" { name = "default-us-east1" region = "us-east1" } ``` -------------------------------- ### Create a Private IP SQL Instance Source: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance This example configures a SQL instance with private IP connectivity, requiring a VPC network peering setup with Google Service Networking. ```terraform resource "google_compute_network" "private_network" { provider = google-beta name = "private-network" } resource "google_compute_global_address" "private_ip_address" { provider = google-beta name = "private-ip-address" purpose = "VPC_PEERING" address_type = "INTERNAL" prefix_length = 16 network = google_compute_network.private_network.id } resource "google_service_networking_connection" "private_vpc_connection" { provider = google-beta network = google_compute_network.private_network.id service = "servicenetworking.googleapis.com" reserved_peering_ranges = [google_compute_global_address.private_ip_address.name] } resource "random_id" "db_name_suffix" { byte_length = 4 } resource "google_sql_database_instance" "instance" { provider = google-beta name = "private-instance-${random_id.db_name_suffix.hex}" region = "us-central1" database_version = "MYSQL_5_7" depends_on = [google_service_networking_connection.private_vpc_connection] settings { tier = "db-f1-micro" ip_configuration { ipv4_enabled = false private_network = google_compute_network.private_network.self_link enable_private_path_for_google_cloud_services = true } } } provider "google-beta" { region = "us-central1" zone = "us-central1-a" } ```