Skip to content

Commit

Permalink
created diff terraform config to use existing bucket and remove redun…
Browse files Browse the repository at this point in the history
…dent file uploads in terraform config
  • Loading branch information
junhyunlee123 committed May 23, 2024
1 parent 0f1d38b commit 444c25c
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
- name: 'Install Terraform CLI'
uses: hashicorp/setup-terraform@v3
- name: Execute 'terraform apply'
run: 'cd terraform/ && terraform init && terraform apply -auto-approve'
run: 'cd terraform_github_action/ && terraform init && terraform apply -auto-approve'



Expand Down
2 changes: 1 addition & 1 deletion terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ resource "aws_s3_bucket" "tf_bucket" {
bucket = "tf-files-for-nginx-expressjs-project"

tags = {
Name = "My bucket"
Name = "expressjs project website bucket"
Environment = "Dev"
}
}
Expand Down
25 changes: 25 additions & 0 deletions terraform_github_action/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

170 changes: 170 additions & 0 deletions terraform_github_action/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}

# Configure the AWS Provider
provider "aws" {
region = "ap-northeast-2"
}

data "aws_s3_bucket" "s3_bucket" {
bucket = "tf-files-for-nginx-expressjs-project"
}

resource "aws_s3_bucket_website_configuration" "website_config" {
bucket = data.aws_s3_bucket.s3_bucket.id

index_document {
suffix = "index.html"
}

error_document {
key = "error.html"
}

}

data "aws_iam_policy_document" "policy_document" {
statement {
sid = "PublicReadGetObject"

actions = [
"s3:GetObject"
]

resources = [
"arn:aws:s3:::tf-files-for-nginx-expressjs-project/*"
]

principals {
type = "AWS"
identifiers = ["*"]
}
}
}

resource "aws_s3_bucket_policy" "attach_policy_to_s3" {
bucket = data.aws_s3_bucket.s3_bucket.id
policy = data.aws_iam_policy_document.policy_document.json
}

resource "aws_s3_bucket_public_access_block" "public_access" {
bucket = data.aws_s3_bucket.s3_bucket.id

block_public_acls = true
block_public_policy = false
ignore_public_acls = true
restrict_public_buckets = false
}



locals {
s3_origin_id = "S3Origin"
}

locals {
ALB_origin_id = "ALBOrigin"
}

variable "lb_arn" {
type = string
default = "arn:aws:elasticloadbalancing:ap-northeast-2:533267359186:loadbalancer/app/expressjsLB/8f25a781c4a32738"
}

variable "lb_name" {
type = string
default = "expressjsLB"
}

data "aws_lb" "api_alb" {
arn = var.lb_arn
name = var.lb_name
}

resource "aws_cloudfront_distribution" "s3_expressjs_distribution" {
origin {
domain_name = data.aws_s3_bucket.s3_bucket.bucket_domain_name
origin_id = local.s3_origin_id
}

origin {
domain_name = data.aws_lb.api_alb.dns_name
origin_id = local.ALB_origin_id
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "http-only"
origin_ssl_protocols = ["SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"]
}
}

enabled = true
is_ipv6_enabled = true
comment = "terraform implementation of s3 and cloudfront part of nginx_expressjs project"
default_root_object = "index.html"

default_cache_behavior {
allowed_methods = ["HEAD", "GET"]
cached_methods = ["HEAD", "GET"]
target_origin_id = local.s3_origin_id
viewer_protocol_policy = "redirect-to-https"

forwarded_values {
query_string = false

cookies {
forward = "none"
}
}
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}

# Cache behavior with precedence 0

ordered_cache_behavior {
path_pattern = "/api/*"
allowed_methods = ["HEAD", "DELETE", "POST", "GET", "OPTIONS", "PUT", "PATCH"]
cached_methods = ["HEAD", "GET"]
target_origin_id = local.ALB_origin_id
viewer_protocol_policy = "redirect-to-https"

forwarded_values {
query_string = false
headers = ["Origin"]

cookies {
forward = "all"
}
}

min_ttl = 0
default_ttl = 86400
max_ttl = 31536000
compress = true
}

price_class = "PriceClass_200"

restrictions {
geo_restriction {
restriction_type = "whitelist"
locations = ["US", "JP", "KR"]
}
}

tags = {
Environment = "production"
}

viewer_certificate {
cloudfront_default_certificate = true
}
}

0 comments on commit 444c25c

Please sign in to comment.