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
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
Parameters
-
arn
optional computed - string -
assign_ipv6_address_on_creation
optional - bool -
availability_zone
optional computed - string -
availability_zone_id
optional computed - string -
cidr_block
required - string -
customer_owned_ipv4_pool
optional - string -
id
optional computed - string -
ipv6_cidr_block
optional - string -
ipv6_cidr_block_association_id
optional computed - string -
map_customer_owned_ip_on_launch
optional - bool -
map_public_ip_on_launch
optional - bool -
outpost_arn
optional - string -
owner_id
optional computed - string -
tags
optional - map from string to string -
tags_all
optional computed - map from string to string -
vpc_id
required - string -
timeouts
single block
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.
aws_default_vpc
Ensure to avoid using default VPC
It is better to define the own VPC and use it.
aws_network_acl_rule
Ensure your network ACL rule blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
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).
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.
aws_security_group
Ensure your security group blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
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
Type: AWS::EC2::Subnet
AppSubnetANACLAssociation:
Properties:
NetworkAclId:
Ref: AppNACL
SubnetId:
Type: AWS::EC2::Subnet
AppSubnetANACLAssociation:
Properties:
NetworkAclId:
Ref: AppNACL
SubnetId:
Type: AWS::EC2::Subnet
Metadata:
Comment: Subnet 01
Properties:
AvailabilityZone:
Fn::Select:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: !Ref PublicSubnetACidrBlock
AvailabilityZone: !Select ["0", !GetAZs { "Ref": "AWS::Region" }]
Tags:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [ 0, !Ref AvailabilityZones ]
CidrBlock: !Select [ 0, !Ref SubnetCIDR ]
MapPublicIpOnLaunch: true
"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"
"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"
"Type": "AWS::EC2::Subnet"
},
"PrivateSubnetRTA23": {
"Properties": {
"SubnetId": {
"Ref": "Subnet23"
"Type": "AWS::EC2::Subnet",
"Properties": {
"CidrBlock": "10.0.1.0/24",
"AvailabilityZone": {
"Fn::Select": [
"0",
"Type": "AWS::EC2::Subnet",
"DeletionPolicy" : "Retain",
"Properties": {
"CidrBlock": {"Ref": "WebSubnet1Cidr"},
"AvailabilityZone": {"Ref" : "subnet1AZ"},
"VpcId": {"Ref": "VPC"},
Parameters
-
AssignIpv6AddressOnCreation
optional - Boolean -
AvailabilityZone
optional - String -
CidrBlock
required - String -
Ipv6CidrBlock
optional - String -
MapPublicIpOnLaunch
optional - Boolean -
OutpostArn
optional - String -
Tags
optional - List of Tag -
VpcId
required - String
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 shalupov/idea-cloudformation, JetBrains/intellij-plugins and iqbal-h/templates source code examples are useful. See the CloudFormation Example section for further details.