How to Set Up an AWS Internet Gateway (IGW) with Terraform.

How to Set Up an AWS Internet Gateway (IGW) with Terraform.

Introduction.

In cloud computing, an Internet Gateway (IGW) is a critical component for enabling communication between instances in your Virtual Private Cloud (VPC) and the internet. When it comes to managing your infrastructure as code (IaC), Terraform is a powerful tool that allows you to automate the setup of AWS resources, including an Internet Gateway, seamlessly.

Setting up an Internet Gateway using Terraform simplifies your infrastructure management by making the process repeatable, reliable, and version-controlled. With Terraform, you can define your cloud resources in configuration files, making it easy to track changes and provision infrastructure with minimal effort.

In this guide, we will walk you through the steps to provision an AWS Internet Gateway using Terraform. You’ll learn how to define and deploy the IGW, attach it to your VPC, and configure the necessary routes for internet access. Whether you are setting up a new VPC or modifying an existing one, understanding how to automate the creation of an IGW with Terraform is essential for managing scalable and secure cloud environments.

By the end of this tutorial, you will have a solid understanding of how to use Terraform to create and manage AWS networking resources. With this knowledge, you can confidently implement and scale internet connectivity in your AWS VPC. Let’s get started with setting up your AWS Internet Gateway using Terraform!

STEP 1: Create provider.tf file and enter the following script and ave it.

provider "aws" {
region = "us-east-1"
}

STEP 2: Next, Create vpc.tf file.

resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"

tags = {
Name = "vpc"
}
}

STEP 3: Create subnet.tf file.

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 variables.tf file.

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.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 main.tf file.

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

Conclusion.

Setting up an AWS Internet Gateway (IGW) using Terraform is a straightforward and efficient way to manage your cloud networking resources. By automating the process, Terraform ensures your infrastructure remains consistent, reproducible, and easy to manage. We’ve walked through the essential steps of defining, creating, and attaching an Internet Gateway to your AWS VPC, providing internet access to your instances and services.

By using Terraform, you not only simplify the deployment process but also enhance the scalability and maintainability of your infrastructure. The ability to track changes, version control your configurations, and automate resource management brings significant efficiency to your cloud operations.

As your AWS environment grows, knowing how to manage core networking components like the IGW with Terraform will help you create a more robust, secure, and optimized infrastructure. Now that you have a solid understanding of how to set up an IGW, you can confidently implement and scale internet access across your VPCs.

With Terraform, you can continuously improve your cloud operations, reduce human errors, and ensure that your infrastructure remains agile and adaptable to changing needs. Happy cloud building!

shamitha
shamitha
Leave Comment
Share This Blog
Recent Posts
Get The Latest Updates

Subscribe To Our Newsletter

No spam, notifications only about our New Course updates.