Terraform:属性“入口"的值不合适在创建 SG 时 [英] Terraform: Inappropriate value for attribute "ingress" while creating SG

查看:13
本文介绍了Terraform:属性“入口"的值不合适在创建 SG 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 terraform 创建一个安全组,并且在运行 terraform 计划时.它给了我一个错误,比如某些字段是必需的,而所有这些字段都是可选的.

Terraform 版本:v1.0.5

AWS 提供商版本:v3.57.0

<块引用>

main.tf

资源aws_security_group"sg_oregon"{名称=tf-sg";描述=允许网络流量";vpc_id = aws_vpc.vpc_terraform.id入口= [{描述=HTTP";from_port = 80to_port = 80协议=tcp";cidr_blocks = [0.0.0.0/0"]},{描述=HTTPS";来自端口 = 443to_port = 443协议=tcp";cidr_blocks = [0.0.0.0/0"]},{描述=SSH"从端口 = 22to_port = 22协议=tcp";cidr_blocks = [0.0.0.0/0"]}]出口 = [{description =对于所有传出流量";从端口 = 0to_port = 0协议=-1";cidr_blocks = [0.0.0.0/0"]ipv6_cidr_blocks = [::/0"]}]标签 = {名称 = sg-for-subnet"}}

<块引用>

控制台出错

│属性ingress"的不适当值:元素0:属性ipv6_cidr_blocks"、prefix_list_ids"、security_groups"和self";是必须的.│ 属性egress"的不适当值:元素 0:属性prefix_list_ids"、security_groups"和self"是必须的.

我正在关注此文档:https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group

任何帮助将不胜感激.

解决方案

由于您使用的是 属性作为块你必须为所有选项提供值:

资源aws_security_group"sg_oregon"{名称=tf-sg";描述=允许网络流量";vpc_id = aws_vpc.vpc_terraform.id入口= [{描述=HTTP";from_port = 80to_port = 80协议=tcp";cidr_blocks = [0.0.0.0/0"]ipv6_cidr_blocks = []prefix_list_ids = []安全组 = []自我=假},{描述=HTTPS";来自端口 = 443to_port = 443协议=tcp";cidr_blocks = [0.0.0.0/0"]ipv6_cidr_blocks = []prefix_list_ids = []安全组 = []自我=假},{描述=SSH"从端口 = 22to_port = 22协议=tcp";cidr_blocks = [0.0.0.0/0"]ipv6_cidr_blocks = []prefix_list_ids = []安全组 = []自我=假}]出口 = [{description =对于所有传出流量";从端口 = 0to_port = 0协议=-1";cidr_blocks = [0.0.0.0/0"]ipv6_cidr_blocks = [::/0"]prefix_list_ids = []安全组 = []自我=假}]标签 = {名称 = sg-for-subnet"}}

I'm creating a Security group using terraform, and when I'm running terraform plan. It is giving me an error like some fields are required, and all those fields are optional.

Terraform Version: v1.0.5

AWS Provider version: v3.57.0

main.tf

resource "aws_security_group" "sg_oregon" {
  name        = "tf-sg"
  description = "Allow web traffics"
  vpc_id      = aws_vpc.vpc_terraform.id

  ingress = [
    {
      description      = "HTTP"
      from_port        = 80
      to_port          = 80
      protocol         = "tcp"
      cidr_blocks      = ["0.0.0.0/0"]  
    },
  {
      description      = "HTTPS"
      from_port        = 443
      to_port          = 443
      protocol         = "tcp"
      cidr_blocks      = ["0.0.0.0/0"]  
  },

    {
      description      = "SSH"
      from_port        = 22
      to_port          = 22
      protocol         = "tcp"
      cidr_blocks      = ["0.0.0.0/0"]  
    }
  ]


  egress = [
    {
      description      = "for all outgoing traffics"
      from_port        = 0
      to_port          = 0
      protocol         = "-1"
      cidr_blocks      = ["0.0.0.0/0"]
      ipv6_cidr_blocks = ["::/0"]
      
    }
  ]

  tags = {
    Name = "sg-for-subnet"
  }
}

error in console

│ Inappropriate value for attribute "ingress": element 0: attributes "ipv6_cidr_blocks", "prefix_list_ids", "security_groups", and "self" are required.

│ Inappropriate value for attribute "egress": element 0: attributes "prefix_list_ids", "security_groups", and "self" are required.

I'm following this doc: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group

Any help would be appreciated.

解决方案

Since you are using Attributes as Blocks you have to provide values for all options:

resource "aws_security_group" "sg_oregon" {
  name        = "tf-sg"
  description = "Allow web traffics"
  vpc_id      = aws_vpc.vpc_terraform.id

  ingress = [
    {
      description      = "HTTP"
      from_port        = 80
      to_port          = 80
      protocol         = "tcp"
      cidr_blocks      = ["0.0.0.0/0"]  
      ipv6_cidr_blocks = []
      prefix_list_ids = []
      security_groups = []
      self = false
    },
  {
      description      = "HTTPS"
      from_port        = 443
      to_port          = 443
      protocol         = "tcp"
      cidr_blocks      = ["0.0.0.0/0"]  
      ipv6_cidr_blocks = []
      prefix_list_ids = []
      security_groups = []
      self = false      
  },

    {
      description      = "SSH"
      from_port        = 22
      to_port          = 22
      protocol         = "tcp"
      cidr_blocks      = ["0.0.0.0/0"]  
      ipv6_cidr_blocks = []
      prefix_list_ids = []
      security_groups = []
      self = false      
    }
  ]


  egress = [
    {
      description      = "for all outgoing traffics"
      from_port        = 0
      to_port          = 0
      protocol         = "-1"
      cidr_blocks      = ["0.0.0.0/0"]
      ipv6_cidr_blocks = ["::/0"]
      prefix_list_ids = []
      security_groups = []
      self = false
    }
  ]

  tags = {
    Name = "sg-for-subnet"
  }
}

这篇关于Terraform:属性“入口"的值不合适在创建 SG 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