» aws_subnet

Provides an VPC subnet resource.

» Example Usage

» Basic Usage

resource "aws_subnet" "main" {
  vpc_id     = "${aws_vpc.main.id}"
  cidr_block = ""

  tags = {
    Name = "Main"

» Subnets In Secondary VPC CIDR Blocks

When managing subnets in one of a VPC's secondary CIDR blocks created using a aws_vpc_ipv4_cidr_block_association resource, it is recommended to reference that resource's vpc_id attribute to ensure correct dependency ordering.

resource "aws_vpc_ipv4_cidr_block_association" "secondary_cidr" {
  vpc_id     = "${aws_vpc.main.id}"
  cidr_block = ""

resource "aws_subnet" "in_secondary_cidr" {
  vpc_id     = "${aws_vpc_ipv4_cidr_block_association.secondary_cidr.vpc_id}"
  cidr_block = ""

» Argument Reference

The following arguments are supported:

  • availability_zone - (Optional) The AZ for the subnet.
  • availability_zone_id - (Optional) The AZ ID of the subnet.
  • cidr_block - (Required) The CIDR block for the subnet.
  • ipv6_cidr_block - (Optional) The IPv6 network range for the subnet, in CIDR notation. The subnet size must use a /64 prefix length.
  • map_public_ip_on_launch - (Optional) Specify true to indicate that instances launched into the subnet should be assigned a public IP address. Default is false.
  • assign_ipv6_address_on_creation - (Optional) Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address. Default is false
  • vpc_id - (Required) The VPC ID.
  • tags - (Optional) A mapping of tags to assign to the resource.

» Attributes Reference

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

» Import

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

$ terraform import aws_subnet.public_subnet subnet-9d4a7b6c