0
votes

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"
}
1
Your question is unclear. Are you trying to refer to resources that are created in the same template, or resources that exist outside of your CloudFormation stack?John Rotenstein
I have created a template using cloudformer of the Dev env, and want to make that template useful for creating Prod env and future use. So to make the template generic, I have added parameter, and trying to pass the values to make it useful for any client who wants to have same kind of env.Deepak Kumar Ojha
I am trying to refer to my template resources. I purpose is simple, I have to create a template which can create resources VPC(along with subnet,rt, igw etc), S3, Cloudfron, ASG, ELB and RDS. And want to pass the values as parameters like names , CIDR etc everywhere possible.Deepak Kumar Ojha
Could you please edit your question to include the relevant parts of your CloudFormation template? (Only show the parts that build the CloudFront Distribution and related elements such as the Origin.)John Rotenstein
Please give me your emai id, not able to share the code hereDeepak Kumar Ojha

1 Answers

0
votes

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.