Google Compute Engine Firewall Policy Rule

This page shows how to write Terraform for Compute Engine Firewall Policy Rule and write them securely.

google_compute_firewall_policy_rule (Terraform)

The Firewall Policy Rule in Compute Engine can be configured in Terraform with the resource name google_compute_firewall_policy_rule. The following sections describe how to use the resource and its parameters.

Example Usage from GitHub

An example could not be found in GitHub.

Review your Terraform file for Google best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).


The following arguments are supported:

  • action - (Required) The Action to perform when the client connection triggers the rule. Can currently be either "allow" or "deny()" where valid values for status are 403, 404, and 502.
  • direction - (Required) The direction in which this rule applies. Possible values: INGRESS, EGRESS
  • firewall_policy - (Required) The firewall policy of the resource.
  • match - (Required) A match condition that incoming traffic is evaluated against. If it evaluates to true, the corresponding 'action' is enforced. Structure is documented below.
  • priority - (Required) An integer indicating the priority of a rule in the list. The priority must be a positive value between 0 and 2147483647. Rules are evaluated from highest to lowest priority where 0 is the highest priority and 2147483647 is the lowest prority.

The match block supports:

  • dest_ip_ranges - (Optional) CIDR IP address range. Maximum number of destination CIDR IP ranges allowed is 256.
  • layer4_configs - (Required) Pairs of IP protocols and ports that the rule should match. Structure is documented below.
  • src_ip_ranges - (Optional) CIDR IP address range. Maximum number of source CIDR IP ranges allowed is 256.

The layer4_configs block supports:

  • ip_protocol - (Required) The IP protocol to which this rule applies. The protocol type is required when creating a firewall rule. This value can either be one of the following well known protocol strings (tcp, udp, icmp, esp, ah, ipip, sctp), or the IP protocol number.
  • ports - (Optional) An optional list of ports to which this rule applies. This field is only applicable for UDP or TCP protocol. Each entry must be either an integer or a range. If not specified, this rule applies to connections through any port. Example inputs include: ``.

  • description - (Optional) An optional description for this resource.
  • disabled - (Optional) Denotes whether the firewall policy rule is disabled. When set to true, the firewall policy rule is not enforced and traffic behaves as if it did not exist. If this is unspecified, the firewall policy rule will be enabled.
  • enable_logging - (Optional) Denotes whether to enable logging for a particular rule. If logging is enabled, logs will be exported to the configured export destination in Stackdriver. Logs may be exported to BigQuery or Pub/Sub. Note: you cannot enable logging on "goto_next" rules.
  • target_resources - (Optional) A list of network resource URLs to which this rule applies. This field allows you to control which network's VMs get this rule. If this field is left blank, all VMs within the organization will receive the rule.
  • target_service_accounts - (Optional) A list of service accounts indicating the sets of instances that are applied with this rule.

In addition to the arguments listed above, the following computed attributes are exported:

  • id - an identifier for the resource with format locations/global/firewallPolicies/[[firewall_policy]]/rules/[[priority]]

  • kind - Type of the resource. Always compute#firewallPolicyRule for firewall policy rules

  • rule_tuple_count - Calculation of the complexity of a single firewall policy rule.

Explanation in Terraform Registry

Hierarchical firewall policy rules let you create and enforce a consistent firewall policy across your organization. Rules can explicitly allow or deny connections or delegate evaluation to lower level policies. For more information see the official documentation

Tips: Best Practices for The Other Google Compute Engine Resources

In addition to the google_compute_disk, Google Compute Engine has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.



Ensure the encryption key for your GCE disk is stored securely

It is better to store the encryption key for your GCE disk securely. Secret Manager could be used instead.



Ensure your VPC firewall blocks unwanted outbound traffic

It is better to block unwanted outbound traffic not to expose resources in the VPC to unwanted attacks.



Ensure appropriate service account is assigned to your GCE instance

It is better to create a custom service account for the instance and assign it.



Ensure OS login for your GCE instances is enabled at project level

It is better to enable OS login for your GCE instances. Enabling OS login ensures that SSH keys used to connect to instances are mapped with IAM users, allowing centralized and automated SSH key management.



Ensure to use modern TLS protocols

It's better to adopt TLS v1.2+ instead of outdated TLS protocols.



Ensure VPC flow logging is enabled

It is better to enable VPC flow logging. VPC flow logging allows us to audit traffic in your network.

Review your Google Compute Engine settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

Frequently asked questions

What is Google Compute Engine Firewall Policy Rule?

Google Compute Engine Firewall Policy Rule is a resource for Compute Engine of Google Cloud Platform. Settings can be wrote in Terraform.