Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CloudFormation to configure CloudFront with an S3 origin

I am trying to use CloudFormation for the first time to configure a CloudFront distribution that uses an S3 bucket as its origin.

However I am receiving the error One or more of your origins do not exist when the template is run. I have assumed it is down to the origin DomainName being configured incorrectly, however have not been able to find a configuration that works.

I currently have the following template:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "AssetBucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "BucketName": "cdn-assets",
        "AccessControl": "PublicRead",
        "CorsConfiguration": {
          "CorsRules": [
            {
              "AllowedHeaders": [
                "*"
              ],
              "AllowedMethods": [
                "GET"
              ],
              "AllowedOrigins": [
                "*"
              ],
              "Id": "OpenCors",
              "MaxAge": "3600"
            }
          ]
        }
      }
    },
    "AssetCDN": {
      "Type": "AWS::CloudFront::Distribution",
      "Properties": {
        "DistributionConfig": {
          "Origins": [
            {
              "DomainName": {
                "Fn::GetAtt": [
                              "AssetBucket",
                              "DomainName"
                          ]
              },
              "Id": "AssetBucketOrigin",
              "S3OriginConfig": {}
            }
          ],
          "Enabled": "true",
          "DefaultCacheBehavior": {
            "Compress": true,
            "AllowedMethods": [
              "GET",
              "HEAD",
              "OPTIONS"
            ],
            "TargetOriginId": "origin-access-identity/cloudfront/AssetCDN",
            "ForwardedValues": {
              "QueryString": "false",
              "Cookies": {
                "Forward": "none"
              }
            },
            "ViewerProtocolPolicy": "allow-all"
          },
          "PriceClass": "PriceClass_All",
          "ViewerCertificate": {
            "CloudFrontDefaultCertificate": "true"
          }
        }
      },
      "DependsOn": [
        "AssetBucket"
      ]
    }
  }
}

I have not been able to find much advice on this, so hoping someone can point me in the right direction.

like image 297
George Thomas Avatar asked Mar 07 '16 18:03

George Thomas


People also ask

How do I add origin to CloudFront?

An origin is the location where content is stored, and from which CloudFront gets content to serve to viewers. To specify an origin: Use S3OriginConfig to specify an Amazon S3 bucket that is not configured with static website hosting.

Can CloudFront have multiple S3 origins?

You can configure a single CloudFront web distribution to serve different types of requests from multiple origins. For example, your website might serve static content from an Amazon Simple Storage Service (Amazon S3) bucket and dynamic content from a load balancer.


2 Answers

Your Cache Behavior's TargetOriginId property must match the value specified in the S3 Origin's Id property.

In your above example, TargetOriginId is origin-access-identity/cloudfront/AssetCDN while Id is AssetBucketOrigin, which is causing the error.

like image 57
wjordan Avatar answered Sep 17 '22 12:09

wjordan


The real issue here is that Cloudfront have a dependency - S3 bucket. And so you should put this reference inside cloudfront object to let CFN know that first of all it should create S3 bucket. To do this you have to change your Origins.Id and DefaultCacheBehavior.TargetOriginId properties to Ref to your bucket config:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "AssetBucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "BucketName": "cdn-assets",
        "AccessControl": "PublicRead",
        "CorsConfiguration": {
          "CorsRules": [
            {
              "AllowedHeaders": [
                "*"
              ],
              "AllowedMethods": [
                "GET"
              ],
              "AllowedOrigins": [
                "*"
              ],
              "Id": "OpenCors",
              "MaxAge": "3600"
            }
          ]
        }
      }
    },
    "AssetCDN": {
      "Type": "AWS::CloudFront::Distribution",
      "Properties": {
        "DistributionConfig": {
          "Origins": [
            {
              "DomainName": {
                "Fn::GetAtt": [
                              "AssetBucket",
                              "DomainName"
                          ]
              },
              "Id": { "Ref": "AssetBucket" }, /// HERE!!!!
              "S3OriginConfig": {}
            }
          ],
          "Enabled": "true",
          "DefaultCacheBehavior": {
            "Compress": true,
            "AllowedMethods": [
              "GET",
              "HEAD",
              "OPTIONS"
            ],
            "TargetOriginId": { "Ref": "AssetBucket" }, /// HERE!!!!
            "ForwardedValues": {
              "QueryString": "false",
              "Cookies": {
                "Forward": "none"
              }
            },
            "ViewerProtocolPolicy": "allow-all"
          },
          "PriceClass": "PriceClass_All",
          "ViewerCertificate": {
            "CloudFrontDefaultCertificate": "true"
          }
        }
      },
      "DependsOn": [
        "AssetBucket"
      ]
    }
  }
}
like image 24
Ololo Avatar answered Sep 17 '22 12:09

Ololo