Interview Prep/AWS/Route 53, CloudFront, API Gateway and Messaging

CloudFront returns `403 AccessDenied` (S3 XML) for every object after you switched the S3 origin to Origin Access Control.

IntermediateTroubleshootingRoute 53, CloudFront, API Gateway and Messaging
Answer

Likely causes → The bucket policy does not allow the cloudfront.amazonaws.com service principal with AWS:SourceArn set to this distribution. Other causes: objects are SSE-KMS and the key policy does not allow CloudFront, the request has no default root object, or the key is missing (S3 returns 403 instead of 404 without s3:ListBucket).

Diagnose →

aws cloudfront get-distribution-config --id E123ABC --query 'DistributionConfig.[DefaultRootObject,Origins.Items[].[DomainName,OriginAccessControlId]]'
aws s3api get-bucket-policy --bucket site-assets --output text
aws s3api head-object --bucket site-assets --key index.html

Fix →

{"Effect":"Allow","Principal":{"Service":"cloudfront.amazonaws.com"},
 "Action":"s3:GetObject","Resource":"arn:aws:s3:::site-assets/*",
 "Condition":{"StringEquals":{"AWS:SourceArn":"arn:aws:cloudfront::111111111111:distribution/E123ABC"}}}

Add a kms:Decrypt statement for CloudFront to the key policy if the objects use SSE-KMS, and set DefaultRootObject to index.html.

Prevent → Create the OAC, the bucket policy and the distribution together in one IaC module.

aws All AWS questions Roadmap: AWS core services

More on Route 53, CloudFront, API Gateway and Messaging