4
Access Denied for bucket: appdeploy-logbucket-1cca50r865s65. 
Please check S3bucket permission (Service: AmazonElasticLoadBalancingV2; Status Code: 400; Error Code: 
InvalidConfigurationRequest; Request ID: e5e2245f-2f9b-11e9-a3e9-2dcad78a31ec)

I want to store my ALB logs to s3 bucket, i have added policies to s3 bucket, but it says access denied, i have tried alot, and worked with so many configurations, but it failed again and again, And my stack Roll back, I have used Troposphere to create template.

I have tried my policies using but it's not wokring.

BucketPolicy = t.add_resource(
    s3.BucketPolicy(
        "BucketPolicy",
        Bucket=Ref(LogBucket),
        PolicyDocument={
            "Id": "Policy1550067507528",
            "Version": "2012-10-17",
            "Statement": [
              {
                   "Sid": "Stmt1550067500750",
                   "Action": [
                    "s3:PutObject",
                    "s3:PutBucketAcl",
                    "s3:PutBucketLogging",
                    "s3:PutBucketPolicy"
                   ],
                   "Effect": "Allow",
                   "Resource": Join("", [
                     "arn:aws:s3:::",
                     Ref(LogBucket),
                     "/AWSLogs/",
                     Ref("AWS::AccountId"),
                     "/*"]),
                   "Principal": {"AWS": "027434742980"},
              }
            ],
            },
    ))

Any help?

Rajat jain
  • 1,715
  • 3
  • 12
  • 21
  • 1
    Where did you find the value `162827895266`? I don't see that on the [list](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html#access-logging-bucket-permissions). – Michael - sqlbot Feb 13 '19 at 16:00
  • @Michael-sqlbot I was a bit confused but it helps me, and save my efforts. Thanks – Rajat jain Feb 14 '19 at 06:36
  • was that the problem? – Michael - sqlbot Feb 14 '19 at 12:10
  • Well yeah i suppose it was `account ID`. Also adding Multiple `"Action": [ "s3:PutObject", "s3:PutBucketAcl", "s3:PutBucketLogging", "s3:PutBucketPolicy" ],` was not right. Thanks for your HINT – Rajat jain Feb 14 '19 at 12:36

2 Answers2

4

troposphere/stacker maintainer here. We have a stacker blueprint (which is a wrapper around a troposphere template) that we use at work for our logging bucket:

from troposphere import Sub
from troposphere import s3

from stacker.blueprints.base import Blueprint

from awacs.aws import (
    Statement, Allow, Policy, AWSPrincipal
)
from awacs.s3 import PutObject


class LoggingBucket(Blueprint):
    VARIABLES = {
        "ExpirationInDays": {
            "type": int,
            "description": "Number of days to keep logs around for",
        },
        # See the table here for account ids.
        # https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/enable-access-logs.html#attach-bucket-policy
        "AWSAccountId": {
            "type": str,
            "description": "The AWS account ID to allow access to putting "
                           "logs in this bucket.",
            "default": "797873946194"  # us-west-2
        },
    }

    def create_template(self):
        t = self.template
        variables = self.get_variables()

        bucket = t.add_resource(
            s3.Bucket(
                "Bucket",
                LifecycleConfiguration=s3.LifecycleConfiguration(
                    Rules=[
                        s3.LifecycleRule(
                            Status="Enabled",
                            ExpirationInDays=variables["ExpirationInDays"]
                        )
                    ]
                )
            )
        )

        # Give ELB access to PutObject in the bucket.
        t.add_resource(
            s3.BucketPolicy(
                "BucketPolicy",
                Bucket=bucket.Ref(),
                PolicyDocument=Policy(
                    Statement=[
                        Statement(
                            Effect=Allow,
                            Action=[PutObject],
                            Principal=AWSPrincipal(variables["AWSAccountId"]),
                            Resource=[Sub("arn:aws:s3:::${Bucket}/*")]
                        )
                    ]
                )
            )
        )

        self.add_output("BucketId", bucket.Ref())
        self.add_output("BucketArn", bucket.GetAtt("Arn"))

Hopefully that helps!

phobologic
  • 424
  • 3
  • 5
2

The principal is wrong in the CloudFormation template. You should use the proper principal AWS Account Id for your region. Lookup the proper value in this document:

https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html#access-logging-bucket-permissions

Also, you could narrow down your actions. If you just want to push ALB logs to S3, you only need:

        Action: s3:PutObject

Here's a sample BucketPolicy Cloudformation that works (you can easily translate that into the troposphere PolicyDocument element):

Resources:

  # Create an S3 logs bucket
  ALBLogsBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub "my-logs-${AWS::AccountId}"
      AccessControl: LogDeliveryWrite
      LifecycleConfiguration:
        Rules:
          - Id: ExpireLogs
            ExpirationInDays: 365
            Status: Enabled
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true
    DeletionPolicy: Retain

  # Grant access for the load balancer to write the logs
  # For the magic number 127311923021, refer to https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html#access-logging-bucket-permissions
  ALBLoggingBucketPolicy:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: !Ref ALBLogsBucket
      PolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              AWS: 127311923021 # Elastic Load Balancing Account ID for us-east-1
            Action: s3:PutObject
            Resource: !Sub "arn:aws:s3:::my-logs-${AWS::AccountId}/*"
Pierre
  • 2,335
  • 22
  • 40
  • 1
    In this example, ID "127311923021" is for us-east-1. But if you want other regions, I found a list of their respective IDs here https://stackoverflow.com/a/47087131/5437264 . Though, if someone can provide guidance on how to find an official list of these IDs directly from AWS, that would be better. – ONMNZ Feb 28 '23 at 00:36
  • 1
    @ONMNZ the list of "magic numbers" can be found here (expand "Regions available before August 2022"): https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/enable-access-logs.html – Pierre Mar 07 '23 at 16:08