provider "aws" { region = "ap-south-1" } # EC2 instance resource "aws_instance" "ec2_t2-micro" { ami = "ami-0861f4e788f5069dd" # Amazon Linux instance_type = "t2.micro" # Instance type key_name = "kshitij-personal-ed25519" # SSH keys subnet_id = "subnet-0f2515644bbe8e603" # VPC -> Subnet ID associate_public_ip_address = true # Public IP for SSH vpc_security_group_ids = [aws_security_group.ec2_ssh_security.id] # VPC security group tags = { Name = "Test-EC2" # Instance name } } # Security group (firewall) for allowing incoming SSH connections resource "aws_security_group" "ec2_ssh_security" { name = "allow_ssh" # Firewall rule name ingress { # Ingress rules (incoming) from_port = 22 # Starting port range to_port = 22 # End port range protocol = "tcp" # Allowed protocol cidr_blocks = ["${var.ssh_ingress}"] # Allowed IP ranges } egress { from_port = 0 # Starting port range; 0 implies all ports to_port = 0 # End port range; 0 implies all ports protocol = -1 # Allowed protocol; -1 implies all protocols (in HCL) cidr_blocks = ["0.0.0.0/0"] # Allowed IP ranges } # For each protocol, new block; eg. egress { from... to... protocol = "icmp" cidr...} }