【发布时间】:2019-08-03 09:52:50
【问题描述】:
我相信我在 AWS api 或 aws_cloudfront_distribution 模块(版本 v0.11)中遇到了限制。我想要完成的是动态将源和缓存行为添加到现有的 CloudFront 分配。
我知道这可以通过AWS CLI 以某种方式实现。下面显示了一个示例(来自update-distribution documentation。我的问题与如何使用 terraform 简化此实现有关(显示在代码 sn-p 下方)。
$ JSON_OUTPUT=$(aws cloudfront get-distribution-config --id=<MY-DISTROS-ID>)
$ echo $JSON_OUTPUT
{
"ETag": "E2ABCDEFGHIJKL",
"DistributionConfig": {
"CacheBehaviors": {
"Quantity": 0
},
"Origins": {
"Items": [
{
...origin-info-for-default-cache-behavior
}
],
"Quantity": 1
},
"Enabled": true,
"DefaultCacheBehavior": {
...default-cache-behavior
},
...more-cloudfront-stuff
}
$ # remove the ETag
$ # add an entry to the DistributionConfig.Origins.Items array
$ # add an array of Items to the DistributionConfig.CacheBehaviors.Items = [{ the cache behavior }] and set Quantity = 1
$ # save DistributionConfig key value to a file file:///tmp/new-distro-config.json
$ aws cloudfront update-distribution --id <MY-DISTROS-ID> --distribution-config file:///tmp/new-distro-config.json --if-match E2ABCDEFGHIJKL
这将成功地向现有 CloudFront 发行版添加新的缓存行为和来源。
我想使用 terraform 实现同样的最终目标,以便我可以在 S3 中保持状态。因此,例如,使用 terraform,我会
- 构建云端发行版
- 来自另一个 terraform 模块
- 使用数据块导入云端发行版
- 添加一次性缓存行为和来源
在 pseudocode-terraform 中,为了更清楚起见,下面的代码块进一步说明了这一点
# original CloudFront distro
resource "aws_cloudfront_distribution" "my-distro" {
aliases = ["${var.my_alias_domain}"]
origin {
...origin-info-for-default-cache-behavior
}
enabled = true
default_cache_behavior {
...default-cache-behavior
}
...more-cloudfront-stuff
}
然后在另一个 terraform 模块中(以及将来的某个时间),效果是
data "terraform_remote_state" "CloudFront" {
backend = "s3"
config {
acl = "bucket-owner-full-control"
bucket = "${var.CloudFront_BUCKET}"
key = "${var.CloudFront_KEY}"
region = "${var.CloudFront_REGION}"
}
}
resource "aws_cloudfront_distribution_origin" "next-origin" {
cloudfront_distribution_id = "${data.terraform_remote_state.CloudFront.id}"
origin {
...origin-info-for-default-cache-behavior
}
ordered_cache_behavior {
...origin-info-for-cache-behavior
}
}
我很清楚aws_cloudfront_distribution_origin 不是 terraform 中的资源。我只是在写这个伪代码来传达我想要完成的事情。这是一个实际的用例,用于拆分应该全部位于同一域下的服务。
在写完这一切之后,我假设我应该在这种情况下直接放弃 terraform 来代替 AWS CLI。但是,如果其他人使用 terraform 实现了这一点,我很想听听这是怎么可能的。
【问题讨论】:
标签: amazon-cloudfront terraform terraform-provider-aws