1. Packages
  2. Azure Classic
  3. API Docs
  4. network
  5. SubnetNetworkSecurityGroupAssociation

We recommend using Azure Native.

Azure v6.21.0 published on Friday, Mar 7, 2025 by Pulumi

azure.network.SubnetNetworkSecurityGroupAssociation

Explore with Pulumi AI

Associates a Network Security Group with a Subnet within a Virtual Network.

Example Usage

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

const example = new azure.core.ResourceGroup("example", {
    name: "example-resources",
    location: "West Europe",
});
const exampleVirtualNetwork = new azure.network.VirtualNetwork("example", {
    name: "example-network",
    addressSpaces: ["10.0.0.0/16"],
    location: example.location,
    resourceGroupName: example.name,
});
const exampleSubnet = new azure.network.Subnet("example", {
    name: "frontend",
    resourceGroupName: example.name,
    virtualNetworkName: exampleVirtualNetwork.name,
    addressPrefixes: ["10.0.2.0/24"],
});
const exampleNetworkSecurityGroup = new azure.network.NetworkSecurityGroup("example", {
    name: "example-nsg",
    location: example.location,
    resourceGroupName: example.name,
    securityRules: [{
        name: "test123",
        priority: 100,
        direction: "Inbound",
        access: "Allow",
        protocol: "Tcp",
        sourcePortRange: "*",
        destinationPortRange: "*",
        sourceAddressPrefix: "*",
        destinationAddressPrefix: "*",
    }],
});
const exampleSubnetNetworkSecurityGroupAssociation = new azure.network.SubnetNetworkSecurityGroupAssociation("example", {
    subnetId: exampleSubnet.id,
    networkSecurityGroupId: exampleNetworkSecurityGroup.id,
});
Copy
import pulumi
import pulumi_azure as azure

example = azure.core.ResourceGroup("example",
    name="example-resources",
    location="West Europe")
example_virtual_network = azure.network.VirtualNetwork("example",
    name="example-network",
    address_spaces=["10.0.0.0/16"],
    location=example.location,
    resource_group_name=example.name)
example_subnet = azure.network.Subnet("example",
    name="frontend",
    resource_group_name=example.name,
    virtual_network_name=example_virtual_network.name,
    address_prefixes=["10.0.2.0/24"])
example_network_security_group = azure.network.NetworkSecurityGroup("example",
    name="example-nsg",
    location=example.location,
    resource_group_name=example.name,
    security_rules=[{
        "name": "test123",
        "priority": 100,
        "direction": "Inbound",
        "access": "Allow",
        "protocol": "Tcp",
        "source_port_range": "*",
        "destination_port_range": "*",
        "source_address_prefix": "*",
        "destination_address_prefix": "*",
    }])
example_subnet_network_security_group_association = azure.network.SubnetNetworkSecurityGroupAssociation("example",
    subnet_id=example_subnet.id,
    network_security_group_id=example_network_security_group.id)
Copy
package main

import (
	"github.com/pulumi/pulumi-azure/sdk/v6/go/azure/core"
	"github.com/pulumi/pulumi-azure/sdk/v6/go/azure/network"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		example, err := core.NewResourceGroup(ctx, "example", &core.ResourceGroupArgs{
			Name:     pulumi.String("example-resources"),
			Location: pulumi.String("West Europe"),
		})
		if err != nil {
			return err
		}
		exampleVirtualNetwork, err := network.NewVirtualNetwork(ctx, "example", &network.VirtualNetworkArgs{
			Name: pulumi.String("example-network"),
			AddressSpaces: pulumi.StringArray{
				pulumi.String("10.0.0.0/16"),
			},
			Location:          example.Location,
			ResourceGroupName: example.Name,
		})
		if err != nil {
			return err
		}
		exampleSubnet, err := network.NewSubnet(ctx, "example", &network.SubnetArgs{
			Name:               pulumi.String("frontend"),
			ResourceGroupName:  example.Name,
			VirtualNetworkName: exampleVirtualNetwork.Name,
			AddressPrefixes: pulumi.StringArray{
				pulumi.String("10.0.2.0/24"),
			},
		})
		if err != nil {
			return err
		}
		exampleNetworkSecurityGroup, err := network.NewNetworkSecurityGroup(ctx, "example", &network.NetworkSecurityGroupArgs{
			Name:              pulumi.String("example-nsg"),
			Location:          example.Location,
			ResourceGroupName: example.Name,
			SecurityRules: network.NetworkSecurityGroupSecurityRuleArray{
				&network.NetworkSecurityGroupSecurityRuleArgs{
					Name:                     pulumi.String("test123"),
					Priority:                 pulumi.Int(100),
					Direction:                pulumi.String("Inbound"),
					Access:                   pulumi.String("Allow"),
					Protocol:                 pulumi.String("Tcp"),
					SourcePortRange:          pulumi.String("*"),
					DestinationPortRange:     pulumi.String("*"),
					SourceAddressPrefix:      pulumi.String("*"),
					DestinationAddressPrefix: pulumi.String("*"),
				},
			},
		})
		if err != nil {
			return err
		}
		_, err = network.NewSubnetNetworkSecurityGroupAssociation(ctx, "example", &network.SubnetNetworkSecurityGroupAssociationArgs{
			SubnetId:               exampleSubnet.ID(),
			NetworkSecurityGroupId: exampleNetworkSecurityGroup.ID(),
		})
		if err != nil {
			return err
		}
		return nil
	})
}
Copy
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Azure = Pulumi.Azure;

