From 406a25076b85d0bf2a810c4e0977e14c1651cbe4 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Thu, 4 Sep 2025 21:50:24 +0530 Subject: [PATCH] Added subnet and other networking stuff to ec2 instance creation. Also created a firewall rule to allow incoming SSH connection and all outgoing connections. --- Terraform/main.tf | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/Terraform/main.tf b/Terraform/main.tf index ec02c74..6dea987 100644 --- a/Terraform/main.tf +++ b/Terraform/main.tf @@ -2,8 +2,34 @@ provider "aws" { region = "ap-south-1" } -resource "aws_instance" "example" { - ami = "ami-0861f4e788f5069dd" - instance_type = "t2.micro" - key_name = "kshitij-personal-ed25519" +# EC2 instance +resource "aws_instance" "ec2_t2-micro" { + ami = "ami-0861f4e788f5069dd" # Amazon Linux + instance_type = "t2.micro" + key_name = "kshitij-personal-ed25519" + subnet_id = "subnet-0f2515644bbe8e603" + associate_public_ip_address = true + vpc_security_group_ids = [aws_security_group.ec2_ssh_security.id] + + tags = { + Name = "Test-EC2" + } +} + +# Security group (firewall) for allowing incoming SSH connections +resource "aws_security_group" "ec2_ssh_security" { + name = "allow_ssh" + + ingress { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["${var.ssh_ingress}"] + } + egress { + from_port = 0 + to_port = 0 + protocol = -1 + cidr_blocks = ["0.0.0.0/0"] + } }