Search code examples
amazon-web-servicesemrhadoop2amazon-swf

How do I create a Private subnet using AWS java SDK


I am able to create a subnet in my vpc. But, its a public subnet. However I would like to create a private subnet associated to my VPC. How could I acheive that. Thanks.

private static Subnet createSubnet(AmazonEC2 ec2, String vpcId, String az, String subnetACidrBlock)

 CreateSubnetRequest csr = new CreateSubnetRequest();
    csr.withAvailabilityZone(az)
        .withCidrBlock(subnetACidrBlock)
        .withVpcId(vpcId);
    Subnet subnet = ec2.createSubnet(csr).getSubnet();
    System.out.println("Subnet " + subnet.getSubnetId());
    return subnet;

}


Solution

  • You can set the private route table to the created subnet using ec2.associateRouteTable()

    The difference between a "public" and a "private" subnet is in the route table. The subnet with a route table that does not route through an Internet Gateway or Virtual Gateway is private.