6

terraform recently implemented the aws_cloudfront_cache_policy resource and data source (beginning from aws provider verion 0.3.28 IIRC).
It finally enables Brotli compression, and this is why I need to use it, but I am unsure about how to integrate it into the existing terraform codebase, also because I am not exactly sure about the relationship between aws_cloudfront_cache_policy and ordered_cache_behavior.

  1. How to use the aws_cloudfront_cache_policy - can I just put it inside my aws_cloudfront_distribution resource?
  2. What is the difference/relation betwwen the cache policy and the ordered_cache_behavior?
heiopei
  • 61
  • 1
  • 2

1 Answers1

9

this is how i used aws_cloudfront_cache_policy :

data "aws_cloudfront_cache_policy" "cache_policy" {
    name = var.cache_policy_name
}

here, cache_policy_name = Managed-CachingOptimized

resource "aws_cloudfront_distribution" "cfd"{
    default_cache_behavior {
        cache_policy_id = data.aws_cloudfront_cache_policy.cache_policy.id
    }
}
Flair
  • 2,609
  • 1
  • 29
  • 41
uni
  • 170
  • 2
  • 11
  • Hi there, I've tried pointing to the data by name and also by id and the output from that data is returning an object with every attribute marked as null. – Pedro Bezanilla Sep 17 '21 at 14:07
  • Ok, I've passed it by name instead of by ID and it's working... I've done that other day and it wasn't... maybe I misspelled or something, who knows, I'll vote you, thanks! – Pedro Bezanilla Sep 17 '21 at 14:34
  • @PedroBezanilla not sure if it's because the implementation changed but using Terraform 1.4 and I have to use the id and not the name for it to work – Djidiouf Mar 17 '23 at 00:25
  • @Djidiouf well, dunno what to say but trying to update TF. TF had several breaking changes since middle of 2021 so that can be the reason. There's a migration guide too though. – Pedro Bezanilla Mar 17 '23 at 13:18