AWS: Subnet & Internet Gateway
Tags: webdev, aws, news, programming
Published: 2023-02-22 12:20:15

## AWS SUBNET
This is a range of IP addresses in your VPC [(Virtual Private Cloud)](https://dev.to/daniees45/aws-create-vpc-using-terraform-cli-36an) 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](https://dev.to/daniees45/aws-create-vpc-using-terraform-cli-36an)
## For AWS SUBNET


![AWS SUBNET](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfqd6ld33ypb4rih95gh.png)

```
}
//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"
  }
}
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvmytc31keqgbk907abi.png)

##AWS INTERNET GATEWAY (IGW)


![Internet gateway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vt86imwnsux3dq67w699.png)

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

  tags = {
    Name = "main_igw"
  }
}

```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q32l1rwyhr5m94testp1.png)

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.