AWS
CloudFrontDistributionAlreadyExists - CloudFront Distribution Already Exists
Getting a **CloudFrontDistributionAlreadyExists** error means a CloudFront distribution with the specified caller reference already exists—each distribution must have a unique caller reference, so you can't reuse the same reference. This client-side error (4xx) happens when AWS validates caller reference uniqueness. Most common when duplicate caller references are used, but also appears when distributions were already created, previous creation succeeded, caller reference collisions occur, or distributions exist with the same reference.
#Common Causes
- →Identity: IAM policy allows CloudFront but duplicate caller reference. Service Control Policy (SCP) enforces caller reference uniqueness.
- →Network: VPC endpoint CloudFront restrictions. Caller reference collision.
- →Limits: Duplicate caller reference. Distribution already created. Previous creation succeeded. Caller reference collision. Distribution exists with same reference.
✓Solutions
- 1Step 1: Diagnose - Check existing distributions: aws cloudfront list-distributions --query 'DistributionList.Items[*].[Id,Status,Comment]' --output table. Verify if distribution with caller reference exists.
- 2Step 2: Diagnose - Search for caller reference: aws cloudfront list-distributions --query "DistributionList.Items[?Comment=='CALLER_REF'].Id" --output text. Check if reference is already used.
- 3Step 3: Diagnose - Generate unique caller reference: Use timestamp and UUID: CALLER_REF="$(date +%s)-$(uuidgen | tr -d '-' | cut -c1-8)". Or use unique identifier.
- 4Step 4: Fix - Use unique caller reference: Generate new caller reference. Verify it's not in use. Use timestamp-based reference for uniqueness.
- 5Step 5: Fix - Check distribution status: If distribution exists, verify status: aws cloudfront get-distribution --id DIST_ID --query 'Distribution.Status' --output text. Or use existing distribution if appropriate.
</>Code Examples
Generate Unique Caller Reference
1#!/bin/bash
2echo "=== Generating Unique Caller Reference ==="
3
4# Generate timestamp-based caller reference
5CALLER_REF="$(date +%s)-$(uuidgen | tr -d '-' | cut -c1-8)"
6echo "Caller Reference: ${CALLER_REF}"
7
8# Alternative: Use timestamp only
9TIMESTAMP_REF="dist-$(date +%s)"
10echo "Timestamp Reference: ${TIMESTAMP_REF}"
11
12echo "\n=== Checking if Reference Already Exists ==="
13EXISTING=$(aws cloudfront list-distributions \
14 --query "DistributionList.Items[?Comment=='${CALLER_REF}'].Id" \
15 --output text 2>/dev/null)
16
17if [ ! -z "${EXISTING}" ]; then
18 echo "✗ Caller reference already exists (CloudFrontDistributionAlreadyExists)"
19 echo "Distribution ID: ${EXISTING}"
20 echo "Generate a new caller reference"
21else
22 echo "✓ Caller reference is unique"
23 echo "Safe to use: ${CALLER_REF}"
24fiList All CloudFront Distributions
1#!/bin/bash
2echo "=== All CloudFront Distributions ==="
3aws cloudfront list-distributions \
4 --query 'DistributionList.Items[*].[Id,Status,DomainName,Comment]' \
5 --output table
6
7echo "\n=== Distribution Count ==="
8DIST_COUNT=$(aws cloudfront list-distributions \
9 --query 'DistributionList.Quantity' \
10 --output text)
11
12echo "Total distributions: ${DIST_COUNT}"
13
14echo "\n=== Search for Specific Caller Reference ==="
15CALLER_REF="your-caller-reference"
16MATCHING=$(aws cloudfront list-distributions \
17 --query "DistributionList.Items[?Comment=='${CALLER_REF}'].[Id,Status]" \
18 --output table)
19
20if [ ! -z "${MATCHING}" ]; then
21 echo "Found distribution with caller reference:"
22 echo "${MATCHING}"
23 echo "\n✗ Cannot create duplicate (CloudFrontDistributionAlreadyExists)"
24else
25 echo "No distribution found with caller reference: ${CALLER_REF}"
26 echo "✓ Safe to create new distribution"
27fiCreate Distribution with Unique Caller Reference
1#!/bin/bash
2echo "=== Creating CloudFront Distribution ==="
3
4# Generate unique caller reference
5CALLER_REF="dist-$(date +%s)-$(uuidgen | tr -d '-' | cut -c1-8)"
6echo "Using caller reference: ${CALLER_REF}"
7
8# Create distribution config
9cat > dist-config.json <<EOF
10{
11 "CallerReference": "${CALLER_REF}",
12 "Comment": "My distribution",
13 "Origins": {
14 "Quantity": 1,
15 "Items": [
16 {
17 "Id": "origin1",
18 "DomainName": "example.com",
19 "CustomOriginConfig": {
20 "HTTPPort": 80,
21 "HTTPSPort": 443,
22 "OriginProtocolPolicy": "https-only"
23 }
24 }
25 ]
26 },
27 "DefaultCacheBehavior": {
28 "TargetOriginId": "origin1",
29 "ViewerProtocolPolicy": "redirect-to-https",
30 "AllowedMethods": {
31 "Quantity": 2,
32 "Items": ["GET", "HEAD"]
33 }
34 },
35 "Enabled": true
36}
37EOF
38
39echo "\n=== Creating Distribution ==="
40aws cloudfront create-distribution \
41 --distribution-config file://dist-config.json \
42 --output json
43
44if [ $? -eq 0 ]; then
45 echo "\n✓ Distribution created successfully"
46 echo "Caller reference: ${CALLER_REF}"
47else
48 echo "\n✗ Failed to create distribution"
49 echo "Check if caller reference is unique"↗Related Errors
Provider Information
This error code is specific to AWS services. For more information, refer to the official AWS documentation.