Class Subnet


  • public class Subnet
    extends com.pulumi.resources.CustomResource
    Provides an VPC subnet resource. > **NOTE:** Due to [AWS Lambda improved VPC networking changes that began deploying in September 2019](https://aws.amazon.com/blogs/compute/announcing-improved-vpc-networking-for-aws-lambda-functions/), subnets associated with Lambda Functions can take up to 45 minutes to successfully delete. To allow for successful deletion, the provider will wait for at least 45 minutes even if a shorter delete timeout is specified. > **NOTE:** When AWS GuardDuty is enabled in your account, it automatically creates VPC endpoints in your VPCs to monitor network traffic. During subnet deletion, the provider automatically detects and dissociates the subnet from these GuardDuty-managed VPC endpoints if they are blocking the deletion, preserving GuardDuty protection for other subnets in the VPC. This cleanup only targets resources tagged with `GuardDutyManaged=true` and happens automatically during destroy operations with no manual intervention required. For optimal functionality, the IAM role used by Terraform should have the optional permissions listed below. If these permissions are not available, the provider will continue with the deletion attempt and surface warnings only if the deletion ultimately fails. ## Example Usage ### Basic Usage
     
     package generated_program;
     
     import com.pulumi.Context;
     import com.pulumi.Pulumi;
     import com.pulumi.core.Output;
     import com.pulumi.aws.ec2.Subnet;
     import com.pulumi.aws.ec2.SubnetArgs;
     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 main = new Subnet("main", SubnetArgs.builder()
                 .vpcId(mainAwsVpc.id())
                 .cidrBlock("10.0.1.0/24")
                 .tags(Map.of("Name", "Main"))
                 .build());
     
         }
     }
     
     
    ### Subnets In Secondary VPC CIDR Blocks When managing subnets in one of a VPC's secondary CIDR blocks created using a `aws.ec2.VpcIpv4CidrBlockAssociation` resource, it is recommended to reference that resource's `vpcId` attribute to ensure correct dependency ordering.
     
     package generated_program;
     
     import com.pulumi.Context;
     import com.pulumi.Pulumi;
     import com.pulumi.core.Output;
     import com.pulumi.aws.ec2.VpcIpv4CidrBlockAssociation;
     import com.pulumi.aws.ec2.VpcIpv4CidrBlockAssociationArgs;
     import com.pulumi.aws.ec2.Subnet;
     import com.pulumi.aws.ec2.SubnetArgs;
     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 secondaryCidr = new VpcIpv4CidrBlockAssociation("secondaryCidr", VpcIpv4CidrBlockAssociationArgs.builder()
                 .vpcId(main.id())
                 .cidrBlock("172.20.0.0/16")
                 .build());
     
             var inSecondaryCidr = new Subnet("inSecondaryCidr", SubnetArgs.builder()
                 .vpcId(secondaryCidr.vpcId())
                 .cidrBlock("172.20.0.0/24")
                 .build());
     
         }
     }
     
     
    ### IPAM-Managed Subnets
     
     package generated_program;
     
     import com.pulumi.Context;
     import com.pulumi.Pulumi;
     import com.pulumi.core.Output;
     import com.pulumi.aws.AwsFunctions;
     import com.pulumi.aws.inputs.GetRegionArgs;
     import com.pulumi.aws.ec2.VpcIpam;
     import com.pulumi.aws.ec2.VpcIpamArgs;
     import com.pulumi.aws.ec2.inputs.VpcIpamOperatingRegionArgs;
     import com.pulumi.aws.ec2.VpcIpamPool;
     import com.pulumi.aws.ec2.VpcIpamPoolArgs;
     import com.pulumi.aws.ec2.VpcIpamPoolCidr;
     import com.pulumi.aws.ec2.VpcIpamPoolCidrArgs;
     import com.pulumi.aws.ec2.Vpc;
     import com.pulumi.aws.ec2.VpcArgs;
     import com.pulumi.aws.ec2.inputs.VpcIpamPoolSourceResourceArgs;
     import com.pulumi.aws.ec2.Subnet;
     import com.pulumi.aws.ec2.SubnetArgs;
     import com.pulumi.resources.CustomResourceOptions;
     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) {
             final var current = AwsFunctions.getRegion(GetRegionArgs.builder()
                 .build());
     
             var test = new VpcIpam("test", VpcIpamArgs.builder()
                 .operatingRegions(VpcIpamOperatingRegionArgs.builder()
                     .regionName(current.region())
                     .build())
                 .build());
     
             var testVpcIpamPool = new VpcIpamPool("testVpcIpamPool", VpcIpamPoolArgs.builder()
                 .addressFamily("ipv4")
                 .ipamScopeId(test.privateDefaultScopeId())
                 .locale(current.region())
                 .build());
     
             var testVpcIpamPoolCidr = new VpcIpamPoolCidr("testVpcIpamPoolCidr", VpcIpamPoolCidrArgs.builder()
                 .ipamPoolId(testVpcIpamPool.id())
                 .cidr("10.0.0.0/16")
                 .build());
     
             var testVpc = new Vpc("testVpc", VpcArgs.builder()
                 .ipv4IpamPoolId(testVpcIpamPool.id())
                 .ipv4NetmaskLength(24)
                 .build(), CustomResourceOptions.builder()
                     .dependsOn(testVpcIpamPoolCidr)
                     .build());
     
             var vpc = new VpcIpamPool("vpc", VpcIpamPoolArgs.builder()
                 .sourceResource(VpcIpamPoolSourceResourceArgs.builder()
                     .resourceId(testVpc.id())
                     .resourceOwner(currentAwsCallerIdentity.accountId())
                     .resourceRegion(current.region())
                     .resourceType("vpc")
                     .build())
                 .addressFamily("ipv4")
                 .ipamScopeId(test.privateDefaultScopeId())
                 .locale(current.region())
                 .sourceIpamPoolId(testVpcIpamPool.id())
                 .build());
     
             var vpcVpcIpamPoolCidr = new VpcIpamPoolCidr("vpcVpcIpamPoolCidr", VpcIpamPoolCidrArgs.builder()
                 .ipamPoolId(vpc.id())
                 .cidr(testVpc.cidrBlock())
                 .build());
     
             var testSubnet = new Subnet("testSubnet", SubnetArgs.builder()
                 .vpcId(testVpc.id())
                 .ipv4IpamPoolId(vpc.id())
                 .ipv4NetmaskLength(28)
                 .availabilityZone(available.names()[0])
                 .build(), CustomResourceOptions.builder()
                     .dependsOn(vpcVpcIpamPoolCidr)
                     .build());
     
         }
     }
     
     
    ## Import ### Identity Schema #### Required * `id` (String) ID of the subnet. #### Optional * `accountId` (String) AWS Account where this resource is managed. * `region` (String) Region where this resource is managed. Using `pulumi import`, import subnets using the subnet `id`. For example: ```sh $ pulumi import aws:ec2/subnet:Subnet example subnet-9d4a7b6c ```
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.pulumi.resources.CustomResource

        com.pulumi.resources.CustomResource.CustomResourceInternal
      • Nested classes/interfaces inherited from class com.pulumi.resources.Resource

        com.pulumi.resources.Resource.LazyField<T extends java.lang.Object>, com.pulumi.resources.Resource.LazyFields, com.pulumi.resources.Resource.ResourceInternal
    • Field Summary

      • Fields inherited from class com.pulumi.resources.Resource

        childResources, remote
    • Constructor Summary

      Constructors 
      Constructor Description
      Subnet​(java.lang.String name)  
      Subnet​(java.lang.String name, SubnetArgs args)  
      Subnet​(java.lang.String name, SubnetArgs args, com.pulumi.resources.CustomResourceOptions options)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      com.pulumi.core.Output<java.lang.String> arn()  
      com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> assignIpv6AddressOnCreation()  
      com.pulumi.core.Output<java.lang.String> availabilityZone()  
      com.pulumi.core.Output<java.lang.String> availabilityZoneId()  
      com.pulumi.core.Output<java.lang.String> cidrBlock()  
      com.pulumi.core.Output<java.util.Optional<java.lang.String>> customerOwnedIpv4Pool()  
      com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> enableDns64()  
      com.pulumi.core.Output<java.util.Optional<java.lang.Integer>> enableLniAtDeviceIndex()  
      com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> enableResourceNameDnsAaaaRecordOnLaunch()  
      com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> enableResourceNameDnsARecordOnLaunch()  
      static Subnet get​(java.lang.String name, com.pulumi.core.Output<java.lang.String> id, SubnetState state, com.pulumi.resources.CustomResourceOptions options)
      Get an existing Host resource's state with the given name, ID, and optional extra properties used to qualify the lookup.
      com.pulumi.core.Output<java.util.Optional<java.lang.String>> ipv4IpamPoolId()  
      com.pulumi.core.Output<java.util.Optional<java.lang.Integer>> ipv4NetmaskLength()  
      com.pulumi.core.Output<java.lang.String> ipv6CidrBlock()  
      com.pulumi.core.Output<java.lang.String> ipv6CidrBlockAssociationId()  
      com.pulumi.core.Output<java.util.Optional<java.lang.String>> ipv6IpamPoolId()  
      com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> ipv6Native()  
      com.pulumi.core.Output<java.util.Optional<java.lang.Integer>> ipv6NetmaskLength()  
      com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> mapCustomerOwnedIpOnLaunch()  
      com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> mapPublicIpOnLaunch()  
      com.pulumi.core.Output<java.util.Optional<java.lang.String>> outpostArn()  
      com.pulumi.core.Output<java.lang.String> ownerId()  
      com.pulumi.core.Output<java.lang.String> privateDnsHostnameTypeOnLaunch()  
      com.pulumi.core.Output<java.lang.String> region()  
      com.pulumi.core.Output<java.util.Optional<java.util.Map<java.lang.String,​java.lang.String>>> tags()  
      com.pulumi.core.Output<java.util.Map<java.lang.String,​java.lang.String>> tagsAll()  
      com.pulumi.core.Output<java.lang.String> vpcId()  
      • Methods inherited from class com.pulumi.resources.CustomResource

        getId, id, idFuture
      • Methods inherited from class com.pulumi.resources.Resource

        getChildResources, getResourceName, getResourceType, getUrn, pulumiChildResources, pulumiResourceName, pulumiResourceType, urn
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Subnet

        public Subnet​(java.lang.String name)
        Parameters:
        name - The _unique_ name of the resulting resource.
      • Subnet

        public Subnet​(java.lang.String name,
                      SubnetArgs args)
        Parameters:
        name - The _unique_ name of the resulting resource.
        args - The arguments to use to populate this resource's properties.
      • Subnet

        public Subnet​(java.lang.String name,
                      SubnetArgs args,
                      @Nullable
                      com.pulumi.resources.CustomResourceOptions options)
        Parameters:
        name - The _unique_ name of the resulting resource.
        args - The arguments to use to populate this resource's properties.
        options - A bag of options that control this resource's behavior.
    • Method Detail

      • arn

        public com.pulumi.core.Output<java.lang.String> arn()
        Returns:
        The ARN of the subnet.
      • assignIpv6AddressOnCreation

        public com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> assignIpv6AddressOnCreation()
        Returns:
        Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address. Default is `false`
      • availabilityZone

        public com.pulumi.core.Output<java.lang.String> availabilityZone()
        Returns:
        AZ for the subnet.
      • availabilityZoneId

        public com.pulumi.core.Output<java.lang.String> availabilityZoneId()
        Returns:
        AZ ID of the subnet. This argument is not supported in all regions or partitions. If necessary, use `availabilityZone` instead.
      • cidrBlock

        public com.pulumi.core.Output<java.lang.String> cidrBlock()
        Returns:
        The IPv4 CIDR block for the subnet.
      • customerOwnedIpv4Pool

        public com.pulumi.core.Output<java.util.Optional<java.lang.String>> customerOwnedIpv4Pool()
        Returns:
        The customer owned IPv4 address pool. Typically used with the `mapCustomerOwnedIpOnLaunch` argument. The `outpostArn` argument must be specified when configured.
      • enableDns64

        public com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> enableDns64()
        Returns:
        Indicates whether DNS queries made to the Amazon-provided DNS Resolver in this subnet should return synthetic IPv6 addresses for IPv4-only destinations. Default: `false`.
      • enableLniAtDeviceIndex

        public com.pulumi.core.Output<java.util.Optional<java.lang.Integer>> enableLniAtDeviceIndex()
        Returns:
        Indicates the device position for local network interfaces in this subnet. For example, 1 indicates local network interfaces in this subnet are the secondary network interface (eth1). A local network interface cannot be the primary network interface (eth0).
      • enableResourceNameDnsARecordOnLaunch

        public com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> enableResourceNameDnsARecordOnLaunch()
        Returns:
        Indicates whether to respond to DNS queries for instance hostnames with DNS A records. Default: `false`.
      • enableResourceNameDnsAaaaRecordOnLaunch

        public com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> enableResourceNameDnsAaaaRecordOnLaunch()
        Returns:
        Indicates whether to respond to DNS queries for instance hostnames with DNS AAAA records. Default: `false`.
      • ipv4IpamPoolId

        public com.pulumi.core.Output<java.util.Optional<java.lang.String>> ipv4IpamPoolId()
        Returns:
        ID of an IPv4 VPC Resource Planning IPAM Pool. The CIDR of this pool is used to allocate the CIDR for the subnet.
      • ipv4NetmaskLength

        public com.pulumi.core.Output<java.util.Optional<java.lang.Integer>> ipv4NetmaskLength()
        Returns:
        Netmask. Requires specifying a `ipv4IpamPoolId`.
      • ipv6CidrBlock

        public com.pulumi.core.Output<java.lang.String> ipv6CidrBlock()
        Returns:
        The IPv6 network range for the subnet, in CIDR notation. The subnet size must use a /64 prefix length. If the existing IPv6 subnet was created with `assignIpv6AddressOnCreation = true`, changing this value will force resource recreation.
      • ipv6CidrBlockAssociationId

        public com.pulumi.core.Output<java.lang.String> ipv6CidrBlockAssociationId()
        Returns:
        The association ID for the IPv6 CIDR block.
      • ipv6IpamPoolId

        public com.pulumi.core.Output<java.util.Optional<java.lang.String>> ipv6IpamPoolId()
        Returns:
        ID of an IPv6 VPC Resource Planning IPAM Pool. The CIDR of this pool is used to allocate the CIDR for the subnet.
      • ipv6Native

        public com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> ipv6Native()
        Returns:
        Indicates whether to create an IPv6-only subnet. Default: `false`.
      • ipv6NetmaskLength

        public com.pulumi.core.Output<java.util.Optional<java.lang.Integer>> ipv6NetmaskLength()
        Returns:
        Netmask. Requires specifying a `ipv6IpamPoolId`. Valid values are from 44 to 64 in increments of 4.
      • mapCustomerOwnedIpOnLaunch

        public com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> mapCustomerOwnedIpOnLaunch()
        Returns:
        Specify `true` to indicate that network interfaces created in the subnet should be assigned a customer owned IP address. The `customerOwnedIpv4Pool` and `outpostArn` arguments must be specified when set to `true`. Default is `false`.
      • mapPublicIpOnLaunch

        public com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> mapPublicIpOnLaunch()
        Returns:
        Specify true to indicate that instances launched into the subnet should be assigned a public IP address. Default is `false`.
      • outpostArn

        public com.pulumi.core.Output<java.util.Optional<java.lang.String>> outpostArn()
        Returns:
        ARN of the Outpost.
      • ownerId

        public com.pulumi.core.Output<java.lang.String> ownerId()
        Returns:
        The ID of the AWS account that owns the subnet.
      • privateDnsHostnameTypeOnLaunch

        public com.pulumi.core.Output<java.lang.String> privateDnsHostnameTypeOnLaunch()
        Returns:
        The type of hostnames to assign to instances in the subnet at launch. For IPv6-only subnets, an instance DNS name must be based on the instance ID. For dual-stack and IPv4-only subnets, you can specify whether DNS names use the instance IPv4 address or the instance ID. Valid values: `ip-name`, `resource-name`.
      • region

        public com.pulumi.core.Output<java.lang.String> region()
        Returns:
        Region where this resource will be [managed](https://docs.aws.amazon.com/general/latest/gr/rande.html#regional-endpoints). Defaults to the Region set in the provider configuration.
      • tags

        public com.pulumi.core.Output<java.util.Optional<java.util.Map<java.lang.String,​java.lang.String>>> tags()
        Returns:
        A map of tags to assign to the resource. .If configured with a provider `defaultTags` configuration block present, tags with matching keys will overwrite those defined at the provider-level.
      • tagsAll

        public com.pulumi.core.Output<java.util.Map<java.lang.String,​java.lang.String>> tagsAll()
        Returns:
        A map of tags assigned to the resource, including those inherited from the provider `defaultTags` configuration block.
      • vpcId

        public com.pulumi.core.Output<java.lang.String> vpcId()
        Returns:
        The VPC ID.
      • get

        public static Subnet get​(java.lang.String name,
                                 com.pulumi.core.Output<java.lang.String> id,
                                 @Nullable
                                 SubnetState state,
                                 @Nullable
                                 com.pulumi.resources.CustomResourceOptions options)
        Get an existing Host resource's state with the given name, ID, and optional extra properties used to qualify the lookup.
        Parameters:
        name - The _unique_ name of the resulting resource.
        id - The _unique_ provider ID of the resource to lookup.
        state -
        options - Optional settings to control the behavior of the CustomResource.