return await Deployment.RunAsync(() => 
{
    var example = new Azure.Core.ResourceGroup("example", new()
    {
        Name = "example-resources",
        Location = "West Europe",
    });

    var exampleVirtualNetwork = new Azure.Network.VirtualNetwork("example", new()
    {
        Name = "example-network",
        AddressSpaces = new[]
        {
            "10.0.0.0/16",
        },
        Location = example.Location,
        ResourceGroupName = example.Name,
    });

    var exampleSubnet = new Azure.Network.Subnet("example", new()
    {
        Name = "frontend",
        ResourceGroupName = example.Name,
        VirtualNetworkName = exampleVirtualNetwork.Name,
        AddressPrefixes = new[]
        {
            "10.0.2.0/24",
        },
    });

    var exampleNetworkSecurityGroup = new Azure.Network.NetworkSecurityGroup("example", new()
    {
        Name = "example-nsg",
        Location = example.Location,
        ResourceGroupName = example.Name,
        SecurityRules = new[]
        {
            new Azure.Network.Inputs.NetworkSecurityGroupSecurityRuleArgs
            {
                Name = "test123",
                Priority = 100,
                Direction = "Inbound",
                Access = "Allow",
                Protocol = "Tcp",
                SourcePortRange = "*",
                DestinationPortRange = "*",
                SourceAddressPrefix = "*",
                DestinationAddressPrefix = "*",
            },
        },
    });

    var exampleSubnetNetworkSecurityGroupAssociation = new Azure.Network.SubnetNetworkSecurityGroupAssociation("example", new()
    {
        SubnetId = exampleSubnet.Id,
        NetworkSecurityGroupId = exampleNetworkSecurityGroup.Id,
    });

});
Copy
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.azure.core.ResourceGroup;
import com.pulumi.azure.core.ResourceGroupArgs;
import com.pulumi.azure.network.VirtualNetwork;
import com.pulumi.azure.network.VirtualNetworkArgs;
import com.pulumi.azure.network.Subnet;
import com.pulumi.azure.network.SubnetArgs;
import com.pulumi.azure.network.NetworkSecurityGroup;
import com.pulumi.azure.network.NetworkSecurityGroupArgs;
import com.pulumi.azure.network.inputs.NetworkSecurityGroupSecurityRuleArgs;
import com.pulumi.azure.network.SubnetNetworkSecurityGroupAssociation;
import com.pulumi.azure.network.SubnetNetworkSecurityGroupAssociationArgs;
import java.util.List;
import java.util.ArrayList;
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 example = new ResourceGroup("example", ResourceGroupArgs.builder()
            .name("example-resources")
            .location("West Europe")
            .build());

        var exampleVirtualNetwork = new VirtualNetwork("exampleVirtualNetwork", VirtualNetworkArgs.builder()
            .name("example-network")
            .addressSpaces("10.0.0.0/16")
            .location(example.location())
            .resourceGroupName(example.name())
            .build());

        var exampleSubnet = new Subnet("exampleSubnet", SubnetArgs.builder()
            .name("frontend")
            .resourceGroupName(example.name())
            .virtualNetworkName(exampleVirtualNetwork.name())
            .addressPrefixes("10.0.2.0/24")
            .build());

        var exampleNetworkSecurityGroup = new NetworkSecurityGroup("exampleNetworkSecurityGroup", NetworkSecurityGroupArgs.builder()
            .name("example-nsg")
            .location(example.location())
            .resourceGroupName(example.name())
            .securityRules(NetworkSecurityGroupSecurityRuleArgs.builder()
                .name("test123")
                .priority(100)
                .direction("Inbound")
                .access("Allow")
                .protocol("Tcp")
                .sourcePortRange("*")
                .destinationPortRange("*")
                .sourceAddressPrefix("*")
                .destinationAddressPrefix("*")
                .build())
            .build());

        var exampleSubnetNetworkSecurityGroupAssociation = new SubnetNetworkSecurityGroupAssociation("exampleSubnetNetworkSecurityGroupAssociation", SubnetNetworkSecurityGroupAssociationArgs.builder()
            .subnetId(exampleSubnet.id())
            .networkSecurityGroupId(exampleNetworkSecurityGroup.id())
            .build());

    }
}
Copy
resources:
  example:
    type: azure:core:ResourceGroup
    properties:
      name: example-resources
      location: West Europe
  exampleVirtualNetwork:
    type: azure:network:VirtualNetwork
    name: example
    properties:
      name: example-network
      addressSpaces:
        - 10.0.0.0/16
      location: ${example.location}
      resourceGroupName: ${example.name}
  exampleSubnet:
    type: azure:network:Subnet
    name: example
    properties:
      name: frontend
      resourceGroupName: ${example.name}
      virtualNetworkName: ${exampleVirtualNetwork.name}
      addressPrefixes:
        - 10.0.2.0/24
  exampleNetworkSecurityGroup:
    type: azure:network:NetworkSecurityGroup
    name: example
    properties:
      name: example-nsg
      location: ${example.location}
      resourceGroupName: ${example.name}
      securityRules:
        - name: test123
          priority: 100
          direction: Inbound
          access: Allow
          protocol: Tcp
          sourcePortRange: '*'
          destinationPortRange: '*'
          sourceAddressPrefix: '*'
          destinationAddressPrefix: '*'
  exampleSubnetNetworkSecurityGroupAssociation:
    type: azure:network:SubnetNetworkSecurityGroupAssociation
    name: example
    properties:
      subnetId: ${exampleSubnet.id}
      networkSecurityGroupId: ${exampleNetworkSecurityGroup.id}
