Terraform 将现有安全组添加到新的自动缩放 ec2 组

Terraform add existing security group to new auto scaling ec2 group

我想将 VPC 中定义的现有安全组添加到 EC2 Auto Scaling 组。 没有定义 LB。此示例现在创建单个 EC2 实例。

Terraform 文档表明,对于使用 sg_attachment

的 EC2 实例,这是可能的
resource "aws_network_interface_sg_attachment" "bastion" {
  security_group_id    = var.sg_id
  network_interface_id = aws_autoscaling_group.bastion.primary_network_interface_id
}

但我收到以下错误,可能是因为我使用的是自动缩放组:

Error: Unsupported attribute

on ......\modules\ec2_auto_scaling_group\bastion.tf line 51, in resource "aws_network_interface_sg_attachment" "bastion": 51:
network_interface_id = aws_autoscaling_group.bastion.primary_network_interface_id

This object has no argument, nested block, or exported attribute named "primary_network_interface_id".

我已看到自动缩放组附件 - https://www.terraform.io/docs/providers/aws/r/autoscaling_attachment.html

但这根本不是指安全组。

当然 - 我可以隐式指定一个具有所有相同规则的新安全组,或者只声明一个 ec2 实例。但是在控制台上创建自动缩放组时 - 您可以选择导入现有安全组。所以我想 terraform 有一个等价物。

看来我忽略了之前的设置:

resource "aws_launch_configuration" "bastion" {
  # Launch configuration can't be updated, (provisioning)
  # in order to update the resource will be destroyed and rebuilt

  name_prefix = var.bastion_name_prefix

  image_id = data.aws_ami.RHEL_77.id 
  instance_type = var.bastion_instance_type
  key_name = aws_key_pair.bastion.key_name
  associate_public_ip_address = true
  enable_monitoring = false
  security_groups = [var.vpc_main_sg_id,aws_security_group.bastion.id]

  lifecycle {
      create_before_destroy = true
  }
}

向 aws_launch_configuration 添加安全组,解决了该问题。