Steps To Create AWS IGW Using AWS Terraform
Step 1: First mention the cloud provider and also the region in which you want to create an AWS Internet Gateway.
provider.tf
provider "aws" {
region = "us-east-1"
}
Step 2: Then create a VPC . Mention the CIDR range .
create_vpc.tf
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
tags = {
Name = "vpc"
}
}
Step 3 : Create a public subnet in the VPC . Mention the CIDR range and VPC ID .
subnet.tf
resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch=true
tags = {
Name = "Public-Subnet"
}
}
Step 4 : Create a variables file to store the ami-id, instance type , ssh port .
variables.tf
variable "instance_type" {
description = "This describes the instance type"
type = string
default = "t2.micro"
}
variable "ami_id" {
description = "This describes the ami image"
type = string
default = "ami-01c647eace872fc02"
}
variable "ssh_port" {
description = "SSH Port"
type = number
default = 22
}
Step 5 : Now create a security group . Mention the name of the security group and VPC ID .
security_group.tf
resource "aws_security_group" "security-group" {
name = "terraform-security-group"
vpc_id = aws_vpc.main.id
ingress {
from_port = var.ssh_port
to_port = var.ssh_port
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
Step 6 : Create an EC2 instance resource . Here attach all the variables and also mention the subnet ID and security group ID .
main.tf
resource "aws_instance" "example"{
ami = var.ami_id
instance_type = var.instance_type
subnet_id = aws_subnet.main.id
vpc_security_group_ids = [aws_security_group.security-group.id]
tags = {
Name = "EC2-Server"
}
}
Step 7 : After this use the below commands one by one to execute all these terraform files .
terraform init
terraform plan
terraform apply
Step 8 : Now go to your AWS Console and try to connect the EC2 instance . You will observe , it throws an error because there is no gateway that allows to access the EC2 instance from internet .
Step 9 : The error can be resolved if an internet gateway is created and attached to the VPC . Now create an internet gateway and a route table . Here basically in route table, routes are mentioned to route the traffic to internet gateway .
igw.tf
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "IGW"
}
}
resource "aws_route_table" "rt" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "route_table"
}
}
resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.main.id
route_table_id = aws_route_table.rt.id
}
Step 10 : Execute the terraform file using the command below . Here in this step all the previous resources state will be refreshed and 3 more resources will be created .
terraform apply
Step 11 : Now go to AWS Console and try to connect the EC2 instance , you will observe that you can connect EC2 instance successfully .
Now if you want delete the resources, use the command below .
terraform destroy
AWS IGW Using AWS Terraform
Terraform is an Infrastructure As Code tool that is used to programmatically provision infrastructure on a cloud platform. Here in this guide, i will first discuss what is AWS Internet Gateway and its important features. Then I will discuss Terraform. After this i will walk you through the different steps to create an AWS Internet Gateway using Terraform and how it helps to connect an EC2 Instance inside the VPC.