gpt4 book ai didi

python - 使用 S3 网站源的 Cloudfront 对流层模板

转载 作者:行者123 更新时间:2023-12-01 09:33:05 26 4
gpt4 key购买 nike

我正在尝试自动化静态网站的部署。我有一个位于存储桶中的静态网站 (angular/javascript/html),需要使用 AWS Cloudfront CDN。

   s3dnsname = self.template.add_parameter(Parameter(
"S3DNSName",
Description="The DNS name of an existing S3 bucket to use as the "
"Cloudfront distribution origin",
Type="String",
))

myDistribution = self.template.add_resource(Distribution(
"myDistribution",
DistributionConfig=DistributionConfig(
Origins=[Origin(Id="Origin 1", DomainName=Ref(s3dnsname),
S3OriginConfig=S3Origin())],
DefaultCacheBehavior=DefaultCacheBehavior(
TargetOriginId="Origin 1",
ForwardedValues=ForwardedValues(
QueryString=False
),
ViewerProtocolPolicy="allow-all"),
Enabled=True,
HttpVersion='http2'

)
))

这是我的代码,我收到错误“参数 Origin DomainName 未引用有效的 S3 存储桶。”

我研究了几篇关于此的文章/博客,它们都指出添加以下内容:

custom_origin_config { 
origin_protocol_policy = "http-only"
http_port = 80
https_port = 443
origin_ssl_protocols = ["TLSv1.2", "TLSv1.1", "TLSv1"]
}

任何人都可以指出我需要在哪里添加此部分吗?

更新:我尝试将 S3OriginConfig 替换为 CustomOriginConfig,如下所示:

   myDistribution = self.template.add_resource(Distribution(
"myDistribution",
DistributionConfig=DistributionConfig(
Origins=[Origin(
Id="Origin 1",
DomainName=Ref(s3dnsname),
CustomOriginConfig=CustomOriginConfig(
OriginProtocolPolicy="http-only",
HTTPPort=80,
HTTPSPort=443))],
DefaultCacheBehavior=DefaultCacheBehavior(
TargetOriginId="Origin 1",
ForwardedValues=ForwardedValues(
QueryString=False
),
ViewerProtocolPolicy="allow-all"),
Enabled=True,
HttpVersion='http2'
)
))

运行此命令后,我收到错误“NameError:全局名称'CustomOriginConfig'未定义”

我在这里遗漏了一些我无法识别的东西。

最佳答案

这是对我有用的解决方案:

from troposphere.cloudfront import S3Origin, CustomOrigin

并将代码更改为:

Origins=[Origin(Id="Origin 1", DomainName=Ref(s3dnsname),
CustomOriginConfig=CustomOrigin(
HTTPPort=80,
HTTPSPort=443,
OriginProtocolPolicy="http-only"
))],

我正在使用这个例子:https://github.com/cloudtools/troposphere/blob/master/examples/CloudFront_S3.py

希望这可以帮助任何面临同样问题的人。

关于python - 使用 S3 网站源的 Cloudfront 对流层模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49784028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com