0
votes

below is the code in ec2, security group module.

data "aws_ami" "ubuntu" {
    owners           = ["373042721571"]
    most_recent      = true


  filter {
    name   = "name"
    values = ["image-test"]
  }
}

# data "template_file" "user_data" {
# template = file("userdata.sh")
# }

resource "aws_instance" "web-1" {
    count     = 1
    ami       = "${data.aws_ami.ubuntu.id}"
    instance_type = "t2.micro"
    key_name        = var.key_name
    subnet_id       = var.subnetid
    vpc_security_group_ids = var.securitygroupid
    associate_public_ip_address = true
    # user_data = data.template_file.user_data.rendered
    tags = {
        Name = "Server-1"
        Owner = "Terraform"
    }
}
resource "aws_security_group" "allow_all" {
  name        = var.sg_name
  description = "allowing all traffic to the servers in piblic "
  vpc_id      = var.vpcid

  ingress {
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

  egress {
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

  tags = {
    Name = "allow_all_trafic"
  }
}
module "vpc" {
      source = "./module/vpc"
      vpc_cidr = var.vpc_cidr
      vpc_name = var.vpc_name
      IGW_name = var.IGW_name
      routing_table_public_name = var.routing_table_public_name
}

module "securitygroup" {
      source = "./module/securitygroup"
      sg_name = var.sg_name
      vpcid = module.vpc.vpc_id
}

module "ec2" {
      source = "./module/ec2"
      key_name = var.key_name
      securitygroupid = module.securitygroup.aws_security_group.allow_all.id
      subnetid = module.vpcmodule.public[0]

}

this is the terrafrom modules i created locally when i refer to vpc-id and subnet ids i was getting as │ Error: Unsupported attribute │ │ on output.tf line 3, in output "vpc_id": │ 3: value = module.vpc.vpc_id │ ├──────────────── │ │ module.vpc is a object, known only after apply │ │ This object does not have an attribute named "vpc_id".