-
Notifications
You must be signed in to change notification settings - Fork 7
/
ecs-task.cfndsl.rb
344 lines (281 loc) · 12.2 KB
/
ecs-task.cfndsl.rb
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
CloudFormation do
export = external_parameters.fetch(:task_export_name, nil)
export = external_parameters.fetch(:export_name, external_parameters[:component_name]) if export.nil?
task_tags = []
task_tags << { Key: "Name", Value: external_parameters[:component_name] }
task_tags << { Key: "Environment", Value: Ref("EnvironmentName") }
task_tags << { Key: "EnvironmentType", Value: Ref("EnvironmentType") }
tags = external_parameters.fetch(:tags, [])
tags.each do |key,value|
task_tags << { Key: FnSub(key), Value: FnSub(value) }
end
log_retention = external_parameters.fetch(:log_retention, 7)
log_group_name = external_parameters.fetch(:log_group_name, Ref('AWS::StackName'))
Logs_LogGroup('LogGroup') {
LogGroupName log_group_name
RetentionInDays log_retention
}
definitions, task_volumes, secrets = Array.new(4){[]}
secrets_policy = {}
task_definition = external_parameters.fetch(:task_definition, {})
task_definition.each do |task_name, task|
env_vars, mount_points, ports = Array.new(3){[]}
name = task.has_key?('name') ? task['name'] : task_name
image_repo = task.has_key?('repo') ? "#{task['repo']}/" : ''
image_name = task.has_key?('image') ? task['image'] : task_name
image_tag = task.has_key?('tag') ? "#{task['tag']}" : 'latest'
image_tag = task.has_key?('tag_param') ? Ref("#{task['tag_param']}") : image_tag
# create main definition
task_def = {
Name: name,
Image: FnJoin('', [FnSub("#{image_repo}#{image_name}"), ":", image_tag]),
LogConfiguration: {
LogDriver: 'awslogs',
Options: {
'awslogs-group' => Ref("LogGroup"),
"awslogs-region" => Ref("AWS::Region"),
"awslogs-stream-prefix" => name
}
}
}
if task.has_key?('log_pattern')
task_def[:LogConfiguration][:Options]["awslogs-multiline-pattern"] = task['log_pattern']
end
task_def.merge!({ MemoryReservation: task['memory'] }) if task.has_key?('memory')
task_def.merge!({ Cpu: task['cpu'] }) if task.has_key?('cpu')
task_def.merge!({ Ulimits: task['ulimits'] }) if task.has_key?('ulimits')
task_def.merge!({ StartTimeout: task['start_timeout'] }) if task.has_key?('start_timeout')
task_def.merge!({ StopTimeout: task['stop_timeout'] }) if task.has_key?('stop_timeout')
if !(task['env_vars'].nil?)
task['env_vars'].each do |name,value|
split_value = value.to_s.split(/\${|}/)
if split_value.include? 'environment'
fn_join = split_value.map { |x| x == 'environment' ? [ Ref('EnvironmentName'), '.', FnFindInMap('AccountId',Ref('AWS::AccountId'),'DnsDomain') ] : x }
env_value = FnJoin('', fn_join.flatten)
elsif value == 'cf_version'
env_value = cf_version
else
env_value = value
end
env_vars << { Name: name, Value: env_value}
end
end
task_def.merge!({Environment: env_vars }) if env_vars.any?
# add links
if task.key?('links')
task['links'].each do |links|
task_def.merge!({ Links: [ links ] })
end
end
# add entrypoint
if task.key?('entrypoint')
task['entrypoint'].each do |entrypoint|
task_def.merge!({ EntryPoint: entrypoint })
end
end
# By default Essential is true, switch to false if `not_essential: true`
task_def.merge!({ Essential: false }) if task['not_essential']
# add docker volumes
if task.key?('mounts')
task['mounts'].each do |mount|
if mount.is_a? String
parts = mount.split(':',2)
mount_points << { ContainerPath: FnSub(parts[0]), SourceVolume: FnSub(parts[1]), ReadOnly: (parts[2] == 'ro' ? true : false) }
else
mount_points << mount
end
end
task_def.merge!({MountPoints: mount_points })
end
# add volumes from
volumes_from = []
if task.key?('volumes_from')
if task['volumes_from'].kind_of?(Array)
task['volumes_from'].each do |source_container|
volumes_from << { SourceContainer: source_container }
end
task_def.merge!({ VolumesFrom: volumes_from })
end
end
# add port
if task.key?('ports')
port_mapppings = []
task['ports'].each do |port|
port_array = port.to_s.split(":").map(&:to_i)
mapping = {}
mapping.merge!(ContainerPort: port_array[0])
mapping.merge!(HostPort: port_array[1]) if port_array.length == 2
port_mapppings << mapping
end
task_def.merge!({PortMappings: port_mapppings})
end
# add DependsOn
# The dependencies defined for container startup and shutdown. A container can contain multiple dependencies. When a dependency is defined for container startup, for container shutdown it is reversed.
# For tasks using the EC2 launch type, the container instances require at least version 1.3.0 of the container agent to enable container dependencies
depends_on = []
if !(task['depends_on'].nil?)
task['depends_on'].each do |name,value|
depends_on << { ContainerName: name, Condition: value}
end
end
linux_parameters = {}
if task.key?('cap_add')
linux_parameters[:Capabilities] = {Add: task['cap_add']}
end
if task.key?('cap_drop')
if linux_parameters.key?(:Capabilities)
linux_parameters[:Capabilities][:Drop] = task['cap_drop']
else
linux_parameters[:Capabilities] = {Drop: task['cap_drop']}
end
end
if task.key?('init')
linux_parameters[:InitProcessEnabled] = task['init']
end
if task.key?('memory_swap')
linux_parameters[:MaxSwap] = task['memory_swap'].to_i
end
if task.key?('shm_size')
linux_parameters[:SharedMemorySize] = task['shm_size'].to_i
end
if task.key?('memory_swappiness')
linux_parameters[:Swappiness] = task['memory_swappiness'].to_i
end
task_def.merge!({LinuxParameters: linux_parameters}) if linux_parameters.any?
task_def.merge!({EntryPoint: task['entrypoint'] }) if task.key?('entrypoint')
task_def.merge!({Command: task['command'] }) if task.key?('command')
task_def.merge!({HealthCheck: task['healthcheck'] }) if task.key?('healthcheck')
task_def.merge!({WorkingDirectory: task['working_dir'] }) if task.key?('working_dir')
task_def.merge!({Privileged: task['privileged'] }) if task.key?('privileged')
task_def.merge!({User: task['user'] }) if task.key?('user')
task_def.merge!({DependsOn: depends_on }) if depends_on.length > 0
task_def.merge!({ ExtraHosts: task['extra_hosts'] }) if task.has_key?('extra_hosts')
if task.key?('secrets')
if task['secrets'].key?('ssm')
secrets.push *task['secrets']['ssm'].map {|k,v| { Name: k, ValueFrom: v.is_a?(String) && v.start_with?('/') ? FnSub("arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter#{v}") : v }}
resources = task['secrets']['ssm'].map {|k,v| v.is_a?(String) && v.start_with?('/') ? FnSub("arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter#{v}") : v }
secrets_policy['ssm-secrets'] = {
'action' => 'ssm:GetParameters',
'resource' => resources
}
task['secrets'].reject! { |k| k == 'ssm' }
end
if task['secrets'].key?('secretsmanager')
secrets.push *task['secrets']['secretsmanager'].map {|k,v| { Name: k, ValueFrom: v.is_a?(String) && v.start_with?('/') ? FnSub("arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:#{v}") : v }}
resources = task['secrets']['secretsmanager'].map {|k,v| v.is_a?(String) && v.start_with?('/') ? FnSub("arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:#{v}-*") : v }
secrets_policy['secretsmanager'] = {
'action' => 'secretsmanager:GetSecretValue',
'resource' => resources
}
task['secrets'].reject! { |k| k == 'secretsmanager' }
end
unless task['secrets'].empty?
secrets.push *task['secrets'].map {|k,v| { Name: k, ValueFrom: v.is_a?(String) && v.start_with?('/') ? FnSub("arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter#{v}") : v }}
resources = task['secrets'].map {|k,v| v.is_a?(String) && v.start_with?('/') ? FnSub("arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter#{v}") : v }
secrets_policy['ssm-secrets-inline'] = {
'action' => 'ssm:GetParameters',
'resource' => resources
}
end
if secrets.any?
task_def.merge!({Secrets: secrets})
end
end
definitions << task_def
end
# add docker volumes
volumes = external_parameters.fetch(:volumes, [])
volumes.each do |volume|
if volume.is_a? String
parts = volume.split(':')
object = { Name: FnSub(parts[0])}
object.merge!({ Host: { SourcePath: FnSub(parts[1]) }}) if parts[1]
else
object = volume
end
task_volumes << object
end
# add task placement constraints
task_constraints =[];
task_placement_constraints = external_parameters.fetch(:task_placement_constraints, [])
task_placement_constraints.each do |cntr|
object = {Type: "memberOf"}
object.merge!({ Expression: FnSub(cntr)})
task_constraints << object
end
iam_policies = external_parameters.fetch(:iam_policies, {})
service_discovery = external_parameters.fetch(:service_discovery, {})
enable_execute_command = external_parameters.fetch(:enable_execute_command, false)
if enable_execute_command
iam_policies['ssm-session-manager'] = {
'action' => %w(
ssmmessages:CreateControlChannel
ssmmessages:CreateDataChannel
ssmmessages:OpenControlChannel
ssmmessages:OpenDataChannel
)
}
end
unless iam_policies.empty?
unless service_discovery.empty?
iam_policies['ecs-service-discovery'] = {
'action' => %w(
servicediscovery:RegisterInstance
servicediscovery:DeregisterInstance
servicediscovery:DiscoverInstances
servicediscovery:Get*
servicediscovery:List*
route53:GetHostedZone
route53:ListHostedZonesByName
route53:ChangeResourceRecordSets
route53:CreateHealthCheck
route53:GetHealthCheck
route53:DeleteHealthCheck
route53:UpdateHealthCheck
)
}
end
IAM_Role('TaskRole') do
AssumeRolePolicyDocument service_assume_role_policy(['ecs-tasks','ssm'])
Path '/'
Policies(iam_role_policies(iam_policies))
end
IAM_Role('ExecutionRole') do
AssumeRolePolicyDocument service_assume_role_policy(['ecs-tasks','ssm'])
Path '/'
ManagedPolicyArns ["arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"]
if secrets_policy.any?
Policies iam_role_policies(secrets_policy)
end
end
end
task_type = external_parameters.fetch(:task_type, 'EC2')
unless task_definition.empty?
ECS_TaskDefinition('Task') do
ContainerDefinitions definitions
RequiresCompatibilities [task_type]
if external_parameters[:cpu]
Cpu external_parameters[:cpu]
end
if external_parameters[:memory]
Memory external_parameters[:memory]
end
if external_parameters[:network_mode]
NetworkMode external_parameters[:network_mode]
end
if task_volumes.any?
Volumes task_volumes
end
unless iam_policies.empty?
TaskRoleArn Ref('TaskRole')
ExecutionRoleArn Ref('ExecutionRole')
end
EphemeralStorage external_parameters[:ephemeral_storage] unless external_parameters[:ephemeral_storage].nil?
Tags task_tags
end
Output("EcsTaskArn") {
Value(Ref('Task'))
Export FnSub("${EnvironmentName}-#{export}-EcsTaskArn")
}
end
end