-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathecs-schedule-runtasks.cfndsl.rb
75 lines (69 loc) · 2.4 KB
/
ecs-schedule-runtasks.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
CloudFormation do
iam_policies = external_parameters.fetch(:scheduler_iam_policies, {})
IAM_Role(:EventBridgeInvokeRole) do
AssumeRolePolicyDocument ({
Statement: [
{
Effect: 'Allow',
Principal: { Service: [ 'events.amazonaws.com' ] },
Action: [ 'sts:AssumeRole' ]
}
]
})
Path '/'
Policies iam_role_policies(iam_policies)
end
run_tasks.each do |name, task|
schedule = task['schedule']
task_name = name.gsub("-","").gsub("_","")
container_overrides = {}
container_overrides[:name] = task.has_key?('container') ? task['container'] : "#{task['task_definition']}"
container_overrides[:command] = task['command'] if task.has_key?('command')
env_vars = []
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
container_overrides.merge!({environment: env_vars }) if env_vars.any?
container_input = {
containerOverrides: [container_overrides]
}
unless schedule.nil?
Events_Rule("#{task_name}Schedule") do
Name FnSub("${EnvironmentName}-#{name}-schedule")
Description FnSub("${EnvironmentName} #{name} schedule")
ScheduleExpression schedule
State Ref(:State)
Targets [{
Id: name,
Arn: Ref(:EcsClusterArn),
RoleArn: FnGetAtt('EventBridgeInvokeRole', 'Arn'),
EcsParameters: {
TaskDefinitionArn: Ref("#{task['task_definition']}"),
EnableExecuteCommand: true,
TaskCount: 1,
LaunchType: 'FARGATE',
NetworkConfiguration: {
AwsVpcConfiguration: {
Subnets: FnSplit(',', Ref('SubnetIds')),
SecurityGroups: [Ref("#{task['task_definition']}SecurityGroup")],
AssignPublicIp: "DISABLED"
}
}
},
Input: FnSub(container_input.to_json())
}]
end
end
end
end