DEV Community

Cover image for AWS: Subnet & Internet Gateway
Oladipupo Abeeb Olanrewaju
Oladipupo Abeeb Olanrewaju

Posted on

AWS: Subnet & Internet Gateway

AWS SUBNET

This is a range of IP addresses in your VPC (Virtual Private Cloud) that you can use to isolate resources within your network. A subnet can be associated with a specific availability zone, which is an isolated data center within a region, and can contain resources such as EC2 instances, RDS databases, and more.

AWS INTERNET GATEWAY

This s a horizontally scalable, redundant, and highly available VPC component that allows communication between instances in your VPC and the internet. It provides a target in your VPC route tables for internet-routable traffic, and performs network address translation (NAT) for instances that have been assigned public IP addresses.

When creating a subnet in AWS, associate it with an internet gateway, which allows instances in that subnet to access the internet otherwise if it is not associated with an internet gateway, there won't be an internet access, unless VPN or Direct Connect connection is being set up.

This is how Aws Subnet & internet gateway are connected using Terraform on Vscode. Follow this link to learn how to set up AWS VPC

For AWS SUBNET

AWS SUBNET

}
//AWS SUBNET 
resource "aws_subnet" "main_publlc_subnet" {
  vpc_id                  = aws_vpc.main_vpc.id
  map_public_ip_on_launch = true
  cidr_block              = "10.0.0.0/24"
  availability_zone       = "us-east-1a"

  tags = {
    Name = "main_subnet"
  }
}
Enter fullscreen mode Exit fullscreen mode

In the code above, we defined the resource aws_subnet and set the name to "main_publlc_subnet" with the Vpc_id set to "aws_vpc.main_vpc.id", which is where we access our VPC. We also created a Subnet resource with the cidr_block set to 10.0.0.0/24, Availability zone set to "us-east-1a" where your resource is allocated and the Name tag set to main_subnet.

Save the file.

Open the terminal in VSCode and run the command terraform apply to create the Subnet. Terraform will show you a preview of the changes that will be made, and if you're happy with them, type yes to confirm and apply the changes or run the command terraform apply -auto-approve to confirm directly and apply changes.

Terraform apply

AWS INTERNET GATEWAY (IGW)

Internet gateway

//AWS INTERNET GATEWAY
resource "aws_internet_gateway" "main_internet_gateway" {
  vpc_id = aws_vpc.main_vpc.id

  tags = {
    Name = "main_igw"
  }
}

Enter fullscreen mode Exit fullscreen mode

Save the file.

Open the terminal in VSCode and run the command terraform apply to create the Internet Gateway. Terraform will show you a preview of the changes that will be made, and if you're happy with them, type yes to confirm and apply the changes or run the command terraform apply -auto-approve to confirm directly and apply changes.

Successfully created

Once the command finishes executing, you will have a Internet gateway created on your AWS account or check your AWS CLI for internet gateway on the resources.
We hope that you found this blog helpful.

Top comments (0)