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

问题描述

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

地形版本:v1.0.5

AWS 提供商版本:v3.57.0

main.tf

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

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"
  }
}
│ 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 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:12