-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf.yml
511 lines (459 loc) · 14.5 KB
/
cf.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
Parameters:
CognitoIdentityPoolId:
Type: String
Description: the _pre-existing_ id for a cognito identity pool
GoogleClientID:
Type: String
Description: the _pre-existing_ id for google oauth
GoogleMapsAPIKey:
Type: String
Description: the _pre-existing_ google maps api key
HostedZoneName:
Type: String
Description: Route53 hosted zone domain name e.g. yourdomain.com (don't include trailing '.')
WebsiteSubDomain:
Type: String
Description: subdomain for frontend website
UbuntuAMIParameter:
Type: String
Default: ami-ae6259cd
Description: Ubuntu 16.04 AMI (default is ap-southeast-2 image) Choose relevant ubuntu image for your region.
SpatialServicesSubDomain:
Type: String
Description: sub domain which API Gateway will proxy to
LetsencryptEmail:
Type: String
Description: email address for letsencrypt cert registration purposes
KeyPairParameter:
Type: AWS::EC2::KeyPair::KeyName
Description: Keypair for EC2 instance
Mappings:
HostedZoneIdMap:
us-east-1:
hostedZoneid: Z3AQBSTGFYJSTF
us-east-2:
hostedZoneid: Z2O1EMRO9K5GLX
us-west-1:
hostedZoneid: Z2F56UZL2M1ACD
us-west-2:
hostedZoneid: Z3BJ6K6RIION7M
ca-central-1:
hostedZoneid: Z1QDHH18159H29
ap-south-1:
hostedZoneid: Z11RGJOFQNVJUP
ap-northeast-2:
hostedZoneid: Z3W03O7B5YMIYP
ap-southeast-1:
hostedZoneid: Z3O0J2DXBE1FTB
ap-southeast-2:
hostedZoneid: Z1WCIGYICN2BYD
ap-northeast-1:
hostedZoneid: Z2M4EHUR26P7ZW
eu-central-1:
hostedZoneid: Z21DNDUVLTQW6Q
eu-west-1:
hostedZoneid: Z1BKCTXD74EZPE
eu-west-2:
hostedZoneid: Z3GKZC51ZF0DB4
sa-east-1:
hostedZoneid: Z7KQH4QJS55SO
Resources:
VPC:
Type: "AWS::EC2::VPC"
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsSupport: true
EnableDnsHostnames: true
InstanceTenancy: default
Tags:
- Key: Name
Value: !Join [ "-", [ !Ref "AWS::StackName", vpc ] ]
InternetGateway:
Type: "AWS::EC2::InternetGateway"
Properties:
Tags:
- Key: Name
Value: !Join [ "-", [ !Ref "AWS::StackName", ig ] ]
VPCGatewayAttachment:
Type: "AWS::EC2::VPCGatewayAttachment"
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC
PublicSubnet:
Type: "AWS::EC2::Subnet"
Properties:
AvailabilityZone: ap-southeast-2a
CidrBlock: 10.0.1.0/24
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Join [ "-", [ !Ref "AWS::StackName", public-subnet ] ]
VpcId: !Ref VPC
PublicRouteTable:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Join [ "-", [ !Ref "AWS::StackName", public ] ]
PublicRouteTableAssoc:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet
IGRoute:
DependsOn: VPCGatewayAttachment
Type: "AWS::EC2::Route"
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
RouteTableId: !Ref PublicRouteTable
EC2SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: !Join [ "-", [ !Ref "AWS::StackName", sg ] ]
SecurityGroupIngress:
-
IpProtocol: "tcp"
FromPort: "22"
ToPort: "22"
CidrIp: "0.0.0.0/0"
-
IpProtocol: "tcp"
FromPort: "5000"
ToPort: "5000"
CidrIp: "0.0.0.0/0"
-
IpProtocol: "tcp"
FromPort: "443"
ToPort: "443"
CidrIp: "0.0.0.0/0"
-
IpProtocol: "tcp"
FromPort: "80"
ToPort: "80"
CidrIp: "0.0.0.0/0"
VpcId:
!Ref VPC
EC2Role:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: "Allow"
Principal:
Service:
- "ec2.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
-
PolicyName: "inline-policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- apigateway:GET
Resource: !Sub arn:aws:apigateway:${AWS::Region}::/clientcertificates/${APIGClientCertificate}
EC2InstanceProfile:
Type: "AWS::IAM::InstanceProfile"
Properties:
Roles:
- !Ref EC2Role
SpatialServicesEC2:
DependsOn:
- APIGClientCertificate
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.nano
ImageId:
Ref: UbuntuAMIParameter
KeyName:
Ref: KeyPairParameter
Tags:
- Key: Name
Value: !Join [ "-", [ !Ref "AWS::StackName", spatial-ec2 ] ]
SecurityGroupIds:
- !Ref EC2SecurityGroup
IamInstanceProfile: !Ref EC2InstanceProfile
SubnetId: !Ref PublicSubnet
UserData:
Fn::Base64: !Sub |
#!/bin/bash
export APIG_CLIENT_CERT=${APIGClientCertificate}
export FQDN=${SpatialServicesSubDomain}.${HostedZoneName}
export EMAIL=${LetsencryptEmail}
export AWS_DEFAULT_REGION=${AWS::Region}
echo $FQDN
echo $EMAIL
apt-get update
apt-get install -y git
apt-get install -y postgresql-client
git clone https://github.com/gsat-technology/termini
chmod u+x ./termini/ec2-conf/*.sh
./termini/ec2-conf/install-docker.sh
./termini/ec2-conf/install-nginx.sh $FQDN $EMAIL
docker-compose -f ./termini/docker-compose.yml up --build -d
export PGPASSWORD=docker
psql -U docker -h localhost -p 5432 postgres -c "CREATE DATABASE spatial;"
#previous command may not work if postgis not fully in service
#below while loop keeps trying until it works
while [ $? -ne 0 ]; do sleep 1 ; psql -U docker -h localhost -p 5432 postgres -c "CREATE DATABASE spatial;" ; done
psql -U docker -h localhost -p 5432 spatial -f ./termini/files/spatial.dump.sql
#configure api gateway client certificate
pip install awscli
apt-get install -y jq
mkdir /etc/nginx/apig
aws apigateway get-client-certificate --client-certificate-id $APIG_CLIENT_CERT \
| jq .pemEncodedCertificate --raw-output > /etc/nginx/apig/client.crt
service nginx restart
EC2DNSRecord:
Type: "AWS::Route53::RecordSet"
Properties:
HostedZoneName: !Join [ ".", [ !Ref HostedZoneName, "" ] ]
Name: !Join [ ".", [ !Ref SpatialServicesSubDomain, !Ref HostedZoneName ] ]
ResourceRecords:
- !GetAtt SpatialServicesEC2.PublicIp
TTL: 300
Type: A
WebsiteDNS:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneName: !Sub ${HostedZoneName}.
Comment: alias pointing to website-enabled s3 bucket
RecordSets:
- Name: !Sub ${PublicWebsiteS3Bucket}.
Type: A
AliasTarget:
#AWS does not document very well what value is needed for HostedZoneId
#find ids in table called 'Amazon Simple Storage Service Website Endpoints'
#on this page: http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
HostedZoneId: !FindInMap [HostedZoneIdMap, !Ref "AWS::Region", hostedZoneid]
DNSName: !Sub s3-website-${AWS::Region}.amazonaws.com
APIGateway:
Type: "AWS::ApiGateway::RestApi"
Properties:
Name:
!Join [ "_", [ !Ref "AWS::StackName", APIG ] ]
APIGTerminiResource:
Type: "AWS::ApiGateway::Resource"
Properties:
ParentId:
!GetAtt APIGateway.RootResourceId
PathPart: termini
RestApiId: !Ref APIGateway
TerminiResourceGET:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: AWS_IAM
HttpMethod: GET
ResourceId:
!Ref APIGTerminiResource
RestApiId:
!Ref APIGateway
Integration:
IntegrationHttpMethod: GET
Type: HTTP_PROXY
Uri: !Sub https://${SpatialServicesSubDomain}.${HostedZoneName}/termini
TerminiResourceOPTIONS:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
RestApiId: !Ref APIGateway
ResourceId: !Ref APIGTerminiResource
HttpMethod: OPTIONS
Integration:
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Methods: "'GET'"
method.response.header.Access-Control-Allow-Origin: "'*'"
ResponseTemplates:
application/json: ''
RequestTemplates:
application/json: '{"statusCode": 200}'
Type: MOCK
MethodResponses:
- StatusCode: 200
ResponseModels:
application/json: 'Empty'
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: false
method.response.header.Access-Control-Allow-Methods: false
method.response.header.Access-Control-Allow-Origin: false
#Note this resource is required even though it will
#actually create a stage called 'dummystage' which won't
#be used. Some info from AWS forum here:
#https://forums.aws.amazon.com/thread.jspa?messageID=737087
APIGDeploymentProd:
DependsOn:
- APIGTerminiResource
- TerminiResourceGET
Type: "AWS::ApiGateway::Deployment"
Properties:
RestApiId:
!Ref APIGateway
StageName: dummystage
APIGStage:
Type: "AWS::ApiGateway::Stage"
Properties:
ClientCertificateId: !Ref APIGClientCertificate
DeploymentId: !Ref APIGDeploymentProd
RestApiId: !Ref APIGateway
StageName: LATEST
MethodSettings:
-
LoggingLevel: INFO
DataTraceEnabled: "true"
#does all resource and ticks all
#the boxes at the stage level
ResourcePath: /*
HttpMethod: "*"
#or use the below to target the 'termini' resource
#ResourcePath: /~1termini
MetricsEnabled: "true"
APIGClientCertificate:
Type: "AWS::ApiGateway::ClientCertificate"
Properties:
Description: !Sub ${AWS::Region}-client-cert
APIGAccount:
Type: "AWS::ApiGateway::Account"
Properties:
CloudWatchRoleArn: !GetAtt APIGCloudWatchLogsRole.Arn
APIGCloudWatchLogsRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: "Allow"
Principal:
Service:
- "apigateway.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
-
PolicyName: "inline-policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:DescribeLogGroups
- logs:DescribeLogStreams
- logs:PutLogEvents
- logs:GetLogEvents
- logs:FilterLogEvents
Resource: "*"
PublicWebsiteS3Bucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: LogDeliveryWrite
BucketName: !Sub ${WebsiteSubDomain}.${HostedZoneName}
WebsiteConfiguration:
IndexDocument: index.html
LoggingConfiguration:
LogFilePrefix: logs/
DeletionPolicy: Delete
PublicWebsitePolicy:
Type: "AWS::S3::BucketPolicy"
Properties:
Bucket: !Ref PublicWebsiteS3Bucket
PolicyDocument:
Statement:
-
Action:
- s3:GetObject
Effect: "Allow"
Resource:
!Join [ "", [ "arn:aws:s3:::", !Ref PublicWebsiteS3Bucket, "/*" ] ]
#NotResource:
# !Join [ "", [ "arn:aws:s3:::", !Ref PublicWebsiteS3Bucket, "/logs" ] ]
Principal: "*"
CognitoAuthedRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: "Allow"
Principal:
Federated:
- "cognito-identity.amazonaws.com"
Action:
- "sts:AssumeRoleWithWebIdentity"
Condition:
StringEquals:
cognito-identity.amazonaws.com:aud: !Ref CognitoIdentityPoolId
ForAnyValue:StringLike:
cognito-identity.amazonaws.com:amr: authenticated
Path: "/"
Policies:
-
PolicyName: "inline-policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- execute-api:Invoke
Resource: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${APIGateway}/LATEST/GET/termini
# LogGroupAPIGBase:
# Type: "AWS::Logs::LogGroup"
# Properties:
# LogGroupName: !Sub API-Gateway-Execution-Logs_${APIGateway}
# RetentionInDays: 1
#
# LogGroupAPIGLatest:
# Type: "AWS::Logs::LogGroup"
# Properties:
# LogGroupName: !Sub API-Gateway-Execution-Logs_${APIGateway}/LATEST
# RetentionInDays: 1
#
# LogGroupAPIGDummyStage:
# Type: "AWS::Logs::LogGroup"
# Properties:
# LogGroupName: !Sub API-Gateway-Execution-Logs_${APIGateway}/dummystage
# RetentionInDays: 1
Outputs:
APIGEndpoint:
Description: APIG endpoint
Value:
!Sub https://${APIGateway}.execute-api.${AWS::Region}.amazonaws.com/LATEST
APIGID:
Description: APIG Id
Value:
!Ref APIGateway
CognitoAuthedRoleArn:
Description: Cognito authed role arn
Value:
!GetAtt CognitoAuthedRole.Arn
PublicWebsiteS3Bucket:
Description: Public website bucket name
Value: !Ref PublicWebsiteS3Bucket
CognitoIdentityPoolId:
Description: cognito identity pool id
Value: !Ref CognitoIdentityPoolId
GoogleClientID:
Description: google oauth client id
Value: !Ref GoogleClientID
GoogleMapsAPIKey:
Description: google maps api key
Value: !Ref GoogleMapsAPIKey
Region:
Description: aws region of this deployment
Value: !Ref AWS::Region