AWS Amazon EC2 Subnet

This page shows how to write Terraform and CloudFormation for Amazon EC2 Subnet and write them securely.

aws_default_subnet (Terraform)

The Subnet in Amazon EC2 can be configured in Terraform with the resource name aws_default_subnet. The following sections describe 4 examples of how to use the resource and its parameters.

Example Usage from GitHub

createDefaultSubnet.tf#L1
resource "aws_default_subnet" "us-east-1a" {
  availability_zone = "us-east-1a"
}

resource "aws_default_subnet" "us-east-1b" {
  availability_zone = "us-east-1b"
vpc.tf#L2
resource "aws_default_subnet" "default_aza" {
  availability_zone = "eu-west-2a"
  tags              = local.tags
}

resource "aws_default_subnet" "default_azb" {
main.tf#L23
resource "aws_default_subnet" "default_subnet_a" {
  availability_zone = "eu-west-1a"
}

resource "aws_default_subnet" "default_subnet_b" {
  availability_zone = "eu-west-1b"
vpc.tf#L5
resource "aws_default_subnet" "default_subnet_a" {
  availability_zone = var.availability_zones[0]
}

resource "aws_default_subnet" "default_subnet_b" {
  availability_zone = var.availability_zones[1]

Parameters

Explanation in Terraform Registry

Provides a resource to manage a default AWS VPC subnet in the current region. The aws_default_subnet behaves differently from normal resources, in that Terraform does not create this resource but instead "adopts" it into management. The aws_default_subnet resource allows you to manage a region's default VPC subnet but Terraform cannot destroy it. Removing this resource from your configuration will remove it from your statefile and Terraform management.

AWS::EC2::Subnet (CloudFormation)

The Subnet in EC2 can be configured in CloudFormation with the resource name AWS::EC2::Subnet. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

stemflow-vpc.yml#L95
    Type: AWS::EC2::Subnet
  AppSubnetANACLAssociation:
    Properties:
      NetworkAclId:
        Ref: AppNACL
      SubnetId:
tb-ps-vpc.yml#L69
    Type: "AWS::EC2::Subnet"
    Properties:
      CidrBlock: 172.32.100.0/24
      AvailabilityZone: "us-west-2a"
      VpcId: !Ref TBPSVpc
      Tags:
tb-ps-vpc-all-public.yml#L69
    Type: "AWS::EC2::Subnet"
    Properties:
      CidrBlock: 172.32.100.0/24
      AvailabilityZone: "us-west-2a"
      VpcId: !Ref TBPSVpc
      Tags:
vpc.yml#L57
    Type: AWS::EC2::Subnet
  HQPrivateSubnet100:
    Properties:
      AvailabilityZone:
        Fn::Select:
        - 0
SacVpcTemplateV1.5.1.yml#L50
        Type: 'AWS::EC2::Subnet'
        Properties:
            VpcId:
                Ref: VPC
            CidrBlock:
                'Fn::Join':
empty-vpc-us-east-1.json#L34
            "Type" : "AWS::EC2::Subnet",
            "Properties" : {
                "VpcId" : {"Ref" : "vpc1"},
                "Tags" : [ {"Key" : "Name", "Value" : "vpc1_sn_A1"} ],
                "AvailabilityZone" : {
                    "Fn::Select" : [
empty-vpc-us-east-1.json#L34
            "Type" : "AWS::EC2::Subnet",
            "Properties" : {
                "VpcId" : {"Ref" : "vpc1"},
                "Tags" : [ {"Key" : "Name", "Value" : "vpc1_sn_A1"} ],
                "AvailabilityZone" : {
                    "Fn::Select" : [
empty-vpc-us-east-1.json#L34
            "Type" : "AWS::EC2::Subnet",
            "Properties" : {
                "VpcId" : {"Ref" : "vpc1"},
                "Tags" : [ {"Key" : "Name", "Value" : "vpc1_sn_A1"} ],
                "AvailabilityZone" : {
                    "Fn::Select" : [
empty-vpc-us-east-1.json#L34
            "Type" : "AWS::EC2::Subnet",
            "Properties" : {
                "VpcId" : {"Ref" : "vpc1"},
                "Tags" : [ {"Key" : "Name", "Value" : "vpc1_sn_A1"} ],
                "AvailabilityZone" : {
                    "Fn::Select" : [
MinimalVPCOutput.cfn.json#L254
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "AvailabilityZone": {
          "Fn::Select": [
            "0",
            {

Parameters

Explanation in CloudFormation Registry

Specifies a subnet for a VPC.

When you create each subnet, you provide the VPC ID and IPv4 CIDR block for the subnet. After you create a subnet, you can't change its CIDR block. The size of the subnet's IPv4 CIDR block can be the same as a VPC's IPv4 CIDR block, or a subset of a VPC's IPv4 CIDR block. If you create more than one subnet in a VPC, the subnets' CIDR blocks must not overlap. The smallest IPv4 subnet (and VPC) you can create uses a /28 netmask (16 IPv4 addresses), and the largest uses a /16 netmask (65,536 IPv4 addresses).

If you've associated an IPv6 CIDR block with your VPC, you can create a subnet with an IPv6 CIDR block that uses a /64 prefix length.

Frequently asked questions

What is AWS Amazon EC2 Subnet?

AWS Amazon EC2 Subnet is a resource for Amazon EC2 of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.

Where can I find the example code for the AWS Amazon EC2 Subnet?

For Terraform, the hsinha2019/CICDPipeline, ministryofjustice/modernisation-platform and beuleal/flask-app source code examples are useful. See the Terraform Example section for further details.

For CloudFormation, the shalupov/idea-cloudformation, thoughtbend/ps-java-aws and thoughtbend/ps-java-aws source code examples are useful. See the CloudFormation Example section for further details.