Skip to content

Resource: aws_subnet#

Provides an VPC subnet resource.

For more information, see the documentation on Subnets.

Example Usage#

Basic Usage#

resource "aws_vpc" "example" {
  cidr_block = "10.1.0.0/16"
}

resource "aws_subnet" "example" {
  vpc_id     = aws_vpc.example.id
  cidr_block = "10.1.1.0/24"

  tags = {
    Name = "Main"
  }
}

Argument Reference#

The following arguments are supported:

  • availability_zone - (Optional) AZ for the subnet.
  • cidr_block - (Required) The IPv4 CIDR block for the subnet.
  • map_public_ip_on_launch - (Optional) Indicates whether public IP addresses will be associated with instances created in this subnet. Addresses are associated only if there are available allocated Elastic IP addresses. Default is false.
  • tags - (Optional) A map of tags to assign to the resource. If configured with a provider default_tags configuration block present, tags with matching keys will overwrite those defined at the provider-level.
  • vpc_id - (Required) The VPC ID.

Attributes Reference#

Supported attributes#

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

Unsupported attributes#

~> Note These attributes may be present in the terraform.tfstate file but they have preset values and cannot be specified in configuration files.

The following attributes are not currently supported:

arn, assign_ipv6_address_on_creation, availability_zone_id, customer_owned_ipv4_pool, enable_dns64, enable_resource_name_dns_aaaa_record_on_launch, enable_resource_name_dns_a_record_on_launch, ipv6_cidr_block_association_id, ipv6_native, map_customer_owned_ip_on_launch, outpost_arn, owner_id, private_dns_hostname_type_on_launch.

Timeouts#

The timeouts block allows you to specify timeouts for certain actions:

  • create - (Default 10m) How long to wait for a subnet to be created.
  • delete - (Default 20m) How long to wait for a subnet to be deleted.

Import#

Subnets can be imported using the subnet id, e.g.,

$ terraform import aws_subnet.public_subnet subnet-12345678