Skip to content

Commit 6ab6a56

Browse files
committed
Initial commit from existing TF
1 parent aadebdd commit 6ab6a56

10 files changed

+287
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Local .terraform directories
22
**/.terraform/*
33

4+
/.idea
5+
46
# .tfstate files
57
*.tfstate
68
*.tfstate.*

acm.tf

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resource "aws_acm_certificate" "cloudfront" {
2+
provider = aws.us-east-1
3+
domain_name = var.primary_domain_name
4+
validation_method = "DNS"
5+
lifecycle {
6+
create_before_destroy = true
7+
}
8+
9+
subject_alternative_names = var.secondary_domain_names
10+
11+
}
12+
13+
resource "aws_acm_certificate_validation" "cloudfront" {
14+
provider = aws.us-east-1
15+
certificate_arn = aws_acm_certificate.cloudfront.arn
16+
validation_record_fqdns = [for record in aws_route53_record.cf_acm : record.fqdn]
17+
}

cloudfront.tf

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
resource "aws_cloudfront_origin_access_identity" "self" {
2+
}
3+
4+
data "aws_cloudfront_cache_policy" "Managed-CachingOptimized" {
5+
name = "Managed-CachingOptimized"
6+
}
7+
8+
resource "aws_cloudfront_distribution" "self" {
9+
origin {
10+
domain_name = aws_s3_bucket.origin.bucket_regional_domain_name
11+
origin_id = local.s3_origin_id
12+
13+
s3_origin_config {
14+
origin_access_identity = aws_cloudfront_origin_access_identity.self.cloudfront_access_identity_path
15+
}
16+
}
17+
18+
enabled = true
19+
is_ipv6_enabled = true
20+
default_root_object = "index.html"
21+
http_version = "http2and3"
22+
23+
aliases = concat([var.primary_domain_name], var.secondary_domain_names)
24+
25+
logging_config {
26+
include_cookies = false
27+
bucket = aws_s3_bucket.origin_logs.bucket_domain_name
28+
}
29+
30+
default_cache_behavior {
31+
allowed_methods = ["GET", "HEAD"]
32+
cached_methods = ["GET", "HEAD"]
33+
target_origin_id = local.s3_origin_id
34+
35+
cache_policy_id = data.aws_cloudfront_cache_policy.Managed-CachingOptimized.id
36+
37+
38+
compress = true
39+
viewer_protocol_policy = "redirect-to-https"
40+
41+
function_association {
42+
event_type = "viewer-request"
43+
function_arn = aws_cloudfront_function.request.arn
44+
}
45+
46+
/*function_association {
47+
event_type = "viewer-response"
48+
function_arn = aws_cloudfront_function.response.arn
49+
}*/
50+
51+
response_headers_policy_id = aws_cloudfront_response_headers_policy.self.id
52+
}
53+
54+
restrictions {
55+
geo_restriction {
56+
restriction_type = "none"
57+
}
58+
}
59+
60+
price_class = "PriceClass_200"
61+
62+
viewer_certificate {
63+
acm_certificate_arn = aws_acm_certificate.cloudfront.arn
64+
ssl_support_method = "sni-only"
65+
minimum_protocol_version = "TLSv1.2_2021"
66+
}
67+
68+
custom_error_response {
69+
error_code = 404
70+
error_caching_min_ttl = 86400
71+
response_page_path = "/404.html"
72+
response_code = 404
73+
}
74+
}
75+
76+
resource "aws_cloudfront_function" "request" {
77+
code = file("${path.module}/request.js")
78+
name = "indexer"
79+
runtime = "cloudfront-js-1.0"
80+
publish = true
81+
}
82+
83+
resource "aws_cloudfront_function" "response" {
84+
code = file("${path.module}/response.js")
85+
name = "response"
86+
runtime = "cloudfront-js-1.0"
87+
publish = true
88+
}
89+
90+
resource "aws_cloudfront_response_headers_policy" "self" {
91+
name = "blog"
92+
93+
security_headers_config {
94+
content_type_options {
95+
override = true
96+
}
97+
98+
frame_options {
99+
frame_option = "DENY"
100+
override = true
101+
}
102+
103+
strict_transport_security {
104+
access_control_max_age_sec = 31536000
105+
include_subdomains = true
106+
override = true
107+
preload = true
108+
}
109+
110+
xss_protection {
111+
mode_block = true
112+
override = true
113+
protection = true
114+
}
115+
116+
referrer_policy {
117+
override = true
118+
referrer_policy = "strict-origin-when-cross-origin"
119+
}
120+
121+
/*content_security_policy {
122+
content_security_policy = "default-src 'none'; img-src 'self'; script-src 'unsafe-inline'; style-src 'self'"
123+
override = true
124+
}*/
125+
126+
127+
}
128+
}

