AWS CloudFormation
User Guide (API Version 2010-05-15)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Amazon CloudFront Template Snippets

Amazon CloudFront Distribution Resource with an Amazon S3 Origin

This example shows an Amazon CloudFront Distribution using an S3Origin.

 "myDistribution" : {
   "Type" : "AWS::CloudFront::Distribution",
   "Properties" : {
      "DistributionConfig" : {
          "Origins" : [ {
              "DomainName": "mybucket.s3.amazonaws.com",
              "Id" : "myS3Origin",
              "S3OriginConfig" : {
                  "OriginAccessIdentity" : "origin-access-identity/cloudfront/E127EXAMPLE51Z"
              }
          }],
          "Enabled" : "true",
          "Comment" : "Some comment",
          "DefaultRootObject" : "index.html",
          "Logging" : {
              "Bucket" : "mylogs.s3.amazonaws.com",
              "Prefix" : "myprefix"
          },
          "Aliases" : [ "mysite.example.com", "yoursite.example.com" ],
          "DefaultCacheBehavior" : {
              "TargetOriginId" : "myS3Origin",
              "ForwardedValues" : {
                  "QueryString" : "false"
              },
              "TrustedSigners" : [ "1234567890EX", "1234567891EX" ],
              "ViewerProtocolPolicy" : "allow-all"
          }
      }
   }
 }     

Amazon CloudFront Distribution Resource with Custom Origin

This example shows an Amazon CloudFront Distribution using a CustomOrigin.

"myDistribution": {
    "Type": "AWS: : CloudFront: : Distribution",
    "Properties": {
        "DistributionConfig": {
            "Origins": [
                {
                    "DomainName": "www.example.com",
                    "Id": "myCustomOrigin",
                    "CustomOriginConfig": {
                        "HTTPPort": "80",
                        "HTTPSPort": "443",
                        "OriginProtocolPolicy": "http-only"
                    }
                }
            ],
            "Enabled": "true",
            "Comment": "Somecomment",
            "DefaultRootObject": "index.html",
            "Logging": {
                "Bucket": "mylogs.s3.amazonaws.com",
                "Prefix": "myprefix"
            },
            "Aliases": [
                "mysite.example.com",
                "*.yoursite.example.com"
            ],
            "DefaultCacheBehavior": {
                "TargetOriginId": "myCustomOrigin",
                "ForwardedValues": {
                    "QueryString": "false"
                },
                "TrustedSigners": [
                    "1234567890EX",
                    "1234567891EX"
                ],
                "ViewerProtocolPolicy": "allow-all"
            }
        }
    }
}

Amazon CloudFront Distribution with Multi-origin Support.

This template snippet shows how to declare a CloudFront Distribution with multi-origin support. In the DistributionConfig, a list of origins is provided and a DefaultCacheBehavior is set.

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Resources" : {
        "myDistribution" : {
            "Type" : "AWS::CloudFront::Distribution",
            "Properties" : {
               "DistributionConfig" : {
                   "Origins" : [ {
                           "Id" : "myS3Origin",
                           "DomainName" : "mybucket.s3.amazonaws.com",
                           "S3OriginConfig" : {
                               "OriginAccessIdentity" : "origin-access-identity/cloudfront/E127EXAMPLE51Z"
                           }
                       }, 
                       {
                           "Id" : "myCustomOrigin",
                           "DomainName" : "www.example.com",
                           "CustomOriginConfig" : {
                               "HTTPPort" : "80",
                               "HTTPSPort" : "443",
                               "OriginProtocolPolicy" : "http-only"
                           }
                       }
                   ],
                   "Enabled" : "true",
                   "Comment" : "Some comment",
                   "DefaultRootObject" : "index.html", 
                   "Logging" : {
                       "Bucket" : "mylogs.s3.amazonaws.com",
                       "Prefix" : "myprefix"
                   },            
                   "Aliases" : [ "mysite.example.com", "yoursite.example.com" ],
                   "DefaultCacheBehavior" : {
                       "TargetOriginId" : "myS3Origin",
                       "ForwardedValues" : {
                           "QueryString" : "false"
                        },
                       "TrustedSigners" : [ "1234567890EX", "1234567891EX"  ],
                       "ViewerProtocolPolicy" : "allow-all",
                       "MinTTL" : "100"
                   },
                   "CacheBehaviors" : [ {
                            "TargetOriginId" : "myS3Origin",
                            "ForwardedValues" : {
                                "QueryString" : "true"
                            },
                            "TrustedSigners" : [ "1234567890EX", "1234567891EX" ],
                            "ViewerProtocolPolicy" : "allow-all",
                            "MinTTL" : "50",
                            "PathPattern" : "images1/*.jpg"
                        }, 
                        {
                            "TargetOriginId" : "myCustomOrigin",
                            "ForwardedValues" : {
                                "QueryString" : "true"
                            },
                            "TrustedSigners" : [ "1234567890EX", "1234567891EX"  ],
                            "ViewerProtocolPolicy" : "allow-all",
                            "MinTTL" : "50",
                            "PathPattern" : "images2/*.jpg"
                        }
                   ]
                }
            }
        }
    }
}