Copy

Create SubnetNetworkSecurityGroupAssociation Resource

Resources are created with functions called constructors. To learn more about declaring and configuring resources, see Resources.

Constructor syntax

new SubnetNetworkSecurityGroupAssociation(name: string, args: SubnetNetworkSecurityGroupAssociationArgs, opts?: CustomResourceOptions);
@overload
def SubnetNetworkSecurityGroupAssociation(resource_name: str,
                                          args: SubnetNetworkSecurityGroupAssociationArgs,
                                          opts: Optional[ResourceOptions] = None)

@overload
def SubnetNetworkSecurityGroupAssociation(resource_name: str,
                                          opts: Optional[ResourceOptions] = None,
                                          network_security_group_id: Optional[str] = None,
                                          subnet_id: Optional[str] = None)
func NewSubnetNetworkSecurityGroupAssociation(ctx *Context, name string, args SubnetNetworkSecurityGroupAssociationArgs, opts ...ResourceOption) (*SubnetNetworkSecurityGroupAssociation, error)
public SubnetNetworkSecurityGroupAssociation(string name, SubnetNetworkSecurityGroupAssociationArgs args, CustomResourceOptions? opts = null)
public SubnetNetworkSecurityGroupAssociation(String name, SubnetNetworkSecurityGroupAssociationArgs args)
public SubnetNetworkSecurityGroupAssociation(String name, SubnetNetworkSecurityGroupAssociationArgs args, CustomResourceOptions options)
type: azure:network:SubnetNetworkSecurityGroupAssociation
properties: # The arguments to resource properties.
options: # Bag of options to control resource's behavior.

Parameters

