-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudformation.yml
586 lines (550 loc) · 17.6 KB
/
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
AWSTemplateFormatVersion: 2010-09-09
Parameters:
BucketName:
Description: Bucket where files will be stored
Type: String
Default: otel-files-service
Mappings:
# AMI: Amazon Linux 2 AMI 64-bit (x86) by region
RegionMap:
ap-south-1:
AMI: ami-019cd93943ccead1a
eu-north-1:
AMI: ami-09d3c9501075e4112
eu-west-3:
AMI: ami-0584a5ab8f4034679
eu-west-2:
AMI: ami-0f9e888df95272f70
eu-west-1:
AMI: ami-0fc56b47fc1f238ee
ap-northeast-3:
AMI: ami-0217f1c25d44bec66
ap-northeast-2:
AMI: ami-07a2318163330ee84
ap-northeast-1:
AMI: ami-0dbca050974482176
ca-central-1:
AMI: ami-01a9d286de9a4d56e
sa-east-1:
AMI: ami-06b07a4baf3024a9f
ap-southeast-1:
AMI: ami-047d5a3391704b8b2
ap-southeast-2:
AMI: ami-0c372b59cfa8c3d65
eu-central-1:
AMI: ami-03b7db59d53c5e228
us-east-1:
AMI: ami-0ac664bd64e1dcc6b
us-east-2:
AMI: ami-05175b461d18d94d9
us-west-1:
AMI: ami-0853d0de3297e47e0
us-west-2:
AMI: ami-06c7fbd87fa7b507c
Resources:
FrontendInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !FindInMap [RegionMap, !Ref AWS::Region, AMI]
InstanceType: t3.micro
Tags:
- Key: Name
Value: otel-frontend
KeyName: !Sub otel-observability-${AWS::Region}
SecurityGroups:
- !Ref FrontendInstanceSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash
sudo yum update -y
echo ">> INSTALLING DOCKER"
sudo yum install -y docker
sudo service docker start
sudo usermod -a -G docker ec2-user
docker --version
echo ">> INSTALLING DOCKER"
sudo yum install -y git
FrontendInstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
Tags:
- Key: App
Value: otel-frontend
- Key: Name
Value: otel-frontend-sg
GroupDescription: "otel-frontend security group"
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
FilesServiceInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !FindInMap [RegionMap, !Ref AWS::Region, AMI]
InstanceType: t3.micro
KeyName: !Sub otel-observability-${AWS::Region}
Tags:
- Key: Name
Value: otel-files-service
IamInstanceProfile: !Ref FilesServiceInstanceProfile
SecurityGroups:
- !Ref FilesServiceInstanceSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash
sudo yum update -y
sudo yum install -y docker
sudo service docker start
sudo usermod -a -G docker ec2-user
docker --version
sudo yum install -y git
FilesServiceInstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
Tags:
- Key: App
Value: otel-files-service
- Key: Name
Value: otel-files-service-sg
GroupDescription: "otel-files-service security group"
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
FilesServiceInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref FilesServiceInstanceRole
FilesServiceInstanceRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub otel-observability-files-service-role-${AWS::Region}
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: S3AccessPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- s3:AbortMultipartUpload
- s3:ListMultipartUploadParts
- s3:ListBucketMultipartUploads
- s3:PutObject
- s3:GetObject
Resource:
- !Sub arn:aws:s3:::${Bucket}/*
- !Sub arn:aws:s3:::${Bucket}
- PolicyName: DynamoDBAccessPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
- dynamodb:Query
Resource:
- !GetAtt DynamoFilesTable.Arn
- !Join [ "/", [ !GetAtt DynamoFilesTable.Arn, "*" ] ]
- PolicyName: SQSAccessPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- sqs:SendMessage
Resource:
- !GetAtt FilesQueue.Arn
AuthServiceInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !FindInMap [RegionMap, !Ref AWS::Region, AMI]
InstanceType: t3.micro
KeyName: !Sub otel-observability-${AWS::Region}
IamInstanceProfile: !Ref AuthServiceInstanceProfile
Tags:
- Key: Name
Value: otel-auth-service
SecurityGroups:
- !Ref AuthServiceInstanceSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash
sudo yum update -y
sudo yum install -y docker
sudo service docker start
sudo usermod -a -G docker ec2-user
docker --version
sudo yum install -y git
AuthServiceInstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
Tags:
- Key: App
Value: otel-auth-service
- Key: Name
Value: otel-auth-service-sg
GroupDescription: "otel-auth-service security group"
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
AuthServiceInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref AuthServiceInstanceRole
AuthServiceInstanceRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub otel-observability-auth-service-role-${AWS::Region}
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: DynamoDBAccessPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
- dynamodb:Scan
Resource:
- !GetAtt DynamoAuthTable.Arn
- !Join [ "/", [ !GetAtt DynamoAuthTable.Arn, "*" ] ]
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
CorsConfiguration:
CorsRules:
- AllowedHeaders: ['*']
AllowedMethods: [PUT]
AllowedOrigins: [!Sub 'http://${FrontendInstance.PublicIp}']
ExposedHeaders: [ETag]
MaxAge: 3600 # 1 hour (to avoid cors preflight requests)
DynamoAuthTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: otel-observability-auth
AttributeDefinitions:
- AttributeName: token
AttributeType: S
KeySchema:
- AttributeName: token
KeyType: HASH
BillingMode: PAY_PER_REQUEST
Tags:
- Key: Name
Value: otel-observability-auth
DynamoFilesTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: otel-observability-files
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: data_type
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH # Partition key
GlobalSecondaryIndexes:
- IndexName: creation_datetime-index
KeySchema:
# data_type is an artificial attribute to "hack" dynamodb
# because its required add a has key
- AttributeName: data_type
KeyType: HASH
# id is a ULID what is a string of 26 characters that can be ordered
- AttributeName: id
KeyType: RANGE
Projection:
ProjectionType: ALL # All attributes in table
BillingMode: PAY_PER_REQUEST
Tags:
- Key: Name
Value: otel-observability-files
FilesQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: otel-observability-files-queue
VisibilityTimeout: 900 # 15 minutes
PipelineFunctionRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub otel-observability-pipeline-function-role-${AWS::Region}
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: SQSAccessPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- sqs:ReceiveMessage
- sqs:DeleteMessage
- sqs:GetQueueAttributes
Resource: !GetAtt FilesQueue.Arn
- PolicyName: DynamoDBAccessPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:UpdateItem
Resource: !GetAtt DynamoFilesTable.Arn
- PolicyName: RedshiftServerlessAccessPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- redshift-data:ExecuteStatement
- redshift-serverless:GetCredentials
- redshift-data:GetStatementResult
- redshift-data:DescribeStatement
- redshift-data:ListStatements
Resource: '*'
PipelineFunctionLogs:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: otel-observability-pipeline-function-logs
PipelineFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: otel-observability-pipeline-function
Handler: src.app.main
Code:
ZipFile: |
# this code will be replaced after, it is just to create de lambda function
def main(event, context):
print("empty lambda function")
Role: !GetAtt PipelineFunctionRole.Arn
Runtime: python3.10
Timeout: 900 # 15 minutes
LoggingConfig:
LogGroup: !Ref PipelineFunctionLogs
LogFormat: Text
PipelineLambdaSQSPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref PipelineFunction
Action: "lambda:InvokeFunction"
Principal: "sqs.amazonaws.com"
SourceArn: !GetAtt FilesQueue.Arn
PipelineCodeBuildRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub otel-observability-pipeline-codebuild-role-${AWS::Region}
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: LoadPipelineFunctionPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- lambda:UpdateFunctionCode
- lambda:GetFunction
- lambda:UpdateFunctionConfiguration
- lambda:GetFunctionConfiguration
Resource:
- !GetAtt PipelineFunction.Arn
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- "*"
PipelineCodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Name: otel-observability-pipeline-codebuild-project
Source:
Type: GITHUB
Location: https://github.com/CrissAlvarezH/otel-observability.git
BuildSpec: apps/load-pipeline/buildspec.yml
ServiceRole: !GetAtt PipelineCodeBuildRole.Arn
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:5.0
Type: LINUX_CONTAINER
EnvironmentVariables:
- Name: AWS_REGION
Value: !Ref AWS::Region
- Name: S3_BUCKET_NAME
Value: !Ref Bucket
- Name: LAMBDA_FUNCTION_NAME
Value: !Ref PipelineFunction
- Name: REDSHIFT_WORKGROUP
Value: !Ref RedshiftServerlessWorkgroup
- Name: REDSHIFT_DATABASE
Value: otel-observability
- Name: OTLP_COLLECTOR_ENDPOINT
Value: !Sub "http://${ObservabilityBackendInstance.PublicIp}:4318/v1/traces"
Artifacts:
Type: NO_ARTIFACTS
TimeoutInMinutes: 15
SQSEventSourceMapping:
Type: AWS::Lambda::EventSourceMapping
Properties:
BatchSize: 5 # number of messages to process per invocation
EventSourceArn: !GetAtt FilesQueue.Arn
FunctionName: !Ref PipelineFunction
Enabled: True
RedshiftServerlessNamespace:
Type: AWS::RedshiftServerless::Namespace
Properties:
NamespaceName: "otel-observability-namespace"
AdminUsername: "otel"
AdminUserPassword: "OtelObservability123"
DbName: "otel-observability"
DefaultIamRoleArn: !GetAtt RedshiftServerlessIamRole.Arn
IamRoles:
- !GetAtt RedshiftServerlessIamRole.Arn
RedshiftServerlessWorkgroup:
Type: AWS::RedshiftServerless::Workgroup
Properties:
WorkgroupName: "otel-observability-workgroup"
NamespaceName: !Ref RedshiftServerlessNamespace
BaseCapacity: 8
PubliclyAccessible: true # to allow copy from s3
EnhancedVpcRouting: true
RedshiftServerlessIamRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub otel-observability-redshift-serverless-role-${AWS::Region}
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- redshift-serverless.amazonaws.com
- redshift.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: "RedshiftServerlessS3Access"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:ListBucket
Resource:
- !Sub arn:aws:s3:::${Bucket}/*
- !Sub arn:aws:s3:::${Bucket}
ObservabilityBackendInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !FindInMap [RegionMap, !Ref AWS::Region, AMI]
InstanceType: t3.medium
KeyName: !Sub otel-observability-${AWS::Region}
Tags:
- Key: Name
Value: observability-backend-service
SecurityGroups:
- !Ref ObservabilityBackendInstanceSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash
sudo yum update -y
sudo yum install -y docker
sudo service docker start
sudo usermod -a -G docker ec2-user
docker --version
sudo yum install -y git
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
ObservabilityBackendInstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
Tags:
- Key: Name
Value: observability-backend-sg
GroupDescription: "observability-backend-service security group"
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 16686
ToPort: 16686
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 4317
ToPort: 4317
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 4318
ToPort: 4318
CidrIp: 0.0.0.0/0
Outputs:
FrontendInstancePublicIp:
Description: Public IP of the frontend instance
Value: !GetAtt FrontendInstance.PublicIp
FilesServiceInstancePublicIp:
Description: Public IP of the files service instance
Value: !GetAtt FilesServiceInstance.PublicIp
AuthServiceInstancePublicIp:
Description: Public IP of the auth service instance
Value: !GetAtt AuthServiceInstance.PublicIp
ObservabilityBackendInstancePublicIp:
Description: Public IP of the observability backend instance
Value: !GetAtt ObservabilityBackendInstance.PublicIp
FilesQueueUrl:
Description: URL of the files queue
Value: !GetAtt FilesQueue.QueueUrl