{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowCloudFrontServicePrincipal",
"Effect": "Allow",
"Principal": {
"Service": "cloudfront.amazonaws.com"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::your-bucket-name/*",
"Condition": {
"StringEquals": {
"AWS:SourceArn": "arn:aws:cloudfront::account-id:distribution/distribution-id"
}
}
}
]
}
{
"Rules": [
{
"ID": "DeleteOldVersions",
"Status": "Enabled",
"NoncurrentVersionExpiration": {
"NoncurrentDays": 30
}
}
]
}
Symptoms: Cannot access S3 objects through CloudFront Causes:
Solutions:
# Check bucket policy
aws s3api get-bucket-policy --bucket your-bucket-name
# Verify CloudFront OAC configuration
aws cloudfront get-origin-access-control --id your-oac-id
# Update bucket policy for OAC
aws s3api put-bucket-policy --bucket your-bucket-name --policy file://policy.json
Issue: Confusion between S3 website endpoint and bucket endpoint
S3 Website Endpoint:
bucket-name.s3-website-region.amazonaws.com
S3 Bucket Endpoint:
bucket-name.s3.region.amazonaws.com
Symptoms: Browser blocks requests from web applications Solution:
[
{
"AllowedHeaders": ["*"],
"AllowedMethods": ["GET", "HEAD"],
"AllowedOrigins": ["https://yourdomain.com"],
"ExposeHeaders": ["ETag"],
"MaxAgeSeconds": 3000
}
]
Issue: Unexpected high storage costs Monitoring:
# List object versions
aws s3api list-object-versions --bucket your-bucket-name
# Check storage metrics
aws cloudwatch get-metric-statistics \
--namespace AWS/S3 \
--metric-name BucketSizeBytes \
--dimensions Name=BucketName,Value=your-bucket-name \
--start-time 2024-01-01T00:00:00Z \
--end-time 2024-01-31T23:59:59Z \
--period 86400 \
--statistics Average
# Create bucket with encryption
aws s3api create-bucket --bucket my-secure-bucket \
--region us-east-1 \
--create-bucket-configuration LocationConstraint=us-east-1
# Enable versioning
aws s3api put-bucket-versioning --bucket my-bucket \
--versioning-configuration Status=Enabled
# Set lifecycle policy
aws s3api put-bucket-lifecycle-configuration --bucket my-bucket \
--lifecycle-configuration file://lifecycle.json
# Check bucket policy
aws s3api get-bucket-policy --bucket my-bucket
# List all versions of objects
aws s3api list-object-versions --bucket my-bucket
# Get bucket metrics
aws s3api get-bucket-metrics-configuration --bucket my-bucket \
--id my-metrics-config
Before deploying to production:
If you’re using the setup from this tutorial in production:
Remember: This tutorial is for learning purposes. Production environments require additional security and performance considerations.