Deepak Kumar Ojha
Deepak Kumar Ojha

Reputation: 31

Cloudfront CloudFormation

We have option to get the value of DomainName in cloudformation template while creating a CloudFront Distribution using Fn::GetAtt function. But I could not find anywhere that how we get Origin's Id and DefaultCacheBehaviour's TargetOriginId dynamically?

Can I just use Ref to my S3 and ELB?

This is my code, I have used some parameters also and changed the Cloudfront code as well. Please check it once whether it is correct or not.

And it is throwing me an error called "Property validation failure: [Encountered unsupported properties in {/DistributionConfig/Origins/1/S3OriginConfig}: [HTTPSPort, HTTPPort, OriginProtocolPolicy]]"

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
        "ClientName": {
            "Type": "String",
            "Description": "Name of the Client"
        },
        "EnvName": {
            "Type": "String",
            "Description": "Name of the Environment"
        }
    },
    "Resources": {
        "distd2v0l803ay8odocloudfrontnet": {
            "Type": "AWS::CloudFront::Distribution",
            "Properties": {
                "DistributionConfig": {
                    "Enabled": true,
                    "DefaultRootObject": "index.html",
                    "PriceClass": "PriceClass_All",
                    "CacheBehaviors": [
                        {
                            "TargetOriginId": {
                                "Ref": "elbhtlbetaelb"
                            },
                            "PathPattern": "/app*",
                            "ViewerProtocolPolicy": "allow-all",
                            "MinTTL": 0,
                            "AllowedMethods": [
                                "HEAD",
                                "DELETE",
                                "POST",
                                "GET",
                                "OPTIONS",
                                "PUT",
                                "PATCH"
                            ],
                            "CachedMethods": [
                                "HEAD",
                                "GET"
                            ],
                            "ForwardedValues": {
                                "QueryString": true,
                                "Cookies": {
                                    "Forward": "all"
                                }
                            }
                        },
                        {
                            "TargetOriginId": {
                                "Ref": "elbhtlbetaelb"
                            },
                            "PathPattern": "/api*",
                            "ViewerProtocolPolicy": "allow-all",
                            "MinTTL": 0,
                            "AllowedMethods": [
                                "HEAD",
                                "DELETE",
                                "POST",
                                "GET",
                                "OPTIONS",
                                "PUT",
                                "PATCH"
                            ],
                            "CachedMethods": [
                                "HEAD",
                                "GET"
                            ],
                            "ForwardedValues": {
                                "QueryString": true,
                                "Cookies": {
                                    "Forward": "all"
                                }
                            }
                        }
                    ],
                    "DefaultCacheBehavior": {
                        "TargetOriginId": {
                            "Ref": "s3htlbeta"
                        },
                        "ViewerProtocolPolicy": "allow-all",
                        "MinTTL": 0,
                        "AllowedMethods": [
                            "HEAD",
                            "DELETE",
                            "POST",
                            "GET",
                            "OPTIONS",
                            "PUT",
                            "PATCH"
                        ],
                        "CachedMethods": [
                            "HEAD",
                            "GET"
                        ],
                        "ForwardedValues": {
                            "Cookies": {
                                "Forward": "none"
                            }
                        }
                    },
                    "Origins": [
                        {
                            "DomainName": {
                                "Fn::GetAtt": [
                                    "s3htlbeta",
                                    "DomainName"
                                ]
                            },
                            "Id": {
                                "Ref": "s3htlbeta"
                            },
                            "S3OriginConfig": {
                                "OriginAccessIdentity": "origin-access-identity/cloudfront/EYD1QGO9CUDA2"
                            }
                        },
                        {
                            "DomainName": {
                                "Fn::GetAtt": [
                                    "elbhtlbetaelb",
                                    "DNSName"
                                ]
                            },
                            "Id": {
                                "Ref": "elbhtlbetaelb"
                            },
                            "S3OriginConfig": {
                                "HTTPPort": "80",
                                "HTTPSPort": "443",
                                "OriginProtocolPolicy": "http-only"
                            }
                        }
                    ],
                    "Restrictions": {
                        "GeoRestriction": {
                            "RestrictionType": "none",
                            "Locations": []
                        }
                    },
                    "ViewerCertificate": {
                        "CloudFrontDefaultCertificate": "true",
                        "MinimumProtocolVersion": "TLSv1"
                    }
                }
            }
        },
        "s3htlbeta": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "AccessControl": "Private",
                "VersioningConfiguration": {
                    "Status": "Suspended"
                }
            }
        }
    },
    "Description": "xxx-beta cloudformation template"
}

Upvotes: 0

Views: 1752

Answers (1)

Mariusz
Mariusz

Reputation: 236

The DistributionConfig/Origins/ID field should just be a text name, it doesn't need to reference anything.

ie. Set DistributionConfig/Origins/ID to a string e.g. 'MyOriginBucket'

Then your CacheBehaviour TargetOriginId is also a string set to 'MyOriginBucket'

The only Ref required to your new bucket is in Origins/DomainName.

The purpose of the TargetOriginId is to point to the origin ID that you specified in the list of Origins, not point to the bucket name.

Upvotes: 0

Related Questions