存储桶记录表单Applicationloadbalancer的访问被拒绝:请检查S3bucket权限

时间:2019-02-13 14:43:55

标签: amazon-web-services amazon-s3 load-balancing amazon-cloudformation troposphere

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)

我想将我的ALB日志存储到s3存储桶中,我已将策略添加到s3存储桶中,但是它说访问被拒绝,我尝试了很多,并使用了许多配置,但是一次又一次失败,并且我的堆栈回滚,我已使用Troposphere创建模板。

我已尝试使用自己的策略,但操作不正常。

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"},
              }
            ],
            },
    ))

有帮助吗?

2 个答案:

答案 0 :(得分:1)

对流层/堆栈维护者。我们有一个堆栈器蓝图(它是对流层模板的包装纸),可用于工作记录桶:

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"))

希望有帮助!

答案 1 :(得分:0)

CloudFormation 模板中的主体错误。您应该为您所在的地区使用正确的主要 AWS 账户 ID。在此文档中查找正确的值:

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

此外,您可以缩小您的操作范围。如果你只是想将 ALB 日志推送到 S3,你只需要:

        Action: s3:PutObject

这是一个有效的 BucketPolicy Cloudformation 示例(您可以轻松地将其转换为对流层 PolicyDocument 元素):

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}/*"