Cloudfront Cloudformation

问题描述 投票:0回答:1

我们可以选择在使用Fn :: GetAtt函数创建CloudFront分配时,在cloudformation模板中获取DomainName的值。但我无法找到我们如何动态获取Origin的Id和DefaultCacheBehaviour的TargetOriginId?

我可以将Ref用于我的S3和ELB吗?

这是我的代码,我也使用了一些参数并更改了Cloudfront代码。请检查一次是否正确。

它给我一个错误,称为“属性验证失败:[在{/ 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"
}
amazon-web-services amazon-cloudformation amazon-cloudfront
1个回答
0
投票

DistributionConfig / Origins / ID字段应该只是一个文本名称,它不需要引用任何东西。

即。将DistributionConfig / Origins / ID设置为字符串,例如'MyOriginBucket'

然后你的CacheBehaviour TargetOriginId也是一个设置为'MyOriginBucket'的字符串

您的新存储桶所需的唯一Ref是在Origins / DomainName中。

TargetOriginId的目的是指向您在Origins列表中指定的原始ID,而不是指向存储桶名称。

© www.soinside.com 2019 - 2024. All rights reserved.