From 6690d7ded51bf6e57609fe45d8c23736284d8f05 Mon Sep 17 00:00:00 2001 From: mutai-gilbert Date: Mon, 18 Nov 2024 09:26:38 +0300 Subject: [PATCH] Restructure the folder and add terraform files to the frontend folder --- frontend/terraform/main.tf | 33 +++++++++++++++++++++++++++++++++ frontend/terraform/outputs.tf | 7 +++++++ frontend/terraform/provider.tf | 0 frontend/terraform/variables.tf | 3 +++ 4 files changed, 43 insertions(+) create mode 100644 frontend/terraform/main.tf create mode 100644 frontend/terraform/outputs.tf create mode 100644 frontend/terraform/provider.tf create mode 100644 frontend/terraform/variables.tf diff --git a/frontend/terraform/main.tf b/frontend/terraform/main.tf new file mode 100644 index 0000000..27d8899 --- /dev/null +++ b/frontend/terraform/main.tf @@ -0,0 +1,33 @@ +provider "aws" { + region = var.region +} + +resource "aws_s3_bucket" "frontend_bucket" { + bucket = "my-react-frontend-bucket" + acl = "public-read" + force_destroy = true + + website { + index_document = "index.html" + error_document = "index.html" + } +} + +resource "aws_cloudfront_distribution" "cdn" { + origin { + domain_name = aws_s3_bucket.frontend_bucket.bucket_regional_domain_name + origin_id = "S3-my-react-frontend-bucket" + } + + default_cache_behavior { + allowed_methods = ["GET", "HEAD"] + cached_methods = ["GET", "HEAD"] + target_origin_id = "S3-my-react-frontend-bucket" + + viewer_protocol_policy = "redirect-to-https" + } + + enabled = true + is_ipv6_enabled = true + default_root_object = "index.html" +} diff --git a/frontend/terraform/outputs.tf b/frontend/terraform/outputs.tf new file mode 100644 index 0000000..8bc0c3c --- /dev/null +++ b/frontend/terraform/outputs.tf @@ -0,0 +1,7 @@ +output "s3_bucket_name" { + value = aws_s3_bucket.frontend_bucket.bucket +} + +output "cloudfront_url" { + value = aws_cloudfront_distribution.cdn.domain_name +} diff --git a/frontend/terraform/provider.tf b/frontend/terraform/provider.tf new file mode 100644 index 0000000..e69de29 diff --git a/frontend/terraform/variables.tf b/frontend/terraform/variables.tf new file mode 100644 index 0000000..905a247 --- /dev/null +++ b/frontend/terraform/variables.tf @@ -0,0 +1,3 @@ +variable "region" { + default = "us-east-1" +}