-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcsn-cloudformation.yml
555 lines (484 loc) · 15.1 KB
/
pcsn-cloudformation.yml
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
Resources:
# VPC
PlagiarismCheckSystemVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: Plagiarism Check System Network
# Internet Gateway
PlagiarismCheckSystemInternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: Plagiarism Check System Network Internet Gateway
# Internet Gateway Attachment
PlagiarismCheckSystemVPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref PlagiarismCheckSystemVPC
InternetGatewayId: !Ref PlagiarismCheckSystemInternetGateway
PlagiarismCheckSystemVPCRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref PlagiarismCheckSystemVPC
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: Plagiarism Check System Network Route Table
PublicRoute:
Type: AWS::EC2::Route
DependsOn: PlagiarismCheckSystemVPCGatewayAttachment
Properties:
RouteTableId: !Ref PlagiarismCheckSystemVPCRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref PlagiarismCheckSystemInternetGateway
# Public Subnets
PublicSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref PlagiarismCheckSystemVPC
AvailabilityZone: us-east-1a
CidrBlock: 10.0.1.0/24
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: Public Subnet 1
PublicSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref PlagiarismCheckSystemVPC
AvailabilityZone: us-east-1b
CidrBlock: 10.0.2.0/24
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: Public Subnet 2
PublicSubnet1RouteTableAssociation:
DependsOn: PublicSubnet1
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PlagiarismCheckSystemVPCRouteTable
SubnetId: !Ref PublicSubnet1
PublicSubnet2RouteTableAssociation:
DependsOn: PublicSubnet2
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PlagiarismCheckSystemVPCRouteTable
SubnetId: !Ref PublicSubnet2
# Private Subnets
PrivateSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref PlagiarismCheckSystemVPC
AvailabilityZone: us-east-1c
CidrBlock: 10.0.3.0/24
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: Private Subnet 1
PrivateSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref PlagiarismCheckSystemVPC
AvailabilityZone: us-east-1d
CidrBlock: 10.0.4.0/24
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: Private Subnet 2
# Security Groups
PCSNBastionHostSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allows SSH traffic to Bastion Host
VpcId: !Ref PlagiarismCheckSystemVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: PCSN-Bastion-Host
# EC2 Instances
BastionHost1:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: ami-0557a15b87f6559cf # Ubuntu 18.04 LTS
KeyName: vockey
SubnetId: !Ref PublicSubnet1
SecurityGroupIds:
- !Ref PCSNBastionHostSecurityGroup
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: Bastion Host 1
BastionHost1EIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
BastionHost1EIPAssociation:
Type: AWS::EC2::EIPAssociation
Properties:
InstanceId: !Ref BastionHost1
AllocationId: !GetAtt BastionHost1EIP.AllocationId
BastionHost2:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: ami-0557a15b87f6559cf # Ubuntu 18.04 LTS
KeyName: vockey
SubnetId: !Ref PublicSubnet2
SecurityGroupIds:
- !Ref PCSNBastionHostSecurityGroup
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: Bastion Host 2
BastionHost2EIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
BastionHost2EIPAssociation:
Type: AWS::EC2::EIPAssociation
Properties:
InstanceId: !Ref BastionHost2
AllocationId: !GetAtt BastionHost2EIP.AllocationId
PCSNPrivateSecurityGroup1:
DependsOn:
- BastionHost1
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allows SSH traffic from Bastion Host
VpcId: !Ref PlagiarismCheckSystemVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
# CidrIp: !Sub "${BastionHost1EIP.AllocationId}/32"
CidrIp: !Sub "0.0.0.0/0"
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: PCSN-Bastion-Host
PCSNPrivateSecurityGroup2:
DependsOn:
- BastionHost2
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allows SSH traffic from Bastion Host
VpcId: !Ref PlagiarismCheckSystemVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Sub "0.0.0.0/0"
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: PCSN-Bastion-Host
PCSNPrivateSecurityGroup11:
DependsOn:
- BastionHost1
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allows SSH traffic from Bastion Host
VpcId: !Ref PlagiarismCheckSystemVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
# CidrIp: !Sub "${BastionHost2EIP.AllocationId}/32"
CidrIp: !Sub "0.0.0.0/0"
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: PCSN-Bastion-Host
PCSNPrivateSecurityGroup22:
DependsOn:
- BastionHost2
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allows SSH traffic from Bastion Host
VpcId: !Ref PlagiarismCheckSystemVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
# CidrIp: !Sub "${BastionHost1EIP.AllocationId}/32"
CidrIp: !Sub "0.0.0.0/0"
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: PCSN-Bastion-Host
PrivateInstance1:
DependsOn: BastionHost1
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: ami-0557a15b87f6559cf # Ubuntu 18.04 LTS
KeyName: PCSN-Private-Key
SubnetId: !Ref PrivateSubnet1
SecurityGroupIds:
- !Ref PCSNPrivateSecurityGroup1
- !Ref PCSNPrivateSecurityGroup11
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: Private Instance 1
UserData:
Fn::Base64: !Sub |
#!/bin/bash
sudo apt update
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install python3.9 -y
sudo apt install python3-pip -y
pip3 install boto3
pip3 install copydetect
# Create the cronjob
sudo crontab -l > mycron
echo "0 * * * * /usr/bin/python3 /load_and_process.py" >> mycron
sudo crontab mycron
rm mycron
wget https://pcsn-assets.s3.amazonaws.com/load_and_process.py -O /load_and_process.py
python3 /load_and_process.py &
PrivateInstance2:
DependsOn: BastionHost2
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: ami-0557a15b87f6559cf # Ubuntu 18.04 LTS
KeyName: PCSN-Private-Key
SubnetId: !Ref PrivateSubnet2
SecurityGroupIds:
- !Ref PCSNPrivateSecurityGroup2
- !Ref PCSNPrivateSecurityGroup22
Tags:
- Key: Type
Value: Term Project
- Key: Name
Value: Private Instance 2
UserData:
Fn::Base64: !Sub |
#!/bin/bash
sudo apt update
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install python3.9 -y
sudo apt install python3-pip -y
pip3 install boto3
pip3 install copydetect
# Create the cronjob
sudo crontab -l > mycron
echo "0 * * * * /usr/bin/python3 /load_and_process.py" >> mycron
sudo crontab mycron
rm mycron
wget https://pcsn-assets.s3.amazonaws.com/load_and_process.py -O /load_and_process.py
python3 /load_and_process.py &
################## S3 Bucket Configuration Here #####################
PCSNRawDocumentBucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: pdsn-raw-documents
PCSNRawDocumentBucketPolicy:
Type: 'AWS::S3::BucketPolicy'
Properties:
Bucket: !Ref PCSNRawDocumentBucket
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal: "*"
Action: "s3:*"
Resource: !Join ['', ['arn:aws:s3:::', !Ref PCSNRawDocumentBucket, '/*']]
Condition:
StringEquals:
aws:sourceVpc: !Ref PlagiarismCheckSystemVPC
'ArnLike':
aws:SourceArn: !Join ['', ['arn:aws:ec2:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':subnet/', !Ref PrivateSubnet1]]
'ArnLike':
aws:SourceArn: !Join ['', ['arn:aws:ec2:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':subnet/', !Ref PrivateSubnet2]]
PCSNProcessedDocumentBucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: pdsn-processed-documents
PCSNProcessedDocumentBucketEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
VpcEndpointType: Gateway
ServiceName: !Sub 'com.amazonaws.${AWS::Region}.s3'
VpcId: !Ref PlagiarismCheckSystemVPC
RouteTableIds:
- !Ref PlagiarismCheckSystemVPCRouteTable
PCSNProcessedDocumentBucketPolicy:
Type: 'AWS::S3::BucketPolicy'
Properties:
Bucket: !Ref PCSNProcessedDocumentBucket
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal: "*"
Action: "s3:*"
Resource: !Join ['', ['arn:aws:s3:::', !Ref PCSNProcessedDocumentBucket, '/*']]
Condition:
StringEquals:
aws:sourceVpc: !Ref PlagiarismCheckSystemVPC
aws:sourceVpce: !Ref PCSNProcessedDocumentBucketEndpoint
'ArnLike':
aws:SourceArn: !Join ['', ['arn:aws:ec2:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':subnet/', !Ref PrivateSubnet1]]
'ArnLike':
aws:SourceArn: !Join ['', ['arn:aws:ec2:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':subnet/', !Ref PrivateSubnet2]]
PCSNSqsQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: pcsn-sqs-queue
PCSNFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: "PCSNF-doc-upload-function"
Code:
ZipFile: |
import boto3
import json
def lambda_handler(event, context):
print(event)
body = json.loads(event['body'])
text = body['text']
file_name = body["filename"]
# Create S3 client
s3 = boto3.client('s3')
# Upload text to S3 bucket
bucket_name = 'pdsn-raw-documents'
s3.put_object(Bucket=bucket_name, Key=file_name, Body=text)
sqs = boto3.client('sqs', region_name='us-east-1')
queue_name = 'pcsn-sqs-queue'
response = sqs.get_queue_url(QueueName=queue_name)
queue_url = response['QueueUrl']
response = sqs.send_message(QueueUrl=queue_url, MessageBody=file_name)
# Return success message
return {
'statusCode': 200,
'body': 'Document Submitted Successfully'
}
Handler: "index.lambda_handler"
Role: "arn:aws:iam::596092100714:role/LabRole"
Runtime: "python3.9"
PCSNFunctionPolicy:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:invokeFunction
FunctionName: !Ref PCSNFunction
Principal: '*'
RuntimeLambdaUrl:
Type: "AWS::Lambda::Url"
Properties:
AuthType: NONE
TargetFunctionArn: !Ref PCSNFunction
PCSNSNSTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: Processed Document SNS
TopicName: document-processed
PCSNSNSTopicEmailSubscription:
Type: AWS::SNS::Subscription
Properties:
Protocol: email
TopicArn: !Ref PCSNSNSTopic
Endpoint: [email protected]
################################# NAT Gateway ##########################
NatGatewayEip1:
Type: "AWS::EC2::EIP"
Properties:
Domain: "vpc"
Tags:
- Key: Name
Value: Lab NAT Gateway EIP
NatGateway1:
Type: "AWS::EC2::NatGateway"
Properties:
AllocationId: !GetAtt NatGatewayEip1.AllocationId
SubnetId: !Ref PublicSubnet1
Tags:
- Key: Name
Value: Lab NAT Gateway 1
PrivateRouteTable1:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref PlagiarismCheckSystemVPC
Tags:
- Key: Name
Value: Private Route Table 1
PrivateRoute1:
Type: "AWS::EC2::Route"
Properties:
RouteTableId: !Ref PrivateRouteTable1
DestinationCidrBlock: "0.0.0.0/0"
NatGatewayId: !Ref NatGateway1
PublicSubnetRouteTableAssociation1:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref PrivateSubnet1
RouteTableId: !Ref PrivateRouteTable1
NatGatewayEip2:
Type: "AWS::EC2::EIP"
Properties:
Domain: "vpc"
Tags:
- Key: Name
Value: PSCN NAT Gateway EIP
NatGateway2:
Type: "AWS::EC2::NatGateway"
Properties:
AllocationId: !GetAtt NatGatewayEip2.AllocationId
SubnetId: !Ref PublicSubnet2
Tags:
- Key: Name
Value: PSCN NAT Gateway 2
PrivateRouteTable2:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref PlagiarismCheckSystemVPC
Tags:
- Key: Name
Value: Private Route Table 2
PrivateRoute2:
Type: "AWS::EC2::Route"
Properties:
RouteTableId: !Ref PrivateRouteTable2
DestinationCidrBlock: "0.0.0.0/0"
NatGatewayId: !Ref NatGateway2
PublicSubnetRouteTableAssociation2:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref PrivateSubnet2
RouteTableId: !Ref PrivateRouteTable2
###############################################################################
Outputs:
runtimeLambdaUrl:
Value: !GetAtt RuntimeLambdaUrl.FunctionUrl