I have an application that needs CloudFront + s3. I want to create a CloudFront module and make it reusable for others applications.
My problem is that I have one application that needs two Origins ( s3_origin_config and a custom_origin_config) AND other application that needs ONLY s3_origin_config.
The question is: How can I have a module that handles application that needs two Origins and another that needs only one Origin.
Below an example of my code:
resource "aws_cloudfront_distribution" "s3_distribution" {
origin {
domain_name = "${var.domain_name}"
origin_id = "${var.app_name}-${var.environment}"
origin_path = "/${var.environment}/${var.setup}/public"
s3_origin_config {
origin_access_identity = ${aws_cloudfront_origin_access_identity.origin_access_identity.cloudfront_access_identity_path}"
}
}
#HERE, I would like something like if $second_origin, then:
#I know there is no if in terraform, just to have an example ;)
origin {
domain_name = "${var.second_origin_domain_name}"
origin_id = "Custom-${var.second_origin_domain_name}"
custom_origin_config {
http_port = "80"
https_port = "443"
origin_protocol_policy = "https-only"
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
}
custom_header {
name = "${var.second_origin_header_name}"
value = "${var.second_origin_header_key}"
}
}...
Thanks!
You need to create two cloudfront resources with different origin requirements. Using
count
, you can enable one resource and disable another resource at same time. This will allow you to use same module for different origin requirements.Here is simple example.