aws_s3.tf
aws_cloudfront.tf
root.tf
variables.tf
output.tf

aws_s3.tf :

# s3 bucket for terraform state files resource "aws_s3_bucket" "com_programmez_terraform" {     bucket = "${var.aws_s3_bucket_terraform}"     acl    = "private"      versioning {         enabled = true     }      tags {         Tool    = "${var.tags-tool}"         Contact = "${var.tags-contact}"     } }  # s3 bucket for front logs resource "aws_s3_bucket" "front_logs" {   bucket = "${terraform.workspace == "preprod" ? var.bucket_demo_logs_preprod : var.bucket_demo_logs}"   acl    = "log-delivery-write"    tags {     Tool    = "${var.tags-tool}"     Contact = "${var.tags-contact}"   } }  # s3 bucket reached by cloudfront resource "aws_s3_bucket" "front_bucket" {   bucket = "${terraform.workspace == "preprod" ? var.bucket_demo_preprod : var.bucket_demo}"   acl    = "private"    force_destroy = false    depends_on = ["aws_s3_bucket.front_bucket-logs"]    versioning {     enabled = true   }    logging {     target_bucket = "${aws_s3_bucket.front_bucket-logs.bucket}"     target_prefix = "root/"   }    website {     index_document = "index.html"   }    tags {     Tool    = "${var.tags-tool}"     Contact = "${var.tags-contact}"   }    cors_rule {     allowed_headers = ["*"]     allowed_methods = ["GET", "PUT", "POST", "DELETE"]     allowed_origins = ["*"]   }    policy = <<POLICY { "Version": "2012-10-17", "Statement": [ {   "Sid": "PublicReadGetObject",   "Effect": "Allow",   "Principal": "*",   "Action": "s3:GetObject",   "Resource": "arn:aws:s3:::${terraform.workspace == "preprod" ? var.bucket_demo_preprod : var.bucket_demo}/*" } ] } POLICY }

aws_cloudfront.tf :

resource "aws_cloudfront_distribution" "front_cdn" {    # Use All Edge Locations (Best Performance)   price_class  = "PriceClass_All"   http_version = "http2"    "origin" {     origin_id   = "origin-bucket-${aws_s3_bucket.front_bucket.id}"     domain_name = "${aws_s3_bucket.front_bucket.website_endpoint}"     origin_path = "/root"      custom_origin_config {       origin_protocol_policy = "http-only"       http_port              = "80"       https_port             = "443"       origin_ssl_protocols   = ["TLSv1"]     }   }    enabled             = true   is_ipv6_enabled     = true   default_root_object = "index.html"    logging_config {     include_cookies = true     bucket          = "${aws_s3_bucket.front_bucket-logs.bucket}.s3.amazonaws.com"     prefix          = "cloudfront/"   }     "default_cache_behavior" {     allowed_methods = ["GET", "HEAD", "DELETE", "OPTIONS", "PATCH", "POST", "PUT"]     cached_methods  = ["GET", "HEAD"]      "forwarded_values" {       query_string = false        cookies {         forward = "none"       }     }      min_ttl          = "21600"     default_ttl      = "86400"     max_ttl          = "31536000"     target_origin_id = "origin-bucket-${aws_s3_bucket.front_bucket.id}"     // This redirects any HTTP request to HTTPS. Security first!     viewer_protocol_policy = "redirect-to-https"     compress               = true   }   "restrictions" {     "geo_restriction" {       restriction_type = "none"     }   }   # Pre-requisits: Put a SSL cert on AWS store in us-east-1 region   # Generate a csr in loacalhost, make requst to IT, get the returned cert   # put the cert + intermediate + private key in AWS (click in import button)   "viewer_certificate" {     # default certificate if you don't already added one in AWS certificate manager     cloudfront_default_certificate = true     minimum_protocol_version = "TLSv1.1_2016"   }   aliases = ["${var.demo_domain_name}"]    depends_on = ["aws_s3_bucket.front_bucket"]    tags {     Tool    = "${var.tags-tool}"     Contact = "${var.tags-contact}"   } } 
root.tf :

provider "aws" {
  region = "eu-central-1"
}

variables.tf :

variable "default-aws-region" {     default = "eu-central-1" }  variable "tags-tool" {     default = "Terraform" }  variable "tags-contact" {     default = "Aurelie Vache" }  variable "aws_s3_bucket_terraform" {     default = "com.programmez.terraform" }  variable "bucket_demo" {   default = "com.programmez.demo" }  variable "bucket_demo_logs" {   default = "com.programmez.demo.logs" }  variable "bucket_demo_preprod" {   default = "com.programmez.demo.preprod.demo" }  variable "bucket_demo_logs_preprod" {   default = "com.programmez.demo.preprod.demo.logs" }  variable "demo_domain_name" {   default = "demo.programmez.com" }

output.tf :

output "cloudfront_id" {   value = "${aws_cloudfront_distribution.front_cdn.id}" }

backend.tf:

# Backend configuration is loaded early so we can't use variables terraform {   backend "s3" {     region = "eu-central-1"     bucket = "com.programmez.terraform"     key = "state.tfstate"     encrypt = true    #AES-256 encryption   } }