Next thing we need to create is an internet gateway for our 2 public subnets that will give them internet access. Internet Gateways are required for any VPC that needs to access the internet.

Functions of an Internet Gateway

  1. Routing: It serves as a routing target for internet-routable traffic.
  2. NAT: Performs Network Address Translation for instances that have been assigned public IPv4 addresses.
  3. Stateless Filtering: It does not maintain any session information. Each request from your VPC to the internet and vice versa is treated independently.

How Does It Work?

To allow instances within your VPC to communicate with the internet, you would:

  1. Attach an Internet Gateway: First, create and attach an Internet Gateway to your VPC.
  2. Update Route Tables: Modify the VPC route tables to point internet-bound traffic to the Internet Gateway.
  3. Assign Public IPs: Ensure that your instances have a public IP or Elastic IP.

Security Considerations

Internet gateways allow traffic to flow in and out of your network, but they do not provide security features by default. To protect your resources, you should implement security measures such as security groups and network access control lists (NACLs).

Update our main.tf file and add the following code:

# interget gateway
resource "aws_internet_gateway" "dev-igw" {
  vpc_id = aws_vpc.aws-vpc.id

  tags = {
    Name = "dev-igw"
    env  = "dev"
  }
}

Apply out changes:

terraform apply
Check AWS Console.
Current architecture

Don’t forget to destroy resources if you plan to continue later

terraform destroy

Internet gateways on their own do not allow internet access, we must edit or create route tables to make it work. We will see about route tables in the next post.

Let look at NAT GATEWAYS

Next- NAT Gateway