data.tf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "aws_route53_zone" "self" {
2+
name = var.primary_domain_name
3+
}

input.tf

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
variable "name" {
2+
type = string
3+
}
4+
5+
variable "primary_domain_name" {
6+
type = string
7+
}
8+
9+
variable "secondary_domain_names" {
10+
type = list(string)
11+
default = []
12+
}

locals.tf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
s3_origin_id = "blogs3origin"
3+
}

provider.tf

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
provider "aws" {}
2+
3+
provider "aws" {
4+
alias = "us-east-1"
5+
region = "us-east-1"
6+
}
7+
8+
terraform {
9+
backend "s3" {
10+
bucket = "net-corryh-terraform"
11+
key = "website/core.tf"
12+
region = "us-west-2"
13+
}
14+
}

route53.tf

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
resource "aws_route53_record" "cf" {
2+
name = var.primary_domain_name
3+
type = "A"
4+
zone_id = data.aws_route53_zone.self.zone_id
5+
6+
alias {
7+
evaluate_target_health = false
8+
name = aws_cloudfront_distribution.self.domain_name
9+
zone_id = aws_cloudfront_distribution.self.hosted_zone_id
10+
}
11+
}
12+
13+
resource "aws_route53_record" "cf_aaaa" {
14+
name = var.primary_domain_name
15+
type = "AAAA"
16+
zone_id = data.aws_route53_zone.self.zone_id
17+
18+
alias {
19+
evaluate_target_health = false
20+
name = aws_cloudfront_distribution.self.domain_name
21+
zone_id = aws_cloudfront_distribution.self.hosted_zone_id
22+
}
23+
}
24+
25+
resource "aws_route53_record" "cf_acm" {
26+
for_each = {
27+
for dvo in aws_acm_certificate.cloudfront.domain_validation_options : dvo.domain_name => {
28+
name = dvo.resource_record_name
29+
record = dvo.resource_record_value
30+
type = dvo.resource_record_type
31+
}
32+
}
33+
34+
allow_overwrite = true
35+
name = each.value.name
36+
records = [each.value.record]
37+
ttl = 60
38+
type = each.value.type
39+
zone_id = data.aws_route53_zone.self.zone_id
40+
}

s3.tf

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
resource "aws_s3_bucket" "origin" {
2+
bucket = var.name
3+
}
4+
5+
resource "aws_s3_bucket" "origin_logs" {
6+
bucket = "${var.name}-logs"
7+
}
8+
9+
resource "aws_s3_bucket_acl" "origin" {
10+
bucket = aws_s3_bucket.origin.id
11+
acl = "private"
12+
}
13+
14+
resource "aws_s3_bucket_acl" "origin_logs" {
15+
bucket = aws_s3_bucket.origin_logs.id
16+
acl = "log-delivery-write"
17+
}
18+
19+
resource "aws_s3_bucket_public_access_block" "origin" {
20+
bucket = aws_s3_bucket.origin.id
21+
22+
block_public_acls = true
23+
block_public_policy = true
24+
}
25+
26+
resource "aws_s3_bucket_public_access_block" "origin_logs" {
27+
bucket = aws_s3_bucket.origin_logs.id
28+
29+
block_public_acls = true
30+
block_public_policy = true
31+
}
32+
33+
data "aws_iam_policy_document" "origin" {
34+
statement {
35+
actions = ["s3:GetObject"]
36+
resources = ["${aws_s3_bucket.origin.arn}/*"]
37+
38+
principals {
39+
type = "AWS"
40+
identifiers = ["${aws_cloudfront_origin_access_identity.self.iam_arn}"]
41+
}
42+
}
43+
44+
statement {
45+
actions = ["s3:ListBucket"]
46+
resources = ["${aws_s3_bucket.origin.arn}"]
47+
48+
principals {
49+
type = "AWS"
50+
identifiers = ["${aws_cloudfront_origin_access_identity.self.iam_arn}"]
51+
}
52+
}
53+
}
54+
55+
resource "aws_s3_bucket_policy" "origin" {
56+
bucket = aws_s3_bucket.origin.id
57+
policy = data.aws_iam_policy_document.origin.json
58+
}

versions.tf

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 4.0"
6+
}
7+
}
8+
9+
required_version = ">= 0.15"
10+
}

0 commit comments

Comments
 (0)