From 56bf3c121fbc0d4bb31a5e1b073b80daac7dba51 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Mon, 15 Apr 2024 13:31:46 +0100 Subject: [PATCH] Adding cdn to terraform --- infrastructure/terraform/main.tf | 9 +++ .../terraform/modules/cloudfront/main.tf | 65 +++++++++++++++++++ .../terraform/modules/cloudfront/variables.tf | 9 +++ 3 files changed, 83 insertions(+) create mode 100644 infrastructure/terraform/modules/cloudfront/main.tf create mode 100644 infrastructure/terraform/modules/cloudfront/variables.tf diff --git a/infrastructure/terraform/main.tf b/infrastructure/terraform/main.tf index d545cdf8..1d0562dd 100644 --- a/infrastructure/terraform/main.tf +++ b/infrastructure/terraform/main.tf @@ -181,4 +181,13 @@ module "lambda_carbon_prediction_ecr" { module "lambda_heat_prediction_ecr" { ecr_name = "lambda-heat-prediction-${var.stage}" source = "./modules/ecr" +} + +############################################## +# CDN - Cloudfront +############################################## +module "cloudfront_distribution" { + source = "./modules/cloudfront" + bucket_name = module.s3.bucket_name + stage = var.stage } \ No newline at end of file diff --git a/infrastructure/terraform/modules/cloudfront/main.tf b/infrastructure/terraform/modules/cloudfront/main.tf new file mode 100644 index 00000000..fbb88160 --- /dev/null +++ b/infrastructure/terraform/modules/cloudfront/main.tf @@ -0,0 +1,65 @@ +resource "aws_cloudfront_distribution" "s3_distribution" { + origin { + domain_name = "${aws_s3_bucket.bucket.bucket_regional_domain_name}" + origin_id = "S3-${var.bucket_name}" + + s3_origin_config { + origin_access_identity = aws_cloudfront_origin_access_identity.oai.cloudfront_access_identity_path + } + } + + enabled = true + + default_cache_behavior { + allowed_methods = ["GET", "HEAD"] + cached_methods = ["GET", "HEAD"] + target_origin_id = "S3-${var.bucket_name}" + viewer_protocol_policy = "redirect-to-https" + compress = true + + forwarded_values { + query_string = false + cookies { + forward = "none" + } + } + + min_ttl = 0 + default_ttl = 86400 + max_ttl = 31536000 + } + + price_class = "PriceClass_All" + + restrictions { + geo_restriction { + restriction_type = "none" + } + } + + viewer_certificate { + cloudfront_default_certificate = true + } +} + +resource "aws_cloudfront_origin_access_identity" "oai" { + comment = "OAI for ${var.bucket_name}" +} + +resource "aws_s3_bucket_policy" "bucket_policy" { + bucket = aws_s3_bucket.bucket.id + + policy = jsonencode({ + Version = "2012-10-17" + Statement = [ + { + Effect = "Allow" + Principal = { + AWS = "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ${aws_cloudfront_origin_access_identity.oai.id}" + } + Action = "s3:GetObject" + Resource = "${aws_s3_bucket.bucket.arn}/*" + }, + ] + }) +} diff --git a/infrastructure/terraform/modules/cloudfront/variables.tf b/infrastructure/terraform/modules/cloudfront/variables.tf new file mode 100644 index 00000000..433edc24 --- /dev/null +++ b/infrastructure/terraform/modules/cloudfront/variables.tf @@ -0,0 +1,9 @@ +variable "bucket_name" { + description = "The name of the bucket" + type = string +} + +variable "stage" { + description = "The deployment stage" + type = string +}