Search code examples
amazon-ec2terraformamazon-cloudfront

Configure Resource "aws_cloud_distribution" with ec2 as the origin with Terraform


I am setting up a "aws_cloud_distribution" with Terraform and attempting to set an ec2 as my origin.

In my module I have:

origin {
    domain_name = var.domain_name
    origin_id = var.origin_id
  }

In the main file I call this module and use the output of the ec2 public dns.

module "cloudfront" {
  source = "./modules/cloudfront"

  domain_name = module.ec2.ec2_public_dns

  origin_id = "myid"

  target_origin_id = "myid"
}

When I run plan, I have no issues. However when I run apply and begin the build process I get the following error:

error creating CloudFront Distribution: InvalidArgument: The parameter Origin DomainName does not refer to a valid S3 bucket. status code: 400

I am using terraform 0.13.6 out of some company restrictions to other infra in the company. Is this a Terraform version issue or am I missing something in my configuration steps?


Solution

  • So, I figured out this issue by adding the custom_origin_config argument within the origin argument. The solution looks like the following:

    origin {
        domain_name = var.domain_name
        origin_id = var.origin_id
        custom_origin_config {
          http_port = 80
          https_port = 443
          origin_protocol_policy = "match-viewer"
          origin_ssl_protocols = ["TLSv1"]
        }
      }
    

    Terraform defaults to S3 origin if you don't define the custom_origin_config argument. The AWS plugin for Terraform is searching for an s3 bucket and not an AWS FQDN to resolve.