name This property is required. string
The unique name of the resource.
args This property is required. SubnetNetworkSecurityGroupAssociationArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.
resource_name This property is required. str
The unique name of the resource.
args This property is required. SubnetNetworkSecurityGroupAssociationArgs
The arguments to resource properties.
opts ResourceOptions
Bag of options to control resource's behavior.
ctx Context
Context object for the current deployment.
name This property is required. string
The unique name of the resource.
args This property is required. SubnetNetworkSecurityGroupAssociationArgs
The arguments to resource properties.
opts ResourceOption
Bag of options to control resource's behavior.
name This property is required. string
The unique name of the resource.
args This property is required. SubnetNetworkSecurityGroupAssociationArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.
name This property is required. String
The unique name of the resource.
args This property is required. SubnetNetworkSecurityGroupAssociationArgs
The arguments to resource properties.
options CustomResourceOptions
Bag of options to control resource's behavior.

Constructor example

The following reference example uses placeholder values for all input properties.

var subnetNetworkSecurityGroupAssociationResource = new Azure.Network.SubnetNetworkSecurityGroupAssociation("subnetNetworkSecurityGroupAssociationResource", new()
{
    NetworkSecurityGroupId = "string",
    SubnetId = "string",
});
Copy
example, err := network.NewSubnetNetworkSecurityGroupAssociation(ctx, "subnetNetworkSecurityGroupAssociationResource", &network.SubnetNetworkSecurityGroupAssociationArgs{
	NetworkSecurityGroupId: pulumi.String("string"),
	SubnetId:               pulumi.String("string"),
})
Copy
var subnetNetworkSecurityGroupAssociationResource = new SubnetNetworkSecurityGroupAssociation("subnetNetworkSecurityGroupAssociationResource", SubnetNetworkSecurityGroupAssociationArgs.builder()
    .networkSecurityGroupId("string")
    .subnetId("string")
    .build());
Copy
subnet_network_security_group_association_resource = azure.network.SubnetNetworkSecurityGroupAssociation("subnetNetworkSecurityGroupAssociationResource",
    network_security_group_id="string",
    subnet_id="string")
Copy
const subnetNetworkSecurityGroupAssociationResource = new azure.network.SubnetNetworkSecurityGroupAssociation("subnetNetworkSecurityGroupAssociationResource", {
    networkSecurityGroupId: "string",
    subnetId: "string",
});
Copy
type: azure:network:SubnetNetworkSecurityGroupAssociation
properties:
    networkSecurityGroupId: string
    subnetId: string
Copy

SubnetNetworkSecurityGroupAssociation Resource Properties

To learn more about resource properties and how to use them, see Inputs and Outputs in the Architecture and Concepts docs.

Inputs

In Python, inputs that are objects can be passed either as argument classes or as dictionary literals.

The SubnetNetworkSecurityGroupAssociation resource accepts the following input properties:

NetworkSecurityGroupId
This property is required.
Changes to this property will trigger replacement.
string
The ID of the Network Security Group which should be associated with the Subnet. Changing this forces a new resource to be created.
SubnetId
This property is required.
Changes to this property will trigger replacement.
string
The ID of the Subnet. Changing this forces a new resource to be created.
NetworkSecurityGroupId
This property is required.
Changes to this property will trigger replacement.
string
The ID of the Network Security Group which should be associated with the Subnet. Changing this forces a new resource to be created.
SubnetId
This property is required.
Changes to this property will trigger replacement.
string
The ID of the Subnet. Changing this forces a new resource to be created.
networkSecurityGroupId
This property is required.
Changes to this property will trigger replacement.
String
The ID of the Network Security Group which should be associated with the Subnet. Changing this forces a new resource to be created.
subnetId
This property is required.
Changes to this property will trigger replacement.
String
The ID of the Subnet. Changing this forces a new resource to be created.
networkSecurityGroupId
This property is required.
Changes to this property will trigger replacement.
string
The ID of the Network Security Group which should be associated with the Subnet. Changing this forces a new resource to be created.
subnetId
This property is required.
Changes to this property will trigger replacement.
string
The ID of the Subnet. Changing this forces a new resource to be created.
network_security_group_id
This property is required.
Changes to this property will trigger replacement.
str
The ID of the Network Security Group which should be associated with the Subnet. Changing this forces a new resource to be created.
subnet_id
This property is required.
Changes to this property will trigger replacement.
str
The ID of the Subnet. Changing this forces a new resource to be created.
networkSecurityGroupId
This property is required.
Changes to this property will trigger replacement.
String
The ID of the Network Security Group which should be associated with the Subnet. Changing this forces a new resource to be created.
subnetId
This property is required.
Changes to this property will trigger replacement.
String
The ID of the Subnet. Changing this forces a new resource to be created.

Outputs

