S3 bucket policy not associated with its target

Attach required bucket policies to the correct bucket.

Description

In AWS::S3::BucketPolicy, point Bucket at the bucket you intend to control. Even without a bucket policy, IAM permissions and other S3 controls apply.

Potential impact

An invalid reference can fail deployment or leave the intended bucket without its policy.

Remediation

Use !Ref on the bucket’s logical ID and check the object ARN and reader role. After deployment, verify that the policy is attached to the intended bucket and the role can read its objects.

Examples

The first example references a bucket that is not defined. The second references S3Bucket and its object ARN. For an existing bucket, retain its actual resource identity and name and define the approved BucketReaderRole separately.

Before

yaml
Resources:
  S3Bucket3:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: docexamplebucket1

  SampleBucketPolicy5:
    Type: 'AWS::S3::BucketPolicy'
    Properties:
      Bucket:
        Ref: docexamplebucketfail
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Action:
              - 's3:GetObject'
            Effect: Allow
            Resource: !Sub '${S3Bucket3.Arn}/*'
            Principal:
              AWS: !GetAtt BucketReaderRole.Arn

After

yaml
Resources:
  S3Bucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: docexamplebucket

  SampleBucketPolicy:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: !Ref S3Bucket
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Action:
              - 's3:GetObject'
            Effect: Allow
            Resource: !Sub '${S3Bucket.Arn}/*'
            Principal:
              AWS: !GetAtt BucketReaderRole.Arn

References