Terraform ELB S3 Permissions Issue

前端 未结 3 1726
执笔经年
执笔经年 2020-12-10 02:37

I am having an issue using Terraform (v0.9.2) adding services to an ELB (I\'m using: https://github.com/segmentio/stack/blob/master/s3-logs/main.tf).

When I run

相关标签:
3条回答
  • 2020-12-10 02:49

    In the bucket policy, the account number must be NOT yours. Instead it belongs to AWS, and for each region, the account numbers you should use in your bucket policy are listed at: https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/enable-access-logs.html#attach-bucket-policy

    For instance, for us-east-1 region the account number is 127311923021.

    Although the question is about Terraform, I post CloudFormation snippet created a bucket for ELB's access logs its Bucket policy:

        MyAccessLogsBucket:
            Type: AWS::S3::Bucket
            DeletionPolicy: Retain
    
    
        MyAllowELBAccessBucketPolicy:
            Type: AWS::S3::BucketPolicy
            Properties: 
                Bucket: !Ref MyAccessLogsBucket
                PolicyDocument: 
                    Version: "2012-10-17"
                    Statement: 
                        - Effect: "Allow"
                          Principal: 
                              AWS: "arn:aws:iam::127311923021:root"
                          Action: 
                              - "s3:PutObject"
                          Resource: !Sub "arn:aws:s3:::${MyAccessLogsBucket}/AWSLogs/*"
    
    

    In the principle, 127311923021 is used as this is AWS account number which should be used for account number in us-east-1.

    0 讨论(0)
  • 2020-12-10 02:59

    The docs for ELB access logs say that you want to allow a specific Amazon account to be able to write to S3, not your account.

    As such you want something like:

    {
      "Id": "Policy1429136655940",
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "Stmt1429136633762",
          "Action": [
            "s3:PutObject"
          ],
          "Effect": "Allow",
          "Resource": "arn:aws:s3:::my-loadbalancer-logs/my-app/AWSLogs/123456789012/*",
          "Principal": {
            "AWS": [
              "652711504416"
            ]
          }
        }
      ]
    }
    

    In Terraform you can use the aws_elb_service_account data source to automatically fetch the account ID used for writing logs as can be seen in the example in the docs:

    data "aws_elb_service_account" "main" {}
    
    resource "aws_s3_bucket" "elb_logs" {
      bucket = "my-elb-tf-test-bucket"
      acl    = "private"
    
      policy = <<POLICY
    {
      "Id": "Policy",
      "Version": "2012-10-17",
      "Statement": [
        {
          "Action": [
            "s3:PutObject"
          ],
          "Effect": "Allow",
          "Resource": "arn:aws:s3:::my-elb-tf-test-bucket/AWSLogs/*",
          "Principal": {
            "AWS": [
              "${data.aws_elb_service_account.main.arn}"
            ]
          }
        }
      ]
    }
    POLICY
    }
    
    resource "aws_elb" "bar" {
      name               = "my-foobar-terraform-elb"
      availability_zones = ["us-west-2a"]
    
      access_logs {
        bucket   = "${aws_s3_bucket.elb_logs.bucket}"
        interval = 5
      }
    
      listener {
        instance_port     = 8000
        instance_protocol = "http"
        lb_port           = 80
        lb_protocol       = "http"
      }
    }
    
    0 讨论(0)
  • 2020-12-10 03:00

    Even when having everything by the docs, I kept getting the "Access Denied for bucket" error. Removing the encryption from the bucket worked for me.

    0 讨论(0)
提交回复
热议问题