【问题标题】:Terraform: Setting up logging from AWS LoadBalancer to S3 bucketTerraform:设置从 AWS LoadBalancer 到 S3 存储桶的日志记录
【发布时间】:2019-11-07 02:48:36
【问题描述】:

我有一个想要记录到 S3 存储桶的 aws_lb。

我没有成功的尝试:

data "aws_elb_service_account" "main" {}

data "aws_iam_policy_document" "bucket_policy" {
  statement {
    sid       = ""
    actions   = ["s3:PutObject"]
    resources = ["arn:aws:s3:::my-bucket/*"]

    principals {
      type        = "AWS"
      identifiers = ["${data.aws_elb_service_account.main.arn}"]
    }
  }
}

我也试过这个:

resource "aws_iam_role" "lb-logs-role" {
  name = "lb-logs-role"

  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Principal": {
        "Service": "elasticloadbalancing.amazonaws.com"
      },
      "Effect": "Allow",
      "Sid": ""
    }
  ]
}
EOF

  tags = {
    Name = "lb-logs-role"
    Environment  = terraform.workspace
    Management   = "Managed by Terraform"
  }
}

resource "aws_iam_role_policy" "s3-logs-access" {
  name = "s3-logs-access"
  role = aws_iam_role.lb-logs-role.id

  policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "s3:PutObject"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::my-bucket/*"
    }
  ]
}
EOF
}

这是我看到的错误:

Error: Failure configuring LB attributes: InvalidConfigurationRequest: Access Denied for bucket: my-bucket. Please check S3bucket permission
        status code: 400, request id: 5b629210-9738-11e9-bcc6-6f3b4f22bf28

  on modules/tableau-linux/lb.tf line 1, in resource "aws_lb" "main":
   1: resource "aws_lb" "main" {

有什么想法吗?

【问题讨论】:

  • 请添加您在编辑问题时遇到的错误。
  • 我添加了错误。不知道有多大用处。
  • 我已经尝试了可能重复的代码,但无济于事。不知道这是否重要,但该问题使用 aws_elb,而不是 aws_lb。
  • @JosephTura 请检查我使用 Terraform 版本进行测试Terraform v0.12.1

标签: terraform terraform-provider-aws


【解决方案1】:

看起来 API 将请求存储桶的 ACL 以查看其是否具有权限,并填充初始文件夹结构,因此即使 aws_elb_service_account 对存储桶中的 putObject 具有权限,api 调用也会失败。此策略是 AWS Web 控制台在为您创建 S3 存储桶时创建的内容,它为我解决了这个问题。

data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
data "aws_elb_service_account" "main" {}
resource "aws_s3_bucket_policy" "lb-bucket-policy" {
  bucket = aws_s3_bucket.lb-log-storage-s3.id

  policy = <<POLICY
{
    "Id": "Policy",
    "Version": "2012-10-17",
    "Statement": [{
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "${data.aws_elb_service_account.main.arn}"
                ]
            },
            "Action": [
                "s3:PutObject"
            ],
            "Resource": "${aws_s3_bucket.lb-log-storage-s3.arn}/AWSLogs/${data.aws_caller_identity.current.account_id}/*"
        },
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "delivery.logs.amazonaws.com"
            },
            "Action": [
                "s3:PutObject"
            ],
            "Resource": "${aws_s3_bucket.lb-log-storage-s3.arn}/AWSLogs/${data.aws_caller_identity.current.account_id}/*",
            "Condition": {
                "StringEquals": {
                    "s3:x-amz-acl": "bucket-owner-full-control"
                }
            }
        },
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "delivery.logs.amazonaws.com"
            },
            "Action": [
                "s3:GetBucketAcl"
            ],
            "Resource": "${aws_s3_bucket.lb-log-storage-s3.arn}"
        }
    ]
}
POLICY
}

【讨论】:

    【解决方案2】:

    似乎问题出在您的策略上,但您可以使用 aws_lb 尝试我的代码,这是在默认 VPC 中启动到 LB 并创建名为 test-bucket-1-unique-name 的存储桶、策略和名为 test-http-lb 的 LB 的完整配置。连同被评论的 SG 和 Route53 条目。

    # Creating Load Balancer
    resource "aws_lb" "httplb" {
      name                       = "test-http-lb"
      internal                   = false
      load_balancer_type         = "application"
      security_groups            = ["${aws_security_group.lbsg.id}"]
      subnets                       = ["subnet-99fdf8e0", "subnet-902b0ddb"]
      enable_deletion_protection = false
      access_logs {
        bucket  = "${aws_s3_bucket.bucket.bucket}"
        prefix  = "http-lb"
        enabled = true
      }
      tags = {
        Environment = "test-http"
      }
    }
    
    
    # Creating Security Groups for Load Balancer
    resource "aws_security_group" "lbsg" {
      name        = "test-loadbalancer-sg"
      description = "test-Allow LB traffic"
      tags = {
        Name = "test-SG-Balancer"
      }
    
      ingress {
        from_port   = 80
        to_port     = 80
        protocol    = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
        description = "HTTP"
      }
    
      egress {
        from_port   = 0
        to_port     = 0
        protocol    = "-1"
        cidr_blocks = ["0.0.0.0/0"]
      }
    }
    
    
    
    
    
    
    
    
    
    
    #uncomment this if you want to add route53 record
    # resource "aws_route53_record" "web" {
    #   zone_id = "${data.aws_route53_zone.primary.zone_id}"
    #   name    = "${var.env_prefix_name}.ironman.co
    #   type    = "A"
    
    #   alias {
    #     name                   = "${aws_lb.httplb.dns_name}"
    #     zone_id                = "${aws_lb.httplb.zone_id}"
    #     evaluate_target_health = true
    #   }
    # }
    
    
    
    
    data "aws_elb_service_account" "main" {}
    
    # Creating policy on S3, for lb to write
    resource "aws_s3_bucket_policy" "lb-bucket-policy" {
      bucket = "${aws_s3_bucket.bucket.id}"
    
      policy = <<POLICY
    {
      "Id": "testPolicy1561031527701",
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "testStmt1561031516716",
          "Action": [
            "s3:PutObject"
          ],
          "Effect": "Allow",
          "Resource": "arn:aws:s3:::test-bucket-1-for-lb-logs/http-lb/*",
          "Principal": {
            "AWS": [
               "${data.aws_elb_service_account.main.arn}"
            ]
          }
        }
      ]
    }
    POLICY
    }
    
    resource "aws_s3_bucket" "bucket" {
      bucket = "test-bucket-1-for-lb-logs"
      acl    = "private"
      region = "us-west-2"
    
      versioning {
        enabled = false
      }
      force_destroy = true
    
    
    }
    

    然后转到您的 S3 存储桶并验证 TestFile 这是来自 terraform 的日志

    【讨论】:

    • @Joseph 你试过这个吗?
    猜你喜欢
    • 2019-08-23
    • 2020-01-17
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2022-07-12
    相关资源
    最近更新 更多