-
Notifications
You must be signed in to change notification settings - Fork 0
/
eks.tf
281 lines (254 loc) · 7.31 KB
/
eks.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
provider "aws" {
region = "ap-south-1"
profile = "terrauser"
}
resource "tls_private_key" "key_form" {
algorithm = "RSA"
}
resource "aws_key_pair" "task_key" {
key_name = "task_key"
public_key = "${tls_private_key.key_form.public_key_openssh}"
}
resource "aws_vpc" "myvpc" {
cidr_block = "10.5.0.0/16"
enable_dns_hostnames = true
tags = {
Name = "main"
}
}
resource "aws_subnet" "alpha-1a" {
vpc_id = "${aws_vpc.myvpc.id}"
availability_zone = "ap-south-1a"
cidr_block = "10.5.1.0/24"
map_public_ip_on_launch = true
tags = {
Name = "main-1a"
}
}
resource "aws_internet_gateway" "gw" {
vpc_id = "${aws_vpc.myvpc.id}"
tags = {
Name = "main-1a"
}
}
resource "aws_route_table" "rtable" {
vpc_id = "${aws_vpc.myvpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gw.id}"
}
tags = {
Name = "main-1a"
}
}
resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.alpha-1a.id
route_table_id = aws_route_table.rtable.id
}
resource "aws_security_group" "allow_http" {
name = "allow_http"
description = "Allow HTTP inbound traffic"
vpc_id = "${aws_vpc.myvpc.id}"
ingress {
description = "Http from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0" ]
}
ingress {
description = "SSH from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0" ]
}
ingress {
description = "NFS"
from_port = 2049
to_port = 2049
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0" ]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "sgroup"
}
}
resource "aws_efs_file_system" "mypd" {
creation_token = "my-secure-pd"
tags = {
Name = "Mypersonalpd"
}
}
resource "aws_efs_file_system_policy" "policy" {
file_system_id = "${aws_efs_file_system.mypd.id}"
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "efs-policy-wizard-c45881c9-af16-441d-aa48-0fbd68ffaf79",
"Statement": [
{
"Sid": "efs-statement-20e4223c-ca0e-412d-8490-3c3980f60788",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Resource": "${aws_efs_file_system.mypd.arn}",
"Action": [
"elasticfilesystem:ClientMount",
"elasticfilesystem:ClientWrite",
"elasticfilesystem:ClientRootAccess"
],
"Condition": {
"Bool": {
"aws:SecureTransport": "true"
}
}
}
]
}
POLICY
}
resource "aws_efs_mount_target" "alpha" {
file_system_id = "${aws_efs_file_system.mypd.id}"
subnet_id = "${aws_subnet.alpha-1a.id}"
security_groups = [ "${aws_security_group.allow_http.id}" ]
}
resource "aws_instance" "web" {
ami = "ami-00b494a3f139ba61f"
instance_type = "t2.micro"
key_name = "task_key"
availability_zone = "ap-south-1a"
subnet_id = "${aws_subnet.alpha-1a.id}"
security_groups = [ "${aws_security_group.allow_http.id}" ]
tags = {
Name = "HelloWorld"
}
}
resource "null_resource" "null_vol_attach" {
depends_on = [
aws_efs_mount_target.alpha,
]
connection {
type = "ssh"
user = "ec2-user"
private_key = tls_private_key.key_form.private_key_pem
host = aws_instance.web.public_ip
}
provisioner "remote-exec" {
inline = [
"sleep 30",
"sudo yum install -y httpd git php amazon-efs-utils nfs-utils",
"sudo systemctl start httpd",
"sudo systemctl enable httpd",
"sudo chmod ugo+rw /etc/fstab",
"sudo echo '${aws_efs_file_system.mypd.id}:/ /var/www/html efs tls,_netdev' >> /etc/fstab",
"sudo mount -a -t efs,nfs4 defaults",
"sudo rm -rf /var/www/html/*",
"sudo git clone https://github.com/gaurav-gupta-gtm/terraform-aws-code.git /var/www/html/"
]
}
}
resource "aws_s3_bucket" "my-terra-bucket-12341" {
bucket = "my-terra-bucket-12341"
acl = "public-read"
force_destroy = true
cors_rule {
allowed_headers = ["*"]
allowed_methods = ["PUT", "POST"]
allowed_origins = ["https://my-terra-bucket-12341"]
expose_headers = ["ETag"]
max_age_seconds = 3000
}
depends_on = [
null_resource.null_vol_attach,
]
}
resource "aws_s3_bucket_object" "obj" {
key = "gaurav.jpeg"
bucket = aws_s3_bucket.my-terra-bucket-12341.id
source = "gaurav.jpeg"
acl="public-read"
}
# Create Cloudfront distribution
resource "aws_cloudfront_distribution" "distribution" {
origin {
domain_name = "${aws_s3_bucket.my-terra-bucket-12341.bucket_regional_domain_name}"
origin_id = "S3-${aws_s3_bucket.my-terra-bucket-12341.bucket}"
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "match-viewer"
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
}
}
# By default, show gaurav.jpeg file
default_root_object = "gaurav.jpeg"
enabled = true
# If there is a 404, return gaurav.jpeg with a HTTP 200 Response
custom_error_response {
error_caching_min_ttl = 3000
error_code = 404
response_code = 200
response_page_path = "/gaurav.jpeg"
}
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "S3-${aws_s3_bucket.my-terra-bucket-12341.bucket}"
#Not Forward all query strings, cookies and headers
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
# Restricts who is able to access this content
restrictions {
geo_restriction {
# type of restriction, blacklist, whitelist or none
restriction_type = "none"
}
}
# SSL certificate for the service.
viewer_certificate {
cloudfront_default_certificate = true
}
}
resource "null_resource" "update_pic_url" {
depends_on = [
null_resource.null_vol_attach,
aws_cloudfront_distribution.distribution,
]
connection {
type = "ssh"
user = "ec2-user"
private_key = tls_private_key.key_form.private_key_pem
host = aws_instance.web.public_ip
}
provisioner "remote-exec" {
inline = [
"sudo chmod ugo+rw /var/www/html/index.php",
"sudo echo '<img src=http://${aws_cloudfront_distribution.distribution.domain_name}/gaurav.jpeg alt='GAURAV GUPTA' width='500' height='600'</a>' >> /var/www/html/index.php"
]
}
}
output "cloudfront_ip_addr" {
value = aws_cloudfront_distribution.distribution.domain_name
}
resource "null_resource" "save_key_pair" {
provisioner "local-exec" {
command = "echo '${tls_private_key.key_form.private_key_pem}' > key.pem"
}
}