Here are the steps:
- Create a vpc (which also creates a new route table behind the scenes)
- Create 2 subnets inside this vpc. These subnets also needs to be in the same availability zone as the web-server instance, which by default both subnets ends up inheriting the vpc's route table. We'll call them public-subnet and private-subnet.
- Create an "Internet Gateway" and attach it to the vpc's routing table. This ends up making both public-subnet and private-subnet, public. Later on we will set the subnet, private-subnet, to a private subnet.
- attach the Internet Gateway to the route table, configure it to accept traffic from an IP address
- Create an EC2 instance inside the vpc (via dropdown list), and attach the subnet, private-subnet (via dropdown list). Also assign a public ip address
- Log into this EC2 instance using the public ip address, it is still accessible via the public address since private-subnet is still actually private
- Install httpd, disable firewalld, start httpd service inside the instance
- check that you can access the instance homepage via the ip address.
- In AWS web console's ec2 section, create an ELB, attach this ELB to the public-subnet, attach your ec2 to this ELB instance.
- Check you can access the instance's homepage, this time via the ELB's url, the ip address should also work too.
- Now go back into the VPC section and create a new route table, call it "private-route-table", don't attach an Internet Gateway to this.
- Switch the private-subnet's route table to this one.
- The homepage should no longer be accessible via the ip address, and now only works via the elb's url. Also you can't no longer ssh into the instance. You can use the elb's url to do this, since the elb is designed to distribute traffic (also why you can't open ssh port when creating elb, only http and https). To now ssh into your instance you will need to do it in of the following ways:
- ssh to another instance that is publicly accessible, and shares a subnet with the target instance, then ssh into that.
- use aws direct connect
- set up a vpn
vpc and subnets
To create a vpc, you need to provide:
A vpc is essentially a range of ip addresses.
You can break this range, into smaller ranges, by creating subnets.
Let's assume in this scenario, that you only plan to set this up for a single private EC2 instance:
- Create a vpc, with the following info:
- CIDR block range=10.0.0.16
- Tenancy = default
- Create a subnet in the "London" AZ. Note this AZ will be where our private EC2 instance will reside. A subnet is attached to an AZ. Here are the details you need to provide:
- subnet name="public-subnet"
- vpc name="vpc1"
- CIDR Block=10.0.1.0/24
- Create a private subnet with the London AZ, with the following details:
- subnet name = "private-subnet"
- vpc name="vpc1"
- CIDR block=10.0.2.0/24
A Public subnet must exist in the vpc, on the same AZ as the private subnet that hosts the private ec2 instance. That's because a public ELB behind the scenes builds EC2 instance on a public subnet, and then reroutes traffic to private ec2 instances. The reason that this public subnet must be in the same AZ as the private subnet, is that it improves performance.
Note: when creating an ELB, you will be prompted to select at least 2 subnets for High Availability purposes. That's why it's best to create another public subnet in a different AZ to achieve this. Although it's unlikely this second pubsubnet get's used.