Google Compute Engine Security Policy

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

google_compute_security_policy (Terraform)

The Security Policy in Compute Engine can be configured in Terraform with the resource name google_compute_security_policy. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub
resource "google_compute_security_policy" "policy" {
  name    = "internal"
  project = var.project

  rule {
    action   = "deny(403)"
resource "google_compute_security_policy" "policy" {
    name = "my-policy"
    rule {
        action = "deny(403)"
        priority = "1000"
        match {
resource "google_compute_security_policy" "policy" {
    name = "my-policy"
    rule {
        action = "deny(403)"
        priority = "1000"
        match {
resource "google_compute_security_policy" "security-policy-1" {
  name        = local.google_compute_security_policy_frontend_name
  project     = google_project.in_scope.project_id
  description = "Web application security policy"

  # Reject all traffic that hasn't been whitelisted.
resource "google_compute_security_policy" "allow_crowe" {
  name = "allow-crowe"

  rule {
    action   = "allow"
    priority = "1000"

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).


An optional description of this security policy. Max size is 2048.

Fingerprint of this resource.

  • id optional computed - string
  • name required - string

The name of the security policy.

The project in which the resource belongs. If it is not provided, the provider project is used.

The URI of the created resource.

  • rule set block

    Action to take when match matches the request. Valid values: "allow" : allow access to target, "deny(status)" : deny access to target, returns the HTTP response code specified (valid values are 403, 404 and 502)

    An optional description of this rule. Max size is 64.

    When set to true, the action specified above is not enforced. Stackdriver logs for requests that trigger a preview action are annotated as such.

    An unique positive integer indicating the priority of evaluation for a rule. Rules are evaluated from highest priority (lowest numerically) to lowest priority (highest numerically) in order.

    • match list block

      Predefined rule expression. If this field is specified, config must also be specified. Available options: SRC_IPS_V1: Must specify the corresponding src_ip_ranges field in config.

      • config list block

        Set of IP addresses or ranges (IPV4 or IPV6) in CIDR notation to match against inbound traffic. There is a limit of 10 IP ranges per rule. A value of '*' matches all IPs (can be used to override the default behavior).

      • expr list block

        Textual representation of an expression in Common Expression Language syntax. The application context of the containing message determines which well-known feature set of CEL is supported.

  • timeouts single block

Explanation in Terraform Registry

A Security Policy defines an IP blacklist or whitelist that protects load balanced Google Cloud services by denying or permitting traffic from specified IP ranges. For more information see the official documentation and the API. Security Policy is used by google_compute_backend_service.

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 Security Policy?

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

Where can I find the example code for the Google Compute Engine Security Policy?

For Terraform, the intetunder-temp/terraform-modules, osmdilya/terraform-google and Mirlan777/terraform-google source code examples are useful. See the Terraform Example section for further details.


Automate config file reviews on your commits

Fix issues in your infrastructure as code with auto-generated patches.