ust*_*erk 4 amazon-web-services amazon-cloudfront aws-cli
我想使用 awscli 编辑/更新我的 CloudFront 分配。
我正在使用最新的 cli 版本:
aws-cli/1.11.56 Python/2.7.10 Darwin/16.4.0 botocore/1.5.19
要在 awscli 中使用 cloudfront 功能,您需要将其添加到 aws 配置文件中:
[preview]
cloudfront = true
Run Code Online (Sandbox Code Playgroud)
我正在获取要修改的发行版的配置:
aws cloudfront get-distribution-config --id FOO_BAR_ID > cf_config.json
看起来它按预期工作。配置对我来说没问题。现在我正在尝试使用相同的配置重新配置我的 CF 发行版。
aws cloudfront update-distribution --distribution-config file://cf_config.json --id FOO_BAR_ID
我得到:
Parameter validation failed:
Missing required parameter in DistributionConfig: "CallerReference"
Missing required parameter in DistributionConfig: "Origins"
Missing required parameter in DistributionConfig: "DefaultCacheBehavior"
Missing required parameter in DistributionConfig: "Comment"
Missing required parameter in DistributionConfig: "Enabled"
Unknown parameter in DistributionConfig: "ETag", must be one of: CallerReference, Aliases, DefaultRootObject, Origins, DefaultCacheBehavior, CacheBehaviors, CustomErrorResponses, Comment, Logging, PriceClass, Enabled, ViewerCertificate, Restrictions, WebACLId, HttpVersion, IsIPV6Enabled
Unknown parameter in DistributionConfig: "DistributionConfig", must be one of: CallerReference, Aliases, DefaultRootObject, Origins, DefaultCacheBehavior, CacheBehaviors, CustomErrorResponses, Comment, Logging, PriceClass, Enabled, ViewerCertificate, Restrictions, WebACLId, HttpVersion, IsIPV6Enabled
Run Code Online (Sandbox Code Playgroud)
使用 awscli 重新配置 CF 的正确方法是什么?
@usterk 的回答是正确的,但我又花了 3 个小时才找到我需要的脚本。在这里,我分享一下。
我在 S3 中托管一个静态网站 (SSG),我希望它由 CloudFront 提供服务。该网站的代码(不仅仅是内容)经常更新,我想将网站的所有版本存储在 S3 中(就像所有工件或 docker 图像一样)并更新 CloudFront 以指向新版本,对吧在新版本推送到 S3 之后。
我知道 S3 中存在“文件版本控制”,但这种保留所有资产版本的老式格式有助于分析资产以及轻松回滚。
s3://<mybucket-name>/artifacts/<version-id>www网站。Route53 forwww.domain.com指向它。/api到 ELB 的路径。)wwwOriginPath/artifacts/<version-id>作为@usterk和@BrianLeishman指出,这份工作唯一的CLI命令是update-distribution其中每个文档,需要整个配置分布,以替换它。因此,没有命令可以部分更新配置中的一个字段。
要实现这一点,必须首先获取当前的 distribution-config,然后提取“DistributionConfig”组件,然后更新它所需要的字段,最后,将其放回正确的格式并使用正确的验证令牌。
请注意,“更新”命令需要的是“获取”返回的“子集”。所以解析 JSON viajq是不可避免的。
我想出的以下脚本为我完成了这项工作:
# 0) You need to set the followings for your case
CLOUDFRONT_DISTRIBUTION_ID="EABCDEF12345ABCD"
NEW_ORIGIN_PATH="/art/0.0.9"
CLOUDFRONT_ORIGIN_ID="E1A2B3C4D5E6F"
DIST_CONFIG_OLD_FILENAME="dist-config.json" # a temp file, which will be removed later
DIST_CONFIG_NEW_FILENAME="dist-config2.json" # a temp file, which will be removed later
# 1) Get the current config, entirely, and put it in a file
aws cloudfront get-distribution --id $CLOUDFRONT_DISTRIBUTION_ID > $DIST_CONFIG_OLD_FILENAME
# 2) Extract the Etag which we need this later for update
Etag=`cat $DIST_CONFIG_OLD_FILENAME | jq '.ETag' | tr -d \"`
# 3) Modify the config as wished, for me I used `jq` extensively to update the "OriginPath" of the desired "originId"
cat $DIST_CONFIG_OLD_FILENAME | jq \
--arg targetOriginId $CLOUDFRONT_ORIGIN_ID \
--arg newOriginPath $NEW_ORIGIN_PATH \
'.Distribution.DistributionConfig | .Origins.Items = (.Origins.Items | map(if (.Id == $targetOriginId) then (.OriginPath = $newOriginPath) else . end))' \
> $DIST_CONFIG_NEW_FILENAME
# 4) Update the distribution with the new file
aws cloudfront update-distribution --id $CLOUDFRONT_DISTRIBUTION_ID \
--distribution-config "file://${DIST_CONFIG_NEW_FILENAME}" \
--if-match $Etag \
> /dev/null
# 5) Invalidate the distribution to pick up the changes
aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_DISTRIBUTION_ID --paths "/*"
# 6) Clean up
rm -f $DIST_CONFIG_OLD_FILENAME $DIST_CONFIG_NEW_FILENAME
Run Code Online (Sandbox Code Playgroud)
执行这些操作的用户需要对 CloudFront 中分配的 Get、Invalidate 和 Update 操作进行 IAM 访问。这是政策规定:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"cloudfront:GetDistribution",
"cloudfront:UpdateDistribution",
"cloudfront:CreateInvalidation"
],
"Resource": "arn:aws:cloudfront::<ACCOUNT_ID>:distribution/<DISTRIBUTION_ID>
}
]
}
Run Code Online (Sandbox Code Playgroud)
您必须在使用之前编辑 cf_config.jsonupdate-distribution并删除
{
"ETag": "ETag_Value",
"DistributionConfig":
Run Code Online (Sandbox Code Playgroud)
从文件的开头到最后
}
Run Code Online (Sandbox Code Playgroud)
从文件末尾。
然后使用此命令与已从中删除的权限id和ETag值cf_config.json
aws cloudfront update-distribution --distribution-config file://cf_config.json --id FOO_BAR_ID --if-match ETag_Value
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3529 次 |
| 最近记录: |