通过连接已经存在的安全组,使用TerraForm创建EC2实例

发布于 2025-02-10 02:09:58 字数 763 浏览 1 评论 0原文

如何在TerraForm中创建而不是创建新的安全组资源时将实例添加到现有安全组中?

var.tf中的代码

variable "sg" {
 type =string
 default = "sg-111436g6535hc63xc"
}

resource.tf 中的代码,

resource "aws_instance" "web" {
 ami = var.ami
 key_name = var.key
 instance_type = var.itype
 security_groups =  var.sg 
 tags = {
  Name = "HelloWorld"
  } 
}

但我得到了 - >

│ Error: Incorrect attribute value type
│ 
│   on resource.tf line 5, in resource "aws_instance" "web":
│    5:   security_groups = var.sg
│     ├────────────────
│     │ var.sg is a string, known only after apply
│ 
│ Inappropriate value for attribute "security_groups": set of string required.

如何解决此错误?

How to add an instance to an existing security group while creating in terraform instead of creating a new security group resource?

code in var.tf

variable "sg" {
 type =string
 default = "sg-111436g6535hc63xc"
}

code in resource.tf

resource "aws_instance" "web" {
 ami = var.ami
 key_name = var.key
 instance_type = var.itype
 security_groups =  var.sg 
 tags = {
  Name = "HelloWorld"
  } 
}

But I'm getting ->

│ Error: Incorrect attribute value type
│ 
│   on resource.tf line 5, in resource "aws_instance" "web":
│    5:   security_groups = var.sg
│     ├────────────────
│     │ var.sg is a string, known only after apply
│ 
│ Inappropriate value for attribute "security_groups": set of string required.

How to solve this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

超可爱的懒熊 2025-02-17 02:09:58

通常,security_groups参数仅期望安全组名称或ID列表。因此,最好创建一个变量块类型为list(String),如下所示

variable "sg" {
  description = "List of Security Group IDs"
  type        = list(string)
  default     = [ "sg-111436g6535hc63xc" ]
}

resource "aws_instance" "web" {
  ami              = var.ami
  key_name         = var.key
  instance_type    = var.itype
  security_groups  = var.sg 
  
  tags = {
    Name = "HelloWorld"
  } 
}

,您可以使用data源来通过使用使用data来获取现有的安全组ID 标签参数。

参考链接: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/security_groups#example-usage

Generally, the security_groups argument only expects a list of security group names or IDs. So, it would be better to create a variable block type as list(string) as below

variable "sg" {
  description = "List of Security Group IDs"
  type        = list(string)
  default     = [ "sg-111436g6535hc63xc" ]
}

resource "aws_instance" "web" {
  ami              = var.ami
  key_name         = var.key
  instance_type    = var.itype
  security_groups  = var.sg 
  
  tags = {
    Name = "HelloWorld"
  } 
}

Optionally, you can use the data source to get the existing security group IDs by using the tags argument.

Reference link: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/security_groups#example-usage

衣神在巴黎 2025-02-17 02:09:58

如评论中所述,它正在期待列表参数,因此代码将是;

resource "aws_instance" "web" {
 ami = var.ami
 key_name = var.key
 instance_type = var.itype
 security_groups =  [var.sg] 
 tags = {
  Name = "HelloWorld"
  } 
}

As mentioned in the comments it is expecting a list parameter, so the code would be;

resource "aws_instance" "web" {
 ami = var.ami
 key_name = var.key
 instance_type = var.itype
 security_groups =  [var.sg] 
 tags = {
  Name = "HelloWorld"
  } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文