All input properties are implicitly available as output properties. Additionally, the SubnetNetworkSecurityGroupAssociation resource produces the following output properties:

Id string
The provider-assigned unique ID for this managed resource.
Id string
The provider-assigned unique ID for this managed resource.
id String
The provider-assigned unique ID for this managed resource.
id string
The provider-assigned unique ID for this managed resource.
id str
The provider-assigned unique ID for this managed resource.
id String
The provider-assigned unique ID for this managed resource.

Look up Existing SubnetNetworkSecurityGroupAssociation Resource

Get an existing SubnetNetworkSecurityGroupAssociation resource’s state with the given name, ID, and optional extra properties used to qualify the lookup.

public static get(name: string, id: Input<ID>, state?: SubnetNetworkSecurityGroupAssociationState, opts?: CustomResourceOptions): SubnetNetworkSecurityGroupAssociation
@staticmethod
def get(resource_name: str,
        id: str,
        opts: Optional[ResourceOptions] = None,
        network_security_group_id: Optional[str] = None,
        subnet_id: Optional[str] = None) -> SubnetNetworkSecurityGroupAssociation
func GetSubnetNetworkSecurityGroupAssociation(ctx *Context, name string, id IDInput, state *SubnetNetworkSecurityGroupAssociationState, opts ...ResourceOption) (*SubnetNetworkSecurityGroupAssociation, error)
public static SubnetNetworkSecurityGroupAssociation Get(string name, Input<string> id, SubnetNetworkSecurityGroupAssociationState? state, CustomResourceOptions? opts = null)
public static SubnetNetworkSecurityGroupAssociation get(String name, Output<String> id, SubnetNetworkSecurityGroupAssociationState state, CustomResourceOptions options)
resources:  _:    type: azure:network:SubnetNetworkSecurityGroupAssociation    get:      id: ${id}
name This property is required.
The unique name of the resulting resource.
id This property is required.
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
resource_name This property is required.
The unique name of the resulting resource.
id This property is required.
The unique provider ID of the resource to lookup.
name This property is required.
The unique name of the resulting resource.
id This property is required.
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
name This property is required.
The unique name of the resulting resource.
id This property is required.
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
name This property is required.
The unique name of the resulting resource.
id This property is required.
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
The following state arguments are supported:
NetworkSecurityGroupId Changes to this property will trigger replacement. string
The ID of the Network Security Group which should be associated with the Subnet. Changing this forces a new resource to be created.
SubnetId Changes to this property will trigger replacement. string
The ID of the Subnet. Changing this forces a new resource to be created.
NetworkSecurityGroupId Changes to this property will trigger replacement. string
The ID of the Network Security Group which should be associated with the Subnet. Changing this forces a new resource to be created.
SubnetId Changes to this property will trigger replacement. string
The ID of the Subnet. Changing this forces a new resource to be created.
networkSecurityGroupId Changes to this property will trigger replacement. String
The ID of the Network Security Group which should be associated with the Subnet. Changing this forces a new resource to be created.
subnetId Changes to this property will trigger replacement. String
The ID of the Subnet. Changing this forces a new resource to be created.
networkSecurityGroupId Changes to this property will trigger replacement. string
The ID of the Network Security Group which should be associated with the Subnet. Changing this forces a new resource to be created.
subnetId Changes to this property will trigger replacement. string
The ID of the Subnet. Changing this forces a new resource to be created.
network_security_group_id Changes to this property will trigger replacement. str
The ID of the Network Security Group which should be associated with the Subnet. Changing this forces a new resource to be created.
subnet_id Changes to this property will trigger replacement. str
The ID of the Subnet. Changing this forces a new resource to be created.
networkSecurityGroupId Changes to this property will trigger replacement. String
The ID of the Network Security Group which should be associated with the Subnet. Changing this forces a new resource to be created.
subnetId Changes to this property will trigger replacement. String
The ID of the Subnet. Changing this forces a new resource to be created.

Import

Subnet <-> Network Security Group Associations can be imported using the resource id of the Subnet, e.g.

$ pulumi import azure:network/subnetNetworkSecurityGroupAssociation:SubnetNetworkSecurityGroupAssociation association1 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Network/virtualNetworks/myvnet1/subnets/mysubnet1
Copy

To learn more about importing existing cloud resources, see Importing resources.

Package Details

Repository
Azure Classic pulumi/pulumi-azure
License
Apache-2.0
Notes
This Pulumi package is based on the azurerm Terraform Provider.