Create Origin Group

In this section, you add a second S3 bucket in another Region and combine it with the existing bucket in an origin group. When the primary origin returns one of the failover status codes, CloudFront retries the request on the secondary origin.

Create the secondary S3 bucket

  1. Open the Amazon S3 console, switch to a Region different from the primary bucket (for example, US East (N. Virginia) us-east-1), and choose Create bucket.

CloudFront Lab

  1. Configure the bucket:
    • Bucket type: General purpose
    • Bucket namespace: Global namespace
    • Bucket name: s3bucket-secondary. Bucket names must be globally unique, so add a suffix if the name is taken.

CloudFront Lab

  1. Keep the other settings at their defaults, including Block all public access, and choose Create bucket.

CloudFront Lab

  1. Create a file named secondary-index.html with the content below and save it with UTF-8 encoding.
<html lang="en">
  <body>
    <h1>CloudFront Lab</h1>
    <strong>Hi, this is a page from my secondary Origin! We now support Origin group and failover!</strong>
  </body>
</html>
  1. Upload secondary-index.html to the new bucket. Upload it only to the secondary bucket, not to the primary bucket.

CloudFront Lab

Add the secondary bucket as an origin

  1. Open the CloudFront console, select your distribution, open the Origins tab, and choose Create origin.

CloudFront Lab

  1. Configure the origin:
    • Origin domain: select the secondary bucket
    • Origin access: Origin access control settings (recommended)
    • Origin access control: select the existing OAC that the primary origin uses.
    • Choose Create origin.

CloudFront Lab

  1. The message Successfully created origin appears, and the Origins tab lists 3 origins.

CloudFront Lab

Allow CloudFront to read the secondary bucket

The secondary bucket is private. Unlike the distribution wizard in section 3, the Create origin page doesn’t update the bucket policy for you. Without this policy, S3 returns 403 Access Denied to CloudFront, and CloudFront serves the custom error page from section 5.

  1. Open the Amazon S3 console, select the secondary bucket, open the Permissions tab, and choose Edit under Bucket policy. CloudFront Lab

  2. Paste the policy you copied and choose Save changes. The policy should look like the one below, with your bucket name, account ID, and distribution ID:

{
  "Version": "2008-10-17",
  "Id": "PolicyForCloudFrontPrivateContent",
  "Statement": [
    {
      "Sid": "AllowCloudFrontServicePrincipal",
      "Effect": "Allow",
      "Principal": {
        "Service": "cloudfront.amazonaws.com"
      },
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::<secondary-bucket-name>/*",
      "Condition": {
        "StringEquals": {
          "AWS:SourceArn": "arn:aws:cloudfront::<account-id>:distribution/<distribution-id>"
        }
      }
    }
  ]
}

Create the origin group

  1. In the CloudFront console, open the Origins tab of your distribution and choose Create origin group.

CloudFront Lab

  1. Configure the origin group:

    • Origins: add the primary, then the secondary bucket. Use the arrows to keep the primary in position 1.
    • Name: OriginGroup-CloudFrontLab
    • Failover criteria: 403 Forbidden and 404 Not Found
    • Choose Create origin group.

    S3 returns 403 instead of 404 for a missing object when CloudFront doesn’t have s3:ListBucket permission, so 403 is the code that triggers failover in this lab.

CloudFront Lab

  1. The message Successfully created origin group appears, and the group shows failover criteria 403, 404.

CloudFront Lab

Use the origin group in the default behavior

  1. Open the Behaviors tab, select Default (*), and choose Edit.

CloudFront Lab

  1. Select OriginGroup-CloudFrontLab, keep the other settings, and choose Save changes.

CloudFront Lab

  1. The message The default cache behavior was updated appears. Default (*) now uses OriginGroup-CloudFrontLab, and /api* still uses the EC2 origin.

CloudFront Lab

Test failover

  1. Wait until the distribution’s Last modified column shows a date instead of Deploying.

  2. Open https://<your-distribution-domain>/secondary-index.html in the browser.

    • The primary bucket doesn’t have this file and returns 403.
    • CloudFront retries the request on the secondary bucket, which returns the file. CloudFront Lab