【问题标题】:Terraform multiple instances but by separate executionTerraform 多个实例,但通过单独执行
【发布时间】:2021-04-07 16:53:34
【问题描述】:

我正在尝试使用负载均衡器、安全组和三个实例创建 AWS 实例 ---> GROUP 1

我可以通过声明适当的资源来做到这一点。

现在我想创建多个独立于先前实例的此类实例 ---> GROUP 2

我想要这个是因为 GROUPS 的安全性,即一个组的信息不应与其他组的信息重叠。

我尝试了很多,但找不到方法。

以下是实例示例:

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
}

我想添加多个具有不同安全组和负载均衡器以及所有其他内容的实例。但我不想在 terraform 文件中添加相同的其他副本。我希望这些实例独立于这个实例,但我面临的问题是 terraform 只操纵这个实例。

【问题讨论】:

  • 您能否提供任何实际 TF 代码的示例以及您尝试实现的目标、为什么它不起作用、任何错误消息?
  • 您好,感谢您的留言,我已经编辑了问题。如果可以请帮忙。
  • 那么唯一的区别是安全组?
  • Eevn 在负载均衡器和 VPC 中,我的意思是我想拥有相同部署的多个副本,但地址和名称不同,但目前的问题是 terraform 会破坏并写入前一个实例因为它不知道必须创建新实例。
  • 如果您有很多参数需要更改,那么最好创建一个 ec2 模块。这样,您可以在您的父文件中创建两个模块,一个用于您的每一组。

标签: amazon-web-services terraform terraform-provider-aws


【解决方案1】:

基于 cmets,您可以考虑将实例代码及其依赖项(例如目标组附件)组织为 terraform (TF) modules。此外,由于您希望创建相同类型的多个实例,您可以考虑使用aws_autoscaling_group,它不仅可以让您轻松创建多个实例,还可以轻松管理它们。

随后,您可以如下定义一个模块。以下只是部分示例。我也不使用aws_autoscaling_group,而是使用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 

拥有这样的模块,在您的父文件/模块中,您将创建 GROUP 1 和 2 实例,如下所示(同样,只是部分示例):

./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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 2019-03-30
    • 2021-02-04
    • 1970-01-01
    • 2019-04-28
    • 2018-05-25
    相关资源
    最近更新 更多