AWS Amazon EC2 Subnet

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

aws_subnet (Terraform)

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

Example Usage from GitHub

sn.tf#L1
resource "aws_subnet" "subnet-dev-nat-eu-central-1a" {
    //-09101cb3af5e1772a
    vpc_id                  = aws_vpc.vpc-ha-dev.id
    cidr_block              = "172.31.112.0/24"
    availability_zone       = "eu-central-1a"
    map_public_ip_on_launch = false

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Provides an VPC subnet resource.

NOTE: Due to AWS Lambda improved VPC networking changes that began deploying in September 2019, subnets associated with Lambda Functions can take up to 45 minutes to successfully delete. Terraform AWS Provider version 2.31.0 and later automatically handles this increased timeout, however prior versions require setting the customizable deletion timeout to 45 minutes (delete = "45m"). AWS and HashiCorp are working together to reduce the amount of time required for resource deletion and updates can be tracked in this GitHub issue.

Tips: Best Practices for The Other AWS Amazon EC2 Resources

In addition to the aws_default_vpc, AWS Amazon EC2 has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

aws_default_vpc

Ensure to avoid using default VPC

It is better to define the own VPC and use it.

risk-label

aws_network_acl_rule

Ensure your network ACL rule blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

risk-label

aws_ebs_volume

Ensure to use a customer-managed key for EBS volume encryption

It is better to use a customer-managed key for EBS volume encryption. It can be gain more control over the encryption by using customer-managed keys (CMK).

risk-label

aws_instance

Ensure to avoid storing AWS access keys in user data

It is better to avoid storing AWS access keys in user data. `aws_iam_instance_profile` could be used instead.

risk-label

aws_security_group

Ensure your security group blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

Review your AWS Amazon EC2 settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

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:
stemflow-vpc.yml#L95
    Type: AWS::EC2::Subnet
  AppSubnetANACLAssociation:
    Properties:
      NetworkAclId:
        Ref: AppNACL
      SubnetId:
amazon-eks-vpc-sample.yml#L92
    Type: AWS::EC2::Subnet
    Metadata:
      Comment: Subnet 01
    Properties:
      AvailabilityZone:
        Fn::Select:
network.yml#L133
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      CidrBlock: !Ref PublicSubnetACidrBlock
      AvailabilityZone: !Select ["0", !GetAZs { "Ref": "AWS::Region" }]
      Tags:
template.yml#L108
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 0, !Ref AvailabilityZones ]
      CidrBlock: !Select [ 0, !Ref SubnetCIDR ]
      MapPublicIpOnLaunch: true
Private-Prod-VPC.json#L44
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "CidrBlock" : { "Fn::Select" : [ "2", {"Fn::Cidr" : [{"Ref" : "PrivateSubnetCIDR2"},"4", "8"] }] },
        "AvailabilityZone" : { "Fn::Select" : [ "2", { "Fn::GetAZs" : { "Ref" : "AWS::Region" } } ] },
        "VpcId": {
          "Ref": "VPC"
Private-Non-Prod-VPC.json#L44
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "CidrBlock" : { "Fn::Select" : [ "2", {"Fn::Cidr" : [{"Ref" : "PrivateSubnetCIDR2"},"4", "8"] }] },
        "AvailabilityZone" : { "Fn::Select" : [ "2", { "Fn::GetAZs" : { "Ref" : "AWS::Region" } } ] },
        "VpcId": {
          "Ref": "VPC"
template.json#L29
      "Type": "AWS::EC2::Subnet"
    },
    "PrivateSubnetRTA23": {
      "Properties": {
        "SubnetId": {
          "Ref": "Subnet23"
genericvpc_nested.json#L24
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "CidrBlock": "10.0.1.0/24",
        "AvailabilityZone": {
          "Fn::Select": [
            "0",
create_subnets.json#L122
    "Type": "AWS::EC2::Subnet",
    "DeletionPolicy" : "Retain",
    "Properties": {
      "CidrBlock": {"Ref": "WebSubnet1Cidr"},
      "AvailabilityZone": {"Ref" : "subnet1AZ"},
      "VpcId": {"Ref": "VPC"},

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 mdigbazova/terraforming-4ha-live-all-resources source code example is useful. See the Terraform Example section for further details.

For CloudFormation, the JetBrains/intellij-plugins, shalupov/idea-cloudformation and iqbal-h/templates source code examples are useful. See the CloudFormation Example section for further details.