Terraform启用vpc流日志到Amazon S3

时间:2018-11-07 11:32:32

标签: amazon-web-services terraform terraform-provider-aws aws-automation infrastructure-as-a-code

我正在研究terraform脚本以自动创建AWS资源。作为其一部分,我将创建一个vpc并尝试为此启用vpc流日志。正如terraform文档https://www.terraform.io/docs/providers/aws/r/flow_log.html

中所述,我已经创建了一个s3存储桶,还创建了一个iam角色。

下面给出了我的Terraform代码

data "aws_s3_bucket" "selected" {
  bucket = "${var.s3_bucket_name}"
}

resource "aws_flow_log" "vpc_flow_log" {
    count = "${var.enable_vpc_flow_log}"
    iam_role_arn    = "${aws_iam_role.test_role.arn}"
    log_destination      = "${data.aws_s3_bucket.selected.arn}"
    log_destination_type = "s3"
    traffic_type         = "ALL"
    vpc_id               = "${var.vpc_id}"

}

resource "aws_iam_role" "test_role" {
  name = "example"
  count = "${var.enable_vpc_flow_log}"
  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "vpc-flow-logs.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF
}
resource "aws_iam_role_policy" "example" {
  name = "example"
  count = "${var.enable_vpc_flow_log}"
  role = "${aws_iam_role.test_role.id}"
  policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents",
        "logs:DescribeLogGroups",
        "logs:DescribeLogStreams"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
EOF
}

执行Terraform计划时出现以下错误

Error: module.enable_vpc_flow_log.aws_flow_log.vpc_flow_log: "log_group_name": required field is not set

Error: module.enable_vpc_flow_log.aws_flow_log.vpc_flow_log: : invalid or unknown key: log_destination

Error: module.enable_vpc_flow_log.aws_flow_log.vpc_flow_log: : invalid or unknown key: log_destination_type

根据terraform文档 log_group_name 是可选的,只有在选择cloud_watch_logs作为 log_destination_type

时,才需要指定其值

任何人都可以帮助我解决我的错误并启用vpc流日志到s3。

3 个答案:

答案 0 :(得分:1)

我也遇到了此错误,因为我正在使用AWS提供程序的1.41。浏览发现的代码that support for these properties仅在1.42中发布。升级到1.49就可以了。

答案 1 :(得分:0)

我将自己的Terraform版本从 0.11.8 更新到了 0.11.10 。现在,我可以使用以下资源块将vpc流日志配置为s3,而不会出现任何错误。

resource "aws_flow_log" "vpc_flow_log" {
    log_destination      = "${var.s3_bucket_arn}"
    log_destination_type = "s3"
    traffic_type         = "ALL"
    vpc_id               = "${var.vpc_id}"
}

答案 2 :(得分:0)

发送 VPC 的日志到 s3 时,您不能设置 log_group_name,但您可以将组名附加到 s3 的 arn 中,它会自动为您创建一个文件夹。

socket.id