-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
168 lines (121 loc) · 3.74 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.36.0"
}
}
}
# ~~~~~~~~~~~~~~~~~~~~ Configure the AWS provider ~~~~~~~~~~~~~~~~~~~~
provider "aws" {
region = var.region
}
# ~~~~~~~~~~~~~~~~~~~~~~~~ Create the bucket ~~~~~~~~~~~~~~~~~~~~~~~~~~
resource "aws_s3_bucket" "bucket1" {
bucket = var.bucket_name
force_destroy = true
}
# ~~~~~~~~~~~ Configure public access parameters in the bucket ~~~~~~~~
resource "aws_s3_bucket_ownership_controls" "rule" {
bucket = aws_s3_bucket.bucket1.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_public_access_block" "bucket_access_block" {
bucket = aws_s3_bucket.bucket1.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
resource "aws_s3_bucket_acl" "bucket1-acl" {
bucket = aws_s3_bucket.bucket1.id
acl = "public-read"
depends_on = [ aws_s3_bucket_ownership_controls.rule, aws_s3_bucket_public_access_block.bucket_access_block,aws_s3_bucket_acl.bucket1-acl]
}
# ~~~~~~~~~~~~~~~~~~~ Configure The Bucket policy ~~~~~~~~~~~~~~~~~~
resource "aws_s3_bucket_policy" "allow_access" {
bucket = aws_s3_bucket.bucket1.id
policy = data.aws_iam_policy_document.allow_access_from_another_account.json
depends_on = [ aws_s3_bucket_acl.bucket1-acl ]
}
data "aws_iam_policy_document" "allow_access_from_another_account" {
statement {
principals {
type = "AWS"
identifiers = ["*"]
}
actions = [
"s3:*"
]
resources = [
aws_s3_bucket.bucket1.arn,
"${aws_s3_bucket.bucket1.arn}/*",
]
}
}
# ~~~~~~~~~~~~~~~~~ Upload the site content in the bucket ~~~~~~~~~~~~~
resource "null_resource" "upload_files" {
provisioner "local-exec" {
command = "aws s3 sync ./${var.cp-path} s3://${aws_s3_bucket.bucket1.bucket}/ --region ${var.region} --debug"
}
depends_on = [aws_s3_bucket.bucket1 , aws_s3_bucket_policy.allow_access]
}
# ~~~~~~~~~~~ Configure the web hosting parameters in the bucket ~~~~~~~
resource "aws_s3_bucket_website_configuration" "bucket" {
bucket = aws_s3_bucket.bucket1.id
index_document {
suffix = var.file-key
}
error_document {
key = var.file-key
}
depends_on = [aws_s3_bucket.bucket1]
}
# ~~~~~~~~~~~~~~~~~~~~~~ Configure CloudFont ~~~~~~~~~~~~~~~~~~~~~
locals {
s3_origin_id = "${var.bucket_name}-origin"
s3_domain_name = "${var.bucket_name}.s3-website.${var.region}.amazonaws.com"
}
resource "aws_cloudfront_distribution" "web-distribution" {
enabled = true
origin {
origin_id = local.s3_origin_id
domain_name = local.s3_domain_name
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "http-only"
origin_ssl_protocols = ["TLSv1"]
}
}
default_cache_behavior {
target_origin_id = local.s3_origin_id
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
forwarded_values {
query_string = true
cookies {
forward = "all"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 0
max_ttl = 0
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
cloudfront_default_certificate = true
}
price_class = "PriceClass_200"
depends_on = [ aws_s3_bucket.bucket1 , null_resource.upload_files, aws_s3_bucket_acl.bucket1-acl, aws_s3_bucket_policy.allow_access ]
}
output "INFO" {
value = "AWS Resources has been provisioned yes. Go to http://${aws_cloudfront_distribution.web-distribution.domain_name}"
}