【问题标题】:Cloudfront CloudFormationCloudfront CloudFormation
【发布时间】:2019-05-03 17:00:09
【问题描述】:

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

我可以只对我的 S3 和 ELB 使用 Ref 吗?

这是我的代码,我也使用了一些参数并更改了 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"
}

【问题讨论】:

  • 您的问题不清楚。您是在尝试引用在同一模板中创建的资源,还是在 CloudFormation 堆栈之外存在的资源?
  • 我已经使用 Dev env 的 cloudformer 创建了一个模板,并希望使该模板对创建 Prod env 和未来使用有用。因此,为了使模板通用,我添加了参数,并尝试传递值以使其对任何想要拥有相同类型环境的客户有用。
  • 我正在尝试引用我的模板资源。我的目的很简单,我必须创建一个模板来创建资源 VPC(以及子网、rt、igw 等)、S3、Cloudfron、ASG、ELB 和 RDS。并希望尽可能地将值作为参数传递,例如名称、CIDR 等。
  • 能否编辑您的问题以包含 CloudFormation 模板的相关部分? (仅显示构建 CloudFront Distribution 的部分和相关元素,例如 Origin。)
  • 请给我你的email id,这里不能分享代码

标签: amazon-web-services amazon-cloudformation amazon-cloudfront


【解决方案1】:

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

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

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

新存储桶所需的唯一 Ref 位于 Origins/DomainName 中。

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

【讨论】:

    猜你喜欢
    • 2016-06-21
    • 2019-05-21
    • 2019-07-05
    • 1970-01-01
    • 2020-10-27
    • 2018-03-10
    • 2018-07-29
    • 2016-06-21
    • 2020-12-04
    相关资源
    最近更新 更多