Class: Jets::Cfn::TemplateBuilders::JobBuilder

Inherits:
BaseChildBuilder show all
Defined in:
lib/jets/cfn/template_builders/job_builder.rb

Instance Method Summary collapse

Methods inherited from BaseChildBuilder

#add_class_iam_policy, #add_common_parameters, #add_function, #add_functions, #add_iam_policy, #initialize, #template_path

Methods included from Interface

#add_output, #add_parameter, #add_resource, #build, #post_process_template, #template, #text, #write

Constructor Details

This class inherits a constructor from Jets::Cfn::TemplateBuilders::BaseChildBuilder

Instance Method Details

#add_event_rule(task, map) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jets/cfn/template_builders/job_builder.rb', line 22

def add_event_rule(task, map)
  add_resource(map.logical_id, "AWS::Events::Rule",
    ScheduleExpression: task.schedule_expression,
    State: task.state,
    Targets: [
      {
        Arn: "!GetAtt #{map.lambda_function_logical_id}.Arn",
        Id: map.rule_target_id
      }
    ]
  )
  # Example:
  # add_resource("HardJobDigScheduledEvent", "AWS::Events::Rule",
  #   ScheduleExpression: "rate(1 minute)",
  #   State: "ENABLED",
  #   Targets: [
  #     {
  #       Arn: "!GetAtt HardJobDigLambdaFunction.Arn",
  #       Id: "RuleTargetHardJobDig"
  #     }
  #   ]
  # )
end

#add_permission(map) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jets/cfn/template_builders/job_builder.rb', line 46

def add_permission(map)
  add_resource(map.permission_logical_id, "AWS::Lambda::Permission",
    FunctionName: "!GetAtt #{map.lambda_function_logical_id}.Arn",
    Action: "lambda:InvokeFunction",
    Principal: "events.amazonaws.com",
    SourceArn: "!GetAtt #{map.logical_id}.Arn"
  )
  # Example:
  # add_resource("HardJobDigEventsRulePermission", "AWS::Lambda::Permission",
  #   FunctionName: "!GetAtt HardJobDigLambdaFunction.Arn",
  #   Action: "lambda:InvokeFunction",
  #   Principal: "events.amazonaws.com",
  #   SourceArn: "!GetAtt HardJobDigScheduledEvent.Arn"
  # )
end

#add_scheduled_tasksObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/jets/cfn/template_builders/job_builder.rb', line 9

def add_scheduled_tasks
  # @app_klass is PostsController, HardJob, Hello, or HelloFunction
  @app_klass.tasks.each do |task|
    map = Jets::Cfn::TemplateMappers::EventsRuleMapper.new(task)

    # If there's no scheduled expression dont add a scheduled Events::Rule
    if task.schedule_expression
      add_event_rule(task, map)
      add_permission(map)
    end
  end
end

#composeObject



3
4
5
6
7
# File 'lib/jets/cfn/template_builders/job_builder.rb', line 3

def compose
  add_common_parameters
  add_functions
  add_scheduled_tasks
end