From ce546b56f7db4a88d82ee3f72148d2b4fe64f1c2 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Mon, 15 Apr 2024 13:38:23 +0100 Subject: [PATCH] passing additional data to cloudfront distribution --- infrastructure/terraform/main.tf | 9 ++++++--- .../terraform/modules/cloudfront/main.tf | 6 +++--- .../terraform/modules/cloudfront/variables.tf | 15 +++++++++++++++ infrastructure/terraform/modules/s3/outputs.tf | 12 ++++++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/infrastructure/terraform/main.tf b/infrastructure/terraform/main.tf index 1d0562dd..fde25487 100644 --- a/infrastructure/terraform/main.tf +++ b/infrastructure/terraform/main.tf @@ -187,7 +187,10 @@ module "lambda_heat_prediction_ecr" { # CDN - Cloudfront ############################################## module "cloudfront_distribution" { - source = "./modules/cloudfront" - bucket_name = module.s3.bucket_name - stage = var.stage + source = "./modules/cloudfront" + bucket_name = module.s3.bucket_name + bucket_id = module.s3.bucket_id + bucket_arn = module.s3.bucket_arn + bucket_domain_name = module.s3.bucket_domain_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 index fbb88160..281ff09f 100644 --- a/infrastructure/terraform/modules/cloudfront/main.tf +++ b/infrastructure/terraform/modules/cloudfront/main.tf @@ -1,6 +1,6 @@ resource "aws_cloudfront_distribution" "s3_distribution" { origin { - domain_name = "${aws_s3_bucket.bucket.bucket_regional_domain_name}" + domain_name = var.bucket_domain_name origin_id = "S3-${var.bucket_name}" s3_origin_config { @@ -47,7 +47,7 @@ resource "aws_cloudfront_origin_access_identity" "oai" { } resource "aws_s3_bucket_policy" "bucket_policy" { - bucket = aws_s3_bucket.bucket.id + bucket = var.bucket_id policy = jsonencode({ Version = "2012-10-17" @@ -58,7 +58,7 @@ resource "aws_s3_bucket_policy" "bucket_policy" { 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}/*" + Resource = "${var.bucket_arn}/*" }, ] }) diff --git a/infrastructure/terraform/modules/cloudfront/variables.tf b/infrastructure/terraform/modules/cloudfront/variables.tf index 433edc24..88f770a8 100644 --- a/infrastructure/terraform/modules/cloudfront/variables.tf +++ b/infrastructure/terraform/modules/cloudfront/variables.tf @@ -7,3 +7,18 @@ variable "stage" { description = "The deployment stage" type = string } + +variable "bucket_id" { + description = "The ID of the S3 bucket" + type = string +} + +variable "bucket_arn" { + description = "The ARN of the S3 bucket" + type = string +} + +variable "bucket_domain_name" { + description = "The regional domain name of the S3 bucket" + type = string +} \ No newline at end of file diff --git a/infrastructure/terraform/modules/s3/outputs.tf b/infrastructure/terraform/modules/s3/outputs.tf index a5e7ddb4..7668dbc4 100644 --- a/infrastructure/terraform/modules/s3/outputs.tf +++ b/infrastructure/terraform/modules/s3/outputs.tf @@ -2,3 +2,15 @@ output "bucket_name" { description = "The name of the S3 bucket" value = aws_s3_bucket.bucket.bucket } + +output "bucket_id" { + value = aws_s3_bucket.bucket.id +} + +output "bucket_arn" { + value = aws_s3_bucket.bucket.arn +} + +output "bucket_domain_name" { + value = aws_s3_bucket.bucket.bucket_regional_domain_name +} \ No newline at end of file