-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudformation.yaml
337 lines (321 loc) · 10.7 KB
/
cloudformation.yaml
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
AWSTemplateFormatVersion: "2010-09-09"
Description: Enable streaming Amazon Connect calls to Deepgram
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: "General Configuration"
Parameters:
- deepgramApi
- deepgramApiKey
- vpcId
- subnets
- Label:
default: "Trigger Lambda"
Parameters:
- kvsDgTriggerImage
- kvsDgTriggerLogLevel
- Label:
default: "Integrator ECS Service"
Parameters:
- kvsDgIntegratorImage
- kvsDgIntegratorLogLevel
- kvsDgIntegratorDesiredTaskCount
- kvsDgIntegratorTaskCpu
- kvsDgIntegratorTaskMemory
ParameterLabels:
deepgramApi:
default: Deepgram API
deepgramApiKey:
default: Deepgram API Key
vpcId:
default: VPC ID
subnets:
default: Subnets
kvsDgTriggerImage:
default: Image URI
kvsDgTriggerLogLevel:
default: Log Level
kvsDgIntegratorImage:
default: Task Image URI
kvsDgIntegratorLogLevel:
default: Log Level
kvsDgIntegratorDesiredTaskCount:
default: Desired Task Count
kvsDgIntegratorTaskCpu:
default: Task CPU
kvsDgIntegratorTaskMemory:
default: Task Memory
Parameters:
deepgramApi:
Description: The Deepgram endpoint to stream audio to (change this if you're using Deepgram onprem)
Type: String
Default: "wss://api.deepgram.com/v1/listen"
deepgramApiKey:
Description: Your Deepgram API key
Type: String
NoEcho: "true"
AllowedPattern: "^[a-z0-9]{40}$"
ConstraintDescription: "Deepgram API key must be 40 alphanumeric characters (lowercase only)"
vpcId:
Description: VPC where the resources will be deployed
Type: AWS::EC2::VPC::Id
subnets:
Description: Subnets where the resources will be deployed
Type: List<AWS::EC2::Subnet::Id>
kvsDgTriggerImage:
Description: >
Docker image of the trigger lambda. Before running this CloudFormation template, build the
image and push it to ECR in the current region. Then set this field to the image you pushed.
Type: String
Default: "123456789012.dkr.ecr.region.amazonaws.com/image:tag"
kvsDgTriggerLogLevel:
Description: Logging level for the trigger lambda
Type: String
Default: "info"
AllowedValues:
- "debug"
- "info"
- "warning"
- "error"
kvsDgIntegratorImage:
Description: >
Docker image of the integrator task. Before running this CloudFormation template, build the
image and push it to ECR in the current region. Then set this field to the image you pushed.
Type: String
Default: "123456789012.dkr.ecr.region.amazonaws.com/image:tag"
kvsDgIntegratorLogLevel:
Description: Logging level for the integrator task
Type: String
Default: "info"
AllowedValues:
- "trace"
- "debug"
- "info"
- "warn"
- "error"
kvsDgIntegratorDesiredTaskCount:
Description: >
The number of integrator tasks that should be running in the ECS service. The load balancer
will distribute sessions evenly between these tasks.
Type: Number
Default: "1"
kvsDgIntegratorTaskCpu:
Description: >
The number of CPU units to allot to each integrator task. The value you choose determines the
range of valid values for the memory. See valid cpu/memory combinations here:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-taskdefinition.html#cfn-ecs-taskdefinition-cpu
Type: String
Default: "256"
AllowedValues:
- "256"
- "512"
- "1024"
- "2048"
- "4096"
- "8192"
- "16384"
kvsDgIntegratorTaskMemory:
Description: >
The amount of memory (in MiB) to allot to each integrator task. The value you choose determines the
range of valid values for the CPU. See valid cpu/memory combinations here:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-taskdefinition.html#cfn-ecs-taskdefinition-cpu
Type: String
Default: "1024"
Resources:
kvsDgTriggerRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
kvsDgTriggerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for kvs-dg-trigger Lambda function
VpcId: !Ref vpcId
SecurityGroupEgress:
- CidrIp: 0.0.0.0/0
Description: Allow all outbound traffic
IpProtocol: "-1"
kvsDgTrigger:
Type: "AWS::Lambda::Function"
Properties:
Role: !GetAtt kvsDgTriggerRole.Arn
Timeout: 900
Environment:
Variables:
KVS_DG_INTEGRATOR_DOMAIN: !GetAtt kvsDgIntegratorLoadBalancer.DNSName
LOG_LEVEL: !Ref kvsDgTriggerLogLevel
PackageType: "Image"
Code:
ImageUri: !Ref kvsDgTriggerImage
VpcConfig:
SecurityGroupIds:
- !Ref kvsDgTriggerSecurityGroup
SubnetIds: !Ref subnets
kvsDgIntegratorEcsCluster:
Type: "AWS::ECS::Cluster"
Properties:
ClusterName: kvs-dg-integrator-cluster
ClusterSettings:
- Name: containerInsights
Value: "disabled" # If you are load testing, setting this to "enabled" can reveal useful info
kvsDgIntegratorExecutionRole:
Type: "AWS::IAM::Role"
Properties:
RoleName: kvsDgIntegratorExecutionRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- ecs-tasks.amazonaws.com
Action:
- "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
Policies:
- PolicyName: "CloudWatchLogsCreateLogGroup"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "logs:CreateLogGroup"
Resource: "*"
kvsDgIntegratorTaskRole:
Type: "AWS::IAM::Role"
Properties:
RoleName: kvsDgIntegratorTaskRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- ecs-tasks.amazonaws.com
Action:
- "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/AmazonKinesisVideoStreamsReadOnlyAccess"
kvsDgIntegratorTaskDefinition:
Type: "AWS::ECS::TaskDefinition"
Properties:
ContainerDefinitions:
- Name: kvs-dg-integrator-container
Image: !Ref kvsDgIntegratorImage
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-stream-prefix: ecs
awslogs-region: !Ref "AWS::Region"
awslogs-group: kvs-dg-integrator
awslogs-create-group: "true"
Environment:
- Name: DEEPGRAM_API
Value: !Ref deepgramApi
- Name: DEEPGRAM_API_KEY
Value: !Ref deepgramApiKey
- Name: LOG_LEVEL
Value: !Ref kvsDgIntegratorLogLevel
- Name: APP_REGION
Value: !Ref "AWS::Region"
PortMappings:
- HostPort: 80
ContainerPort: 80
Protocol: tcp
AppProtocol: http
Cpu: !Ref kvsDgIntegratorTaskCpu
Memory: !Ref kvsDgIntegratorTaskMemory
ExecutionRoleArn: !GetAtt kvsDgIntegratorExecutionRole.Arn
TaskRoleArn: !GetAtt kvsDgIntegratorTaskRole.Arn
Family: kvs-dg-integrator-task-family
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
RuntimePlatform:
OperatingSystemFamily: LINUX
kvsDgIntegratorEcsServiceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for kvs-dg-integrator Fargate task
VpcId: !Ref vpcId
SecurityGroupEgress:
- CidrIp: 0.0.0.0/0
Description: Allow all outbound traffic
IpProtocol: "-1"
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
SourceSecurityGroupId: !Ref kvsDgIntegratorLoadBalancerSecurityGroup
kvsDgIntegratorEcsService:
Type: AWS::ECS::Service
DependsOn: kvsDgIntegratorLoadBalancerListener
Properties:
Cluster: !Ref kvsDgIntegratorEcsCluster
TaskDefinition: !Ref kvsDgIntegratorTaskDefinition
DesiredCount: !Ref kvsDgIntegratorDesiredTaskCount
LaunchType: FARGATE
NetworkConfiguration:
AwsvpcConfiguration:
# Change to DISABLED if you're using private subnets that have access to a NAT gateway
AssignPublicIp: ENABLED
Subnets: !Ref subnets
SecurityGroups:
- !Ref kvsDgIntegratorEcsServiceSecurityGroup
LoadBalancers:
- TargetGroupArn: !Ref kvsDgIntegratorLoadBalancerTargetGroup
ContainerName: kvs-dg-integrator-container
ContainerPort: 80
kvsDgIntegratorLoadBalancerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for kvs-dg-integrator load balancer
VpcId: !Ref vpcId
SecurityGroupEgress:
- CidrIp: 0.0.0.0/0
Description: Allow all outbound traffic
IpProtocol: "-1"
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
SourceSecurityGroupId: !Ref kvsDgTriggerSecurityGroup
kvsDgIntegratorLoadBalancer:
Type: "AWS::ElasticLoadBalancingV2::LoadBalancer"
Properties:
Scheme: internal
Subnets: !Ref subnets
SecurityGroups: [!Ref kvsDgIntegratorLoadBalancerSecurityGroup]
kvsDgIntegratorLoadBalancerTargetGroup:
Type: "AWS::ElasticLoadBalancingV2::TargetGroup"
Properties:
VpcId: !Ref vpcId
Port: 80
Protocol: "HTTP"
TargetType: "ip"
HealthCheckIntervalSeconds: 30
HealthCheckPath: "/health-check"
HealthCheckTimeoutSeconds: 5
HealthyThresholdCount: 2
UnhealthyThresholdCount: 2
kvsDgIntegratorLoadBalancerListener:
Type: "AWS::ElasticLoadBalancingV2::Listener"
Properties:
DefaultActions:
- TargetGroupArn: !Ref kvsDgIntegratorLoadBalancerTargetGroup
Type: "forward"
LoadBalancerArn: !Ref kvsDgIntegratorLoadBalancer
Port: 80
Protocol: "HTTP"