通过连接已经存在的安全组,使用TerraForm创建EC2实例
如何在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,
security_groups
参数仅期望安全组名称或ID列表。因此,最好创建一个变量块类型为list(String)
,如下所示,您可以使用
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 aslist(string)
as belowOptionally, you can use the
data
source to get the existing security group IDs by using thetags
argument.Reference link: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/security_groups#example-usage
如评论中所述,它正在期待列表参数,因此代码将是;
As mentioned in the comments it is expecting a list parameter, so the code would be;