Terraform can’t create a CloudFront’s origin with a static S3 website endpoint

Question:

I have a plan which uses two modules: bucket-website and cloudfront-website

Among other things (policies and such) inside the bucket module, there is the following resource for creating the bucket and serve it as a website:

Also this module has the following output:

The cloudfront-website module has a resource with all those cloudfront properties (IPs, cache stuff, etc), but the relevant part is:

The call to the cloudfront module in the plan passes the following parameter:

I can confirm that the value is correct, because in the log of terraform apply is can see:

Which is the same endpoint I would use if I was doing this setup using just the AWS Console, i.e. get the bucket’s static web endpoint (different to the standard bucket endpoint) and use it as the origin of Cloudfront.

However, for some reason Terraform is complaining about the domain name:

And I’m already out of ideas. Everything looks good. The endpoint is correct. I have checked other examples and they also use ${aws_s3_bucket.<BUCKET_RESOURCE_NAME>.website_endpoint}, so I honestly don’t understand what’s wrong.

Answer:

Just found the solution. When serving a S3 website through CloudFront, the following code must be added to the origin section, even though it’s not specified elsewhere to do so.

Leave a Reply