为什么 terraform 失败并显示“此处不需要名为“flow_log_destination_type”的参数”?

Anu*_*hor 16 amazon-web-services terraform

当我使用 terraform 创建 vpc 流日志模块到 s3 存储桶时,它会抛出如下错误:

  An argument named "flow_log_destination_type" is not expected here.
  An argument named "flow_log_destination_arn" is not expected here.
Run Code Online (Sandbox Code Playgroud)

在 Terraform 文档中,我可以看到要填写的详细信息,例如log_destination_type & log_destination_arn,并且我在 GitHub 上找到了一些文档,这些文档完全相同,但在尝试时它对我不起作用

产生以下错误:

Error: Unsupported argument

  on main.tf line 52, in module "vpc_with_flow_logs_s3_bucket":
  52:   flow_log_destination_type = "s3"

An argument named "flow_log_destination_type" is not expected here.


Error: Unsupported argument

  on main.tf line 53, in module "vpc_with_flow_logs_s3_bucket":
  53:   flow_log_destination_arn  = "${aws_s3_bucket.terra-test2-lifecycle.arn}"

An argument named "flow_log_destination_arn" is not expected here.


Error: Unsupported argument

  on main.tf line 55, in module "vpc_with_flow_logs_s3_bucket":
  55:   vpc_flow_log_tags = {

An argument named "vpc_flow_log_tags" is not expected here.
Run Code Online (Sandbox Code Playgroud)

我哪里做错了?

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "2.33.0"
  # Interpolated from the workspace
  name = "${terraform.workspace}"
  cidr = var.vpc_cidr

  azs             = var.vpc_azs
  private_subnets = var.vpc_private_subnets
  public_subnets  = var.vpc_public_subnets

  enable_nat_gateway = var.vpc_enable_nat_gw
  single_nat_gateway = var.vpc_single_nat_gw

  public_subnet_tags = {
    Name = "${terraform.workspace}-public"
  }

  private_subnet_tags = {
    Name = "${terraform.workspace}-private"
  }

  tags = {
    Name = "${terraform.workspace}"
  }

  vpc_tags = {
    owner       = "PEDevOps"
    environment = "${terraform.workspace}"
    version     = "0.0.1"
    managedby   = "Terraform"
  }
}

module "vpc_with_flow_logs_s3_bucket" {
  source = "../../"
  log_destination_type = "s3"
  log_destination_arn  = "${aws_s3_bucket.terra-test2-lifecycle.arn}"

  vpc_flow_log_tags = {
    Name = "vpc-flow-logs-s3-bucket"
  }
  
}

resource "aws_s3_bucket" "terra-test-lifecycle" {
  bucket = "terra-test-lifecycle"
  acl    = "private"

  lifecycle_rule {
    id      = "log"
    enabled = true

    prefix = "log/"

    tags = {
      "rule"      = "log"
      "autoclean" = "true"
    }

    transition {
      days          = 30
      storage_class = "STANDARD_IA" # or "ONEZONE_IA"
    }

    expiration {
      days = 60
    }
  }
  lifecycle_rule {
    id      = "tmp"
    prefix  = "tmp/"
    enabled = true

    expiration {
      date = "2020-06-06"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

为什么 terraform 会失败An argument named "flow_log_destination_type" is not expected here

Ala*_*Dea 11

“../../”处的模块未声明任何log_destination_typelog_destination_arnvpc_flow_log_tags变量,并且 Terraform 认为分配给模块块中未声明的变量是错误的,如下所示:

module "vpc_with_flow_logs_s3_bucket" {
  source = "../../"
  log_destination_type = "s3"
  log_destination_arn  = "${flow_log_destination_arn}"

  vpc_flow_log_tags = {
    Name = "vpc-flow-logs-s3-bucket"
  }
}
Run Code Online (Sandbox Code Playgroud)

“../../”很可能是vpc_with_flow_logs_s3_bucket模块的错误源路径,您应该修复该问题。如果您位于声明此模块块的模块的源路径中并且运行cd ../../,您最终是否会进入包含vpc_with_flow_logs_s3_bucket Terraform 代码的目录?如果不是,则设置不正确,您需要修复它。

如果“../../”是正确的路径,那么您应该添加缺少的变量声明。

variable "log_destination_type" {
    type = string
}

variable "log_destination_arb" {
    type = string
}

variable "vpc_flow_log_tags" {
    type = map(string)
}
Run Code Online (Sandbox Code Playgroud)


err*_*404 2

如果您传递模块不期望的变量,则会发生此错误。

例如

module "vpc_with_flow_logs_s3_bucket" {
  source = "../../"
  log_destination_type = "s3"
  log_destination_arn  = "${flow_log_destination_arn}"

  vpc_flow_log_tags = {
    Name = "vpc-flow-logs-s3-bucket"
  }

}
Run Code Online (Sandbox Code Playgroud)

如果指定此变量,如果变量 flow_log_destination_arn 是在 main.tf 中定义的,而不是在 Variables.tf 中定义的,则会抛出错误

来源: ../../ vpc_with_flow_logs_s3_bucket/main.tf

resource "aws_flow_log" "example" {
  iam_role_arn    = "${aws_iam_role.example.arn}"
  log_destination = "${aws_cloudwatch_log_group.example.arn}"
  traffic_type    = "ALL"
  vpc_id          = "${aws_vpc.example.id}"
}
Run Code Online (Sandbox Code Playgroud)