List AWS Subnets with Node.js

In order to launch an Amazon EC2 instance, we need both the AMI to use and the subnet we want to launch the instance into. In this article we will use Node.js to list the subnets available to us in a particular region.

Please note, it is beyond the scope of this post to define VPCs and subnets, however you can find more information on these concepts in Amazon’s documentation here and in the additional resources below.

Let’s list!

// load AWS SDK
const AWS = require('aws-sdk');

// set the region to Oregon
AWS.config.update({region:'us-west-2'});

// create EC2 service object
const ec2 = new AWS.EC2({apiVersion: '2016-11-15'});

// empty params object, needed to pass into describeSubnets function
const params = {};

ec2.describeSubnets(params, function(err, data) {
  if (err) {
    console.log(err, err.stack); // an error occurred
  } else {
    console.log(data);           // successful response
  }  
});

Example output:

data = {
 "Subnets": [
  {
   "AvailabilityZone": "us-west-2a",
   "AvailabilityZoneId": "usw2-az1",
   "AvailableIpAddressCount": 251,
   "CidrBlock": "10.0.0.0/24",
   "DefaultForAz": false,
   "MapPublicIpOnLaunch": true,
   "State": "available",
   "SubnetId": "subnet-9d4a7b6a",
   "VpcId": "vpc-a01106c2",
   "OwnerId": "38918754239506",
   "AssignIpv6AddressOnCreation": false,
   "Ipv6CidrBlockAssociationSet": [],
   "Tags": [],
   "SubnetArn": "arn:aws:ec2:us-west-2:38918754239506:subnet/subnet-9d4a7b6a"
  },   
  {
   "AvailabilityZone": "us-west-2b",
   "AvailabilityZoneId": "usw2-az2",
   "AvailableIpAddressCount": 251,
   "CidrBlock": "10.0.1.0/24",
   "DefaultForAz": false,
   "MapPublicIpOnLaunch": true,
   "State": "available",
   "SubnetId": "subnet-9d4a7b6b",
   "VpcId": "vpc-a01106c2",
   "OwnerId": "38918754239506",
   "AssignIpv6AddressOnCreation": false,
   "Ipv6CidrBlockAssociationSet": [],
   "Tags": [],
   "SubnetArn": "arn:aws:ec2:us-west-2:38918754239506:subnet/subnet-9d4a7b6b"
  },
  {
   "AvailabilityZone": "us-west-2c",
   "AvailabilityZoneId": "usw2-az3",
   "AvailableIpAddressCount": 251,
   "CidrBlock": "10.0.2.0/24",
   "DefaultForAz": false,
   "MapPublicIpOnLaunch": true,
   "State": "available",
   "SubnetId": "subnet-9d4a7b6c",
   "VpcId": "vpc-a01106c2",
   "OwnerId": "38918754239506",
   "AssignIpv6AddressOnCreation": false,
   "Ipv6CidrBlockAssociationSet": [],
   "Tags": [],
   "SubnetArn": "arn:aws:ec2:us-west-2:38918754239506:subnet/subnet-9d4a7b6c"
  }
 ]
}

From this output we want the SubnetId, which we can obtain with a slight update to our callback function:

ec2.describeSubnets(params, function(err, data) {
  if (err) {                      // an error occurred
    console.log(err, err.stack);  
  } else {                        // successful response
    const subnetId = data.Subnets[0].SubnetId;
    console.log(subnetId);        // subnet-9d4a7b6a
  }  
});

With the SubnetId and the ImageId obtained from describeImages, we are now ready to launch an instance!

Additional Resources

Updated: