Other Articles

CloudFront Distribution – Protect S3 Origin Access

Secure Configuration Checks > AWS

This check ensures that every S3 bucket origin of a CloudFront distribution uses Origin Access Control (OAC) or Origin Access Identity (OAI). The bucket then stays private and can be read only through CloudFront.

Check Details

  • Resource: CloudFront Distribution
  • Check: Protect CloudFront S3 origin access (S3OriginAccessProtected)
  • Risk: Users can bypass CloudFront and read the bucket directly

Remediation via AWS Console

  1. Open the Amazon CloudFront console.

    Amazon CloudFront Console
  2. Navigate to Origin access > Create control setting. Enter a name, select S3, keep Sign requests (recommended), and click Create.

    Create CloudFront Origin Access Control
  3. Open Distributions, select the affected distribution, and navigate to Origins.

    CloudFront Origins tab
  4. Edit the affected S3 origin. Under Origin access, select Origin access control settings (recommended) and choose the OAC created earlier.

  5. Update the S3 bucket policy to allow cloudfront.amazonaws.com access, restricting it with the AWS:SourceArn condition to the affected distribution.

    S3 bucket policy for CloudFront OAC
  6. Save the CloudFront origin changes and repeat for other S3 origins requiring restricted access.

  7. Wait for deployment to complete and verify that the content is accessible through CloudFront.

Note: OAC supports regular S3 bucket origins, not S3 static website endpoints. For SSE-KMS encrypted buckets, update the KMS key policy to grant CloudFront access. Preserve existing bucket policy statements when adding permissions.

Remediation via AWS CLI

  1. Log in to the AWS Management Console and click the CloudShell icon (>_) in the top-right corner.

    AWS CloudShell
  2. Create an origin access control and note its ID:

    aws cloudfront create-origin-access-control \
    --origin-access-control-config \
    "Name=<oac-name>,SigningProtocol=sigv4,SigningBehavior=always,OriginAccessControlOriginType=s3" \
    --query 'OriginAccessControl.Id' \
    --output text
    
  3. Allow the distribution to read the bucket:

    cat > policy.json <<EOF
    {
      "Version": "2012-10-17",
      "Statement": {
        "Sid": "AllowCloudFrontServicePrincipalReadOnly",
        "Effect": "Allow",
        "Principal": {"Service": "cloudfront.amazonaws.com"},
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::<bucket-name>/*",
        "Condition": {"StringEquals": {"AWS:SourceArn":
          "arn:aws:cloudfront::<account-id>:distribution/<distribution-id>"}}
      }
    }
    EOF
    
    aws s3api put-bucket-policy \
    --bucket <bucket-name> \
    --policy file://policy.json
    
    

    This command replaces the existing bucket policy. If the bucket already has one, merge the statement into it first.

  4. Download the current distribution configuration:

    aws cloudfront get-distribution-config \
    --id <distribution-id> > dist-config.json
    
  5. Attach the OAC to the S3 origin:

    OAC_ID="<oac-id>"
    ORIGIN_ID="<origin-id>"
    ETAG=$(jq -r '.ETag' dist-config.json)
    
    jq --arg oac "$OAC_ID" --arg oid "$ORIGIN_ID" '.DistributionConfig
    | .Origins.Items |= map(
    if .Id == $oid then
    .OriginAccessControlId = $oac
    | .S3OriginConfig.OriginAccessIdentity = ""
    else . end)' \
     dist-config.json > updated-config.json
    
    
  6. Apply the updated configuration:

    aws cloudfront update-distribution \
    --id <distribution-id> \
    --if-match "$ETAG" \
    --distribution-config file://updated-config.json
    
  7. Verify that every S3 origin has an access method:

    aws cloudfront get-distribution-config \
    --id <distribution-id> \
    --query 'DistributionConfig.Origins.Items[*].[Id,OriginAccessControlId,S3OriginConfig.OriginAccessIdentity]' \
    --output table
    

Replace <distribution-id> with your distribution ID, for example E1ABCDEF234567. Replace <origin-id> with the origin ID shown on the Origins tab. Repeat steps 4 to 6 for each S3 origin.

Updated on 03 November, 2026