Monday, January 20, 2020

VPC

Amazon Virtual Private Cloud lets you provision a logical section of the AWS where you can launch AWS resources in a virtual network that you define. You have complete control over your VPC including selection of ip range (IPv4 CIDR block), creation of subnets, configuration of route table and network gateway. Its logically isolated from other virtual network in AWS cloud. 

    When you create VPC it automatically creates following
    • Route table
      • It will create Main Route table in the VPC. You will not be able to delete Main route table until. This gets deleted automatically when you delete VPC
      • Main route table will have a local target route with destination of the VPC IPv4 CIDR and also IPv6 if you selected IPv6 CIDR block when you created VPC
      • Any subnet which you will create and not associate explicitly with any route table will automatically be associated to Main route table.
    • Network ACLs
      • A default Network ACL will be created which you cannot delete.
      • Default Network ACL will allow all inbound and outbound traffic. You have option to changing it to deny or modify and rules in it.
    • Security group
      • Default VPC security group will be created
      • By default it will allow all outbound traffic and allow no inbound traffic and allow instances associated with this SG to talk to each other.
      • You can also edit security group rule by adding, removing or updating

    Using VPC peering you can connect one VPC with other via direct network route using private IP address. This can be done for other AWS account as well as other VPCs in the same account.

    Subnets

    A subnetwork or subnet is a logical subdivision of an IP network.[1] The practice of dividing a network into two or more networks is called subnetting.
    • When you create VPC you specify IPv4 CIDR block (and optional Amazon provided IPv6 CIDR block). You can create subnet in VPC with subset of VPC IPv4 CIDR block (and also for IPv6 if you choose to do so).
    • Based on subnet's IPv4 CIDR block, you will get IPv4 address in that subnet. Refer following to get count of available IP for specific CIDR block. One important thing to note here is that first four IP addresses and the last IP address in each subnet CIDR block are not available for you to use, and cannot be assigned to an instance.
    • By default any resource created in this subnet will not get public IP address. If you want to change this behavior, you will have to enable auto assign public IPv4 address settings.
    • Subnet will be associated with Main Route table and Default Network ACLs. This can definitely be modified.

    Route Table

    A route table contains a set of rules, called routes, that are used to determine where network traffic is directed. Each subnet in your VPC is associated with ONLY ONE route table. If you don't explicitly associate you subnet to a route table then its associated to Main route table
    • Each route in a table specifies a destination CIDR and a target. For example destination  10.0.0.0/16 with target for Local, which means traffic destined for any ip within 10.0.0.0/16 is targeted for local. Similarly to open all internet access you can choose 0.0.0.0/0 (which essentially means any ip)  with target internet gateway.
    • When you add an Internet gateway, an egress-only Internet gateway, a virtual private gateway, a NAT device, a peering connection, or a VPC endpoint in your VPC, you must update the route table for any subnet that uses these gateways or connections.
    • For public subnet (instance to be served as web server) you need to have route with destination 0.0.0.0/0 with target as internet gateway.

    Internet Gateway

    An Internet gateway serves two purposes: to provide a target in your VPC route tables for Internet-routable traffic, and to perform network address translation (NAT) for instances that have been assigned public IP (IPv4 and IPv6 traffic) addresses. One VPC can only have one Internet Gateway. 

    NAT Instance

    You can use a network address translation (NAT) instance in a public subnet in your VPC to enable instances in the private subnet to initiate outbound IPv4 traffic to the Internet or other AWS services, but prevent the instances from receiving inbound traffic initiated by someone on the Internet. 

    EC2 instance performs source and destination check which means instance must be source or destination of any traffic it sends or receives. However a NAT instance must be able to send or receive traffic when the source or destination is not itself. Therefor source and destination check must be disabled on NAT instance.

    NAT Gateway

    You can use a network address translation (NAT) gateway to enable instances in a private subnet to connect to the internet or other AWS services, but prevent the internet from initiating a connection with those instances. For IPv6 use an egress-only Internet gateway. 


    NAT instance is instance (you create single or multiple) which you have to manage whereas NAT gateway is clustered instances which amazon manages so you don't have to worry about maintaining that. NAT instance sits behind security group where as NAT gateway is outside security group. Both need to be in public subnet  which allows internet traffic and need to be added to the route table which is associated to the private subnet. This way you can connect to internet in the resources which are within private subnet. The downside of NAT instance is that all your traffic in private subnet goes through NAT instance, so that's a bottleneck as if its goes down it will impact all the resources within your private subnet. NAT instance can be used to bastion server  (meaning it can be used to RDP or SSH servers in private subnet.) where as NAT gateway cannot be. NAT Gateway automatically assign ip address when you create them and amazon manages them. You should have NAT gateway in multiple AZ. You cannot SSH or RDP into nate gateway.

    Network ACL

    A network access control list (ACL) is a  layer of security for your VPC that acts as a firewall for controlling traffic in and out of one or more subnets inside VPC. 

    • By default everything is denied when you create NACL.
    • Each subnet must be associated with NACL, if you don't explicitly associate subnet with NACL it automatically associate it with default VPC.
    • You can associate NACL with multiple subnet, but a subnet can be associated with single NACL and when you update NACL to vpc, it will remove previous associated NACL.
    • NACL can be used across multiple AZ where as subnet is in single AZ
    • ACL contains numbered list of rules that is evaluated in order, starting with lowest numbered rule.
    • Network ACLs are state less, response to allowed inbound traffic are subject to rules for outbound traffic and vice versa, meaning you need to specify both inbound and outbound rules explicitly. Security Group which acts a firewall for controlling traffic in and out of EC2 instance are statefull.
    • Security Group you allow but in NACL you can allow or deny

    Here are some of the examples of minimum Network ACL rule in order to allow specific operation from subnet.

    To Allow ping

    • Inbound - All ICMP - IPv4 Allow, All Trafic Deny
    • Outbound - All ICMP - IPv4 Allow, All Trafic Deny

    To Allow SSH

    • Inbound - SSH (22) Allow, All Trafic Deny
    • Outbound - Custom TCP Rule(1024-65535) (Ephemeral_port) Allow, All Trafic Deny

    To Allow SSH from Public subnet to private subnet

    Since you cannot directly connect to instance in private subnet, you can create Bastions instance, which can act as jump boxes which you can use to administer (like SSH or RDP) to instances in private subnet
    • Public Subnet NACL
      • Inbound - SSH (22) Allow, Custom TCP Rule(1024-65535) (Ephemeral_port) Allow, All Trafic Deny
      • Outbound - Custom TCP Rule(1024-65535) (Ephemeral_port) Allow, SSH (22) Allow, All Trafic Deny
    • Private Subnet NACL
      • Inbound  - SSH (22) Allow, All Trafic Deny
      • Outbound - Custom TCP Rule(1024-65535) (Ephemeral_port) Allow, All Trafic Deny

    Allow HTTP Access from subnet

    • Inbound - Custom TCP Rule(1024-65535) (Ephemeral_port), All Trafic Deny
    • Outbound - HTTP(80) Allow (or HTTPS(443) for ex running aws s3 ls), All Trafic Deny

    Allow HTTP Access to Subnet (instance acting as web server)

    • Inbound- HTTP(80) Allow, All Trafic Deny
    • Outbound - Custom TCP Rule(1024-65535) (Ephemeral_port), All Trafic Deny

    VPC Flow Log

    Its a feature which enables you to capture information about the IP traffic going to and from network interfaces in your VPC. Flow log data is stored using Amazon cloud watch Logs. It can be created at 3 level

    • VPC
    • Subnet
    • Network interface level

    To set up flow log
    • you have to define filter (all, accepted, rejected)
    • role which can perform logs:Create\DescribeLogGroup\Stream, PutLogEvents
    • Assign Log group

    • You cannot enable flow logs for VPC that are peered with your VPC unless the peer VPC is in your account
    • You cannot tag a flow log
    • After you have created a flow log, you cannot change its configuration, for ex you cannot associate different IAM role with the flow log
    Following traffic are not monitored
    • Traffic generated by instances when they contact Amazon DNS server. If you use your own DNS server, then all traffic to that DNS server is logged.
    • Traffic generated by windows instance for Amazon windows license activation.
    • Traffic to and fro 169.254.169.254 for instance meta data
    • DHCP traffic
    • Traffic to reserved IP address for default VPC router.

    No comments:

    Post a Comment