The Virtual Private Cloud (VPC) is the container network. We need to create subnets within that network to divide up the space. Some of these subnets will be public and others will be private.
Be aware that these commands will use the VpcId
provided when the VPC was created. The VpcId
listed with these examples will need to be adjusted based on the output from your commands.
Note that I do not include a profile in these commands. My access keys and region are specified in the default profile. If you are using different configurations from what you have saved in the default profile then you will need to specify the named profile on the command line using the --profile profilename
option.
These commands will each create a new subnet within the VPC I just created. Note that the VPC has a mask of /16
while each of these networks has a mask of /24
. With this netmask, we can create up to 255 subnets.
The availability zones provided must be within the same region as the VPC. In this case, our VPC is in us-east-1
and these AZs are in that region.
AWS CLI
Set $AWS_VPCID
to the value of the VpcId
returned when you created the VPC.
aws ec2 create-subnet --vpc-id ${AWS_VPCID} --cidr-block 10.0.1.0/24 --availability-zone us-east-1a
{
"Subnet": {
"AvailabilityZoneId": "use1-az4",
"OwnerId": "941377140019",
"AssignIpv6AddressOnCreation": false,
"Ipv6CidrBlockAssociationSet": [],
"SubnetArn": "arn:aws:ec2:us-east-1:941377140019:subnet/subnet-06e740b619595a549",
"EnableDns64": false,
"Ipv6Native": false,
"PrivateDnsNameOptionsOnLaunch": {
"HostnameType": "ip-name",
"EnableResourceNameDnsARecord": false,
"EnableResourceNameDnsAAAARecord": false
},
"SubnetId": "subnet-06e740b619595a549",
"State": "available",
"VpcId": "vpc-0fbacb611b69b8966",
"CidrBlock": "10.0.1.0/24",
"AvailableIpAddressCount": 251,
"AvailabilityZone": "us-east-1a",
"DefaultForAz": false,
"MapPublicIpOnLaunch": false
}
}
Repeat this command for as many subnets as you need.
Terraform
The vpc_id
is set using a variable that references a VPC that was created in a different block.
resource "aws_subnet" "basic-aws-subnet-pub1" {
count = "1"
vpc_id = "${aws_vpc.basic-aws-vpc.id}"
cidr_block = "10.0.10.0/24"
map_public_ip_on_launch = "true"
availability_zone = "us-east-1a"
tags = {
Name = "Basic Public Subnet 1"
}
}