heiopei
heiopei

Reputation: 71

How to use the the newly introduced aws_cloudfront_cache_policy resource in terraform

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?

Upvotes: 7

Views: 4436

Answers (2)

jebeaudet
jebeaudet

Reputation: 1613

If you want to use one of the AWS managed policies, namely Amplify, CachingDisabled, CachingOptimized, CachingOptimizedForUncompressedObjects or Elemental-MediaPackage, they all have a hardcoded ID which can be found here. This ID can then be used in your terraform :

resource "aws_cloudfront_distribution" "cfd" {
    default_cache_behavior {
        cache_policy_id = "658327ea-f89d-4fab-a63d-7e88639e58f6"
    }
}

For reference, here are the IDs :

Amplify : 2e54312d-136d-493c-8eb9-b001f22f67d2

CachingDisabled : 4135ea2d-6df8-44a3-9df3-4b5a84be39ad

CachingOptimized : 658327ea-f89d-4fab-a63d-7e88639e58f6

CachingOptimizedForUncompressedObjects: b2884449-e4de-46a7-ac36-70bc7f1ddd6d

Elemental-MediaPackage: 08627262-05a9-4f76-9ded-b50ca2e3a84f

Upvotes: 3

uni
uni

Reputation: 300

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
    }
}

Upvotes: 10

Related Questions