Debug School

Srikanth K N
Srikanth K N

Posted on

Terraform Code

Terraform code to create an EC2 instance that would use a Security group that would allow SSL and Web traffic

// Variable Block

variable "sreevar" {
    type = "map"
    default = {
    region = "us-east-1"
    vpc = "vpc-5234832d"
    ami = "ami-0c1bea58988a989155"
    itype = "t2.micro"
    subnet = "subnet-81896c8e"
    publicip = true
    keyname = "SreeSecKey"
    secgroupname = "SreeSG"
  }
}

provider "aws" {
  region = lookup(var.sreevar, "region")
}

  resource "aws_security_group" "SG_Web_SSL" {
  name = lookup(var.sreevar, "secgroupname")
  description = "Allow Web and SSL traffic"
  vpc_id = lookup(var.sreevar, "vpc")

  // To Allow SSH Transport
  ingress {
    from_port = 22
    protocol = "tcp"
    to_port = 22
    cidr_blocks = ["0.0.0.0/0"]
  }

  // To Allow Port 80 Transport
  ingress {
    from_port = 80
    protocol = "tcp"
    to_port = 80
    cidr_blocks = ["0.0.0.0/0"]
  }

// To Allow outbound traffic
  egress {
    from_port       = 0
    to_port         = 0
    protocol        = "-1"
    cidr_blocks     = ["0.0.0.0/0"]
  }

 tags = {
    Name = "allow_web_ssl"
  }

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_instance" "project-sree" {
  ami = lookup(var.sreevar, "ami")
  instance_type = lookup(var.sreevar, "itype")
  subnet_id = lookup(var.sreevar, "subnet")
  associate_public_ip_address = lookup(var.sreevar, "publicip")
  key_name = lookup(var.sreevar, "keyname")

  tags = {
    Name ="SREE-SERVER"
    Environment = "Dev"
    OS = "UBUNTU"
    Managed = "Identity-Managed"
  }

  vpc_security_group_ids = [aws_security_group.SG_Web_SSL.id] 

  depends_on = [ aws_security_group.SG_Web_SSL ]
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)