Firewall class

Each network has its own firewall controlling access to and from the instances.

All traffic to instances, even from other instances, is blocked by the firewall unless firewall rules are created to allow it.

The default network has automatically created firewall rules that are shown in default firewall rules. No manually created network has automatically created firewall rules except for a default "allow" rule for outgoing traffic and a default "deny" for incoming traffic. For all networks except the default network, you must create any firewall rules you need.

To get more information about Firewall, see:

Example Usage

Firewall Basic

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

const defaultNetwork = new gcp.compute.Network("default", {name: "test-network"});
const _default = new gcp.compute.Firewall("default", {
    name: "test-firewall",
    network: defaultNetwork.name,
    allows: [
        {
            protocol: "icmp",
        },
        {
            protocol: "tcp",
            ports: [
                "80",
                "8080",
                "1000-2000",
            ],
        },
    ],
    sourceTags: ["web"],
});
import pulumi
import pulumi_gcp as gcp

default_network = gcp.compute.Network("default", name="test-network")
default = gcp.compute.Firewall("default",
    name="test-firewall",
    network=default_network.name,
    allows=[
        {
            "protocol": "icmp",
        },
        {
            "protocol": "tcp",
            "ports": [
                "80",
                "8080",
                "1000-2000",
            ],
        },
    ],
    source_tags=["web"])
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Gcp = Pulumi.Gcp;

return await Deployment.RunAsync(() =>
{
    var defaultNetwork = new Gcp.Compute.Network("default", new()
    {
        Name = "test-network",
    });

    var @default = new Gcp.Compute.Firewall("default", new()
    {
        Name = "test-firewall",
        Network = defaultNetwork.Name,
        Allows = new[]
        {
            new Gcp.Compute.Inputs.FirewallAllowArgs
            {
                Protocol = "icmp",
            },
            new Gcp.Compute.Inputs.FirewallAllowArgs
            {
                Protocol = "tcp",
                Ports = new[]
                {
                    "80",
                    "8080",
                    "1000-2000",
                },
            },
        },
        SourceTags = new[]
        {
            "web",
        },
    });

});
package main

import (
	"github.com/pulumi/pulumi-gcp/sdk/v9/go/gcp/compute"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		defaultNetwork, err := compute.NewNetwork(ctx, "default", &compute.NetworkArgs{
			Name: pulumi.String("test-network"),
		})
		if err != nil {
			return err
		}
		_, err = compute.NewFirewall(ctx, "default", &compute.FirewallArgs{
			Name:    pulumi.String("test-firewall"),
			Network: defaultNetwork.Name,
			Allows: compute.FirewallAllowArray{
				&compute.FirewallAllowArgs{
					Protocol: pulumi.String("icmp"),
				},
				&compute.FirewallAllowArgs{
					Protocol: pulumi.String("tcp"),
					Ports: pulumi.StringArray{
						pulumi.String("80"),
						pulumi.String("8080"),
						pulumi.String("1000-2000"),
					},
				},
			},
			SourceTags: pulumi.StringArray{
				pulumi.String("web"),
			},
		})
		if err != nil {
			return err
		}
		return nil
	})
}
pulumi {
  required_providers {
    gcp = {
      source = "pulumi/gcp"
    }
  }
}

resource "gcp_compute_firewall" "default" {
  name    = "test-firewall"
  network = gcp_compute_network.default.name
  allows {
    protocol = "icmp"
  }
  allows {
    protocol = "tcp"
    ports    = ["80", "8080", "1000-2000"]
  }
  source_tags = ["web"]
}
resource "gcp_compute_network" "default" {
  name = "test-network"
}
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.compute.Network;
import com.pulumi.gcp.compute.NetworkArgs;
import com.pulumi.gcp.compute.Firewall;
import com.pulumi.gcp.compute.FirewallArgs;
import com.pulumi.gcp.compute.inputs.FirewallAllowArgs;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) {
        Pulumi.run(App::stack);
    }

    public static void stack(Context ctx) {
        var defaultNetwork = new Network("defaultNetwork", NetworkArgs.builder()
            .name("test-network")
            .build());

        var default_ = new Firewall("default", FirewallArgs.builder()
            .name("test-firewall")
            .network(defaultNetwork.name())
            .allows(
                FirewallAllowArgs.builder()
                    .protocol("icmp")
                    .build(),
                FirewallAllowArgs.builder()
                    .protocol("tcp")
                    .ports(
                        "80",
                        "8080",
                        "1000-2000")
                    .build())
            .sourceTags("web")
            .build());

    }
}
resources:
  default:
    type: gcp:compute:Firewall
    properties:
      name: test-firewall
      network: ${defaultNetwork.name}
      allows:
        - protocol: icmp
        - protocol: tcp
          ports:
            - '80'
            - '8080'
            - 1000-2000
      sourceTags:
        - web
  defaultNetwork:
    type: gcp:compute:Network
    name: default
    properties:
      name: test-network

Firewall With Target Tags

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

const rules = new gcp.compute.Firewall("rules", {
    project: "my-project-name",
    name: "my-firewall-rule",
    network: "default",
    description: "Creates firewall rule targeting tagged instances",
    allows: [{
        protocol: "tcp",
        ports: [
            "80",
            "8080",
            "1000-2000",
        ],
    }],
    sourceTags: ["foo"],
    targetTags: ["web"],
});
import pulumi
import pulumi_gcp as gcp

rules = gcp.compute.Firewall("rules",
    project="my-project-name",
    name="my-firewall-rule",
    network="default",
    description="Creates firewall rule targeting tagged instances",
    allows=[{
        "protocol": "tcp",
        "ports": [
            "80",
            "8080",
            "1000-2000",
        ],
    }],
    source_tags=["foo"],
    target_tags=["web"])
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Gcp = Pulumi.Gcp;

return await Deployment.RunAsync(() =>
{
    var rules = new Gcp.Compute.Firewall("rules", new()
    {
        Project = "my-project-name",
        Name = "my-firewall-rule",
        Network = "default",
        Description = "Creates firewall rule targeting tagged instances",
        Allows = new[]
        {
            new Gcp.Compute.Inputs.FirewallAllowArgs
            {
                Protocol = "tcp",
                Ports = new[]
                {
                    "80",
                    "8080",
                    "1000-2000",
                },
            },
        },
        SourceTags = new[]
        {
            "foo",
        },
        TargetTags = new[]
        {
            "web",
        },
    });

});
package main

import (
	"github.com/pulumi/pulumi-gcp/sdk/v9/go/gcp/compute"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		_, err := compute.NewFirewall(ctx, "rules", &compute.FirewallArgs{
			Project:     pulumi.String("my-project-name"),
			Name:        pulumi.String("my-firewall-rule"),
			Network:     pulumi.String("default"),
			Description: pulumi.String("Creates firewall rule targeting tagged instances"),
			Allows: compute.FirewallAllowArray{
				&compute.FirewallAllowArgs{
					Protocol: pulumi.String("tcp"),
					Ports: pulumi.StringArray{
						pulumi.String("80"),
						pulumi.String("8080"),
						pulumi.String("1000-2000"),
					},
				},
			},
			SourceTags: pulumi.StringArray{
				pulumi.String("foo"),
			},
			TargetTags: pulumi.StringArray{
				pulumi.String("web"),
			},
		})
		if err != nil {
			return err
		}
		return nil
	})
}
pulumi {
  required_providers {
    gcp = {
      source = "pulumi/gcp"
    }
  }
}

resource "gcp_compute_firewall" "rules" {
  project     = "my-project-name"
  name        = "my-firewall-rule"
  network     = "default"
  description = "Creates firewall rule targeting tagged instances"
  allows {
    protocol = "tcp"
    ports    = ["80", "8080", "1000-2000"]
  }
  source_tags = ["foo"]
  target_tags = ["web"]
}
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.compute.Firewall;
import com.pulumi.gcp.compute.FirewallArgs;
import com.pulumi.gcp.compute.inputs.FirewallAllowArgs;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) {
        Pulumi.run(App::stack);
    }

    public static void stack(Context ctx) {
        var rules = new Firewall("rules", FirewallArgs.builder()
            .project("my-project-name")
            .name("my-firewall-rule")
            .network("default")
            .description("Creates firewall rule targeting tagged instances")
            .allows(FirewallAllowArgs.builder()
                .protocol("tcp")
                .ports(
                    "80",
                    "8080",
                    "1000-2000")
                .build())
            .sourceTags("foo")
            .targetTags("web")
            .build());

    }
}
resources:
  rules:
    type: gcp:compute:Firewall
    properties:
      project: my-project-name
      name: my-firewall-rule
      network: default
      description: Creates firewall rule targeting tagged instances
      allows:
        - protocol: tcp
          ports:
            - '80'
            - '8080'
            - 1000-2000
      sourceTags:
        - foo
      targetTags:
        - web

Import

Firewall can be imported using any of these accepted formats:

  • projects/{{project}}/global/firewalls/{{name}}
  • {{project}}/{{name}}
  • {{name}}

When using the pulumi import command, Firewall can be imported using one of the formats above. For example:

$ pulumi import gcp:compute/firewall:Firewall default projects/{{project}}/global/firewalls/{{name}}
$ pulumi import gcp:compute/firewall:Firewall default {{project}}/{{name}}
$ pulumi import gcp:compute/firewall:Firewall default {{name}}

Constructors

Firewall(String name, {FirewallArgs? args, CustomResourceOptions? options})
Creates a new Firewall. name The Pulumi resource name. args Arguments used to configure this Firewall. The set of arguments for Firewall. options Resource options controlling this resource's behavior.
Firewall.reference(String urn)
Creates a typed reference to an existing Firewall resource.

Properties

allows ↔ Output<List<FirewallAllow>?>
The list of ALLOW rules specified by this firewall. Each rule specifies a protocol and port-range tuple that describes a permitted connection. Structure is documented below.
latefinal
childResources → Set<Resource>
finalinherited
completionSources ↔ Map<String, IOutputCompletionSource>
latefinalinherited
creationTimestamp ↔ Output<String>
Creation timestamp in RFC3339 text format.
latefinal
deletionPolicy ↔ Output<String>
Whether Terraform will be prevented from destroying the resource. Defaults to DELETE. When a 'terraform destroy' or 'pulumi up' 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.
latefinal
denies ↔ Output<List<FirewallDeny>?>
The list of DENY rules specified by this firewall. Each rule specifies a protocol and port-range tuple that describes a denied connection. Structure is documented below.
latefinal
description ↔ Output<String?>
An optional description of this resource. Provide this property when you create the resource.
latefinal
destinationRanges ↔ Output<List<String>>
If destination ranges are specified, the firewall will apply only to traffic that has destination IP address in these ranges. These ranges must be expressed in CIDR format. IPv4 or IPv6 ranges are supported.
latefinal
direction ↔ Output<String>
Direction of traffic to which this firewall applies; default is INGRESS. Note: For INGRESS traffic, one of sourceRanges, sourceTags or sourceServiceAccounts is required. Possible values are: INGRESS, EGRESS.
latefinal
disabled ↔ Output<bool?>
Denotes whether the firewall rule is disabled, i.e not applied to the network it is associated with. When set to true, the firewall rule is not enforced and the network behaves as if it did not exist. If this is unspecified, the firewall rule will be enabled.
latefinal
enableLogging ↔ Output<bool>
This field denotes whether to enable logging for a particular firewall rule. If logging is enabled, logs will be exported to Stackdriver. Deprecated in favor of logConfig
latefinal
hashCode → int
The hash code for this object.
no setterinherited
id ↔ Output<String>
getter/setter pairinherited
isCustom → bool
Returns whether this resource is provider-managed.
no setterinherited
isProtected → bool
Returns whether this resource is protected from deletion.
no setterinherited
isRemote → bool
Whether this resource is registered as remote.
no setterinherited
isResourceReference → bool
Whether this instance represents a resource value returned over RPC.
finalinherited
logConfig ↔ Output<FirewallLogConfig?>
This field denotes the logging options for a particular firewall rule. If defined, logging is enabled, and logs will be exported to Cloud Logging. Structure is documented below.
latefinal
name ↔ Output<String>
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? 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.
latefinal
network ↔ Output<String>
The name or selfLink of the network to attach this firewall to.
latefinal
params ↔ Output<FirewallParams?>
Additional params passed with the request, but not persisted as part of resource payload Structure is documented below.
latefinal
priority ↔ Output<int?>
Priority for this rule. This is an integer between 0 and 65535, both inclusive. When not specified, the value assumed is 1000. Relative priorities determine precedence of conflicting rules. Lower value of priority implies higher precedence (eg, a rule with priority 0 has higher precedence than a rule with priority 1). DENY rules take precedence over ALLOW rules having equal priority.
latefinal
project ↔ Output<String>
The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
latefinal
resourceTransforms → List<ResourceTransform>
Inherited/explicit async transforms.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
The URI of the created resource.
latefinal
sourceRanges ↔ Output<List<String>?>
If source ranges are specified, the firewall will apply only to traffic that has source IP address in these ranges. These ranges must be expressed in CIDR format. One or both of sourceRanges and sourceTags may be set. If both properties are set, the firewall will apply to traffic that has source IP address within sourceRanges OR the source IP that belongs to a tag listed in the sourceTags property. The connection does not need to match both properties for the firewall to apply. IPv4 or IPv6 ranges are supported. For INGRESS traffic, one of sourceRanges, sourceTags or sourceServiceAccounts is required.
latefinal
sourceServiceAccounts ↔ Output<List<String>?>
If source service accounts are specified, the firewall will apply only to traffic originating from an instance with a service account in this list. Source service accounts cannot be used to control traffic to an instance's external IP address because service accounts are associated with an instance, not an IP address. sourceRanges can be set at the same time as sourceServiceAccounts. If both are set, the firewall will apply to traffic that has source IP address within sourceRanges OR the source IP belongs to an instance with service account listed in sourceServiceAccount. The connection does not need to match both properties for the firewall to apply. sourceServiceAccounts cannot be used at the same time as sourceTags or targetTags. For INGRESS traffic, one of sourceRanges, sourceTags or sourceServiceAccounts is required.
latefinal
sourceTags ↔ Output<List<String>?>
If source tags are specified, the firewall will apply only to traffic with source IP that belongs to a tag listed in source tags. Source tags cannot be used to control traffic to an instance's external IP address. Because tags are associated with an instance, not an IP address. One or both of sourceRanges and sourceTags may be set. If both properties are set, the firewall will apply to traffic that has source IP address within sourceRanges OR the source IP that belongs to a tag listed in the sourceTags property. The connection does not need to match both properties for the firewall to apply. For INGRESS traffic, one of sourceRanges, sourceTags or sourceServiceAccounts is required.
latefinal
targetServiceAccounts ↔ Output<List<String>?>
A list of service accounts indicating sets of instances located in the network that may make network connections as specified in allowed[]. targetServiceAccounts cannot be used at the same time as targetTags or sourceTags. If neither targetServiceAccounts nor targetTags are specified, the firewall rule applies to all instances on the specified network.
latefinal
targetTags ↔ Output<List<String>?>
A list of instance tags indicating sets of instances located in the network that may make network connections as specified in allowed[]. If no targetTags are specified, the firewall rule applies to all instances on the specified network.
latefinal
transformations → List<ResourceTransformation>
Inherited/explicit legacy transformations.
no setterinherited
urn ↔ Output<String>
latefinalinherited

Methods

failId(Object error) → void
Completes this resource ID with an error when registration fails.
inherited
failOutputs(Object error) → void
Completes all output properties with error.
inherited
failUrn(Object error) → void
Completes this resource URN with an error when registration fails.
inherited
getProvider(String moduleMember) → ProviderResource?
Returns provider for moduleMember's package, if configured.
inherited
getResourceName() → String
Returns this resource's logical name.
inherited
getResourceType() → String
Returns this resource's Pulumi type token.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
registerOutput<T>(String propertyName, {Object? decoder(Object?)?, bool isSecret = false}) → Output<T>
Registers a dynamic output property for this resource.
inherited
resolveId(String? value, {required bool isKnown}) → void
Resolves the provider-assigned ID for this resource.
inherited
resolveOutputs(Struct outputs) → void
Resolves all output properties from a monitor response payload.
inherited
resolveUrn(String value) → void
Resolves this resource's URN once assigned by the engine.
inherited
serializeProperties(Map<String, dynamic> properties) → Future<Struct>
Serializes resource properties for RPC transmission.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

get(String name, Input<String> id, {FirewallState? state, CustomResourceOptions? options}) → Firewall
Gets an existing Firewall resource's state with the given name and id.