Terraform is a product by Hashicorp that uses Infrastructure as Code (IaC) to provision cloud infrastructure. AWS EC2 within public subnet of VPC via Internet Gateway can curl www.google.com
provider "aws" { region = "us-east-1" # Change to your preferred region }
# Generate a new RSA private key resource "tls_private_key" "ssh_key" { algorithm = "RSA" rsa_bits = 4096 }
# Save the private key locally (optional but useful for SSH access) resource "local_file" "private_key" { content = tls_private_key.ssh_key.private_key_pem filename = "private.pem" file_permission = "0600" # 6(-rw-), 4(-r-), 7(-rwx-) }
# Upload the public key to AWS resource "aws_key_pair" "generated_key" { key_name = "ubuntu-ssh-key" # Name for the AWS key pair public_key = tls_private_key.ssh_key.public_key_openssh }
# Create a VPC with public subnet resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16" enable_dns_support = true tags = { Name = "Ubuntu-VPC" } }
# Create an Internet Gateway resource "aws_internet_gateway" "gw" { vpc_id = aws_vpc.main.id tags = { Name = "IGW" } }
# Public subnet (for EC2 instance) resource "aws_subnet" "public" { vpc_id = aws_vpc.main.id cidr_block = "10.0.1.0/24" availability_zone = "us-east-1a" map_public_ip_on_launch = true # Assign public IP automatically tags = { Name = "Public-Subnet" } }
# Route table for public traffic, resource "aws_route_table" "public" { vpc_id = aws_vpc.main.id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.gw.id } tags = { Name = "Public-Route-Table" } }