1
votes

I am trying to create AWS instances with load balancer, security group and three instances ---> GROUP 1

I can do this by declaring appropriate resources.

Now I want to create multiples of such instances which are independent of previous instances ---> GROUP 2

I want this because of security of the GROUPS that one group's information should not overlap with other's.

I tried to look up a lot, but couldn't find an approach.

Below is an example of instance:

resource "aws_instance" "node" {
  ami                    = data.aws_ami.ubuntu.id
  subnet_id              = aws_subnet.development-private-1a.id
  key_name               = aws_key_pair.nodes.key_name
  instance_type          = var.instance_type
  vpc_security_group_ids = [aws_security_group.dev-ec2-sg.id]
  
  tags          = {
    Name        = "${var.app_name}"
    #Environment = "production"
  }
  
  root_block_device {
        volume_type     = "gp2"
        volume_size     = 8
        delete_on_termination   = true
  }

  user_data = file("install_apache.sh")
}

resource "aws_lb_target_group_attachment" "node" {
  target_group_arn = aws_lb_target_group.dev.arn
  target_id        = aws_instance.node.id
  port             = 80
}

I want to add multiple of these instances with different security groups and load balancers and all other stuff. but I dont want to add additional copies of the same in the terraform file. I want that those instances are independent of this one but then the problem I am facing is that terraform manipulates this instance only.

1
Can you provide any example of actual TF code and what you try to achieve, why it does not work, any error messages?Marcin
Hi thanks for message, I have edited the question. If you could please help.Mayuresh Anand
So the only difference is security group?Marcin
Eevn in the load balancer and VPC, what I mean is I would like to have sevaral copies of same deployments but with different address and names, but currently problem is that terraform destroys and writes over the previous instance as it doesnt know that new instances have to be created.Mayuresh Anand
If you have a lot of arguments changing, then probably would be best to create an ec2 module. This way in your parent file you can create two modules, one for each group of yours.Marcin

1 Answers

1
votes

Based on the comments, you could consider organization of your instance code and its dependents (e.g. target group attachment) as terraform (TF) modules. Also since you are wish to create multiple instance of the same type, you could consider using aws_autoscaling_group which would allow you to not only easily create multiple instance but also easily to manage them.

Subsequently, you could define a module as followed. Below is only partial example. I also do not use aws_autoscaling_group, but create multiple instance using count:

./module/ec2/main.tf


variable "subnet_id" {}

variable "app_name" {}

variable "key_pair" {}

variable "security_group_id" {}

variable "target_group_arn" {}

variable "instance_count" {
   default = 1
}

data "aws_ami" "ubuntu" {
 # ...
}

resource "aws_instance" "node" {
 
  count = var.instance_count
  
  ami                    = data.aws_ami.ubuntu.id
  subnet_id              = var.subnet_id
  key_name               = var.key_pair
  instance_type          = var.instance_type
  vpc_security_group_ids = [var.security_group_id]
  
  tags          = {
    Name        = "${var.app_name}"
    #Environment = "production"
  }
  
  root_block_device {
        volume_type     = "gp2"
        volume_size     = 8
        delete_on_termination   = true
  }

  user_data = file("install_apache.sh")
}

resource "aws_lb_target_group_attachment" "node" {

  count = var.instance_count

  target_group_arn = var.target_group_arn
  target_id        = aws_instance.node[count.index].id
  port             = 80
}

# some outputs skipped 

Having such module, in your parent file/module you would create GROUP 1 and 2 instance as follows (again, just partial example):

./main.tf


# resoruces such as LB, SGs, subnets, etc.


module "group1" {
  
  source = "./module/ec2/"

  instance_count = 3

  security_group_id = <security-group-id1>

  target_group_arn = aws_lb_target_group.dev.arn

  # other parameters
}

module "group2" {
  
  source = "./module/ec2/"

  instance_count = 3

  security_group_id = <security-group-id2>

  target_group_arn = aws_lb_target_group.dev.arn

  # other parameters
}