Module: Simplerubysteps

Defined in:
lib/simplerubysteps/dsl.rb,
lib/simplerubysteps/tool.rb,
lib/simplerubysteps/model.rb,
lib/simplerubysteps/version.rb,
lib/simplerubysteps/cloudformation.rb

Defined Under Namespace

Classes: Branch, Callback, Choice, ChoiceItem, Parallel, State, StateMachine, Task, Tool, Wait

Constant Summary collapse

VERSION =
"0.0.11"
CLOUDFORMATION_ERB_TEMPLATE =
<<-YAML
---
AWSTemplateFormatVersion: "2010-09-09"

<% if resources[:functions] %>
Parameters:  
  LambdaS3:
    Description: LambdaS3.
    Type: String
<% end %>

<% if resources[:state_machine] %>
  StepFunctionsS3:
    Description: StepFunctionsS3.
    Type: String
  StateMachineType:
    Description: StateMachineType.
    Type: String
<% end %>

Resources:
  DeployBucket:
    Type: AWS::S3::Bucket

<% if resources[:functions] %>
<% resources[:functions].each_with_index do |resource, index| %>
  LambdaFunction<%= index %>:
    Type: "AWS::Lambda::Function"
    Properties:
      Code:
        S3Bucket: !Ref DeployBucket
        S3Key: !Ref LambdaS3
      Handler: function.handler      
      Role: !GetAtt MyLambdaRole<%= index %>.Arn
      Runtime: ruby2.7
      Environment:
        Variables:
<% if resource["queue"] %>
          QUEUE: !Ref MyQueue<%= index %>        
<% end %>          
<% resource["env"].each do |k, v| %>        
          <%= k %>: <%= v.inspect %>
<% end %>
  LogGroup<%= index %>:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub "/aws/lambda/${LambdaFunction<%= index %>}"
      RetentionInDays: 7  
  MyLambdaRole<%= index %>:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      Policies:
        - PolicyName: lambda-policy
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: arn:aws:logs:*:*:*
<% if resource["iam_permissions"] %>
  MyCustomPolicy<%= index %>:
    Type: AWS::IAM::Policy
    Properties:
      PolicyName: custom
      Roles:
        - !Ref MyLambdaRole<%= index %>
      PolicyDocument: <%= resource["iam_permissions"].inspect %>
<% end %>
<% if resource["queue"] %>
  MyQueue<%= index %>:
    Type: AWS::SQS::Queue
  MyQueuePolicy<%= index %>:
    Type: AWS::IAM::Policy
    Properties:
      PolicyName: queue
      Roles:
        - !Ref MyLambdaRole<%= index %>
      PolicyDocument: 
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action:
              - 'sqs:SendMessage'
            Resource: !GetAtt MyQueue<%= index %>.Arn
<% end %>
<% end %>
<% end %>
  
<% if resources[:state_machine] %>
  StepFunctionsStateMachine:
    Type: "AWS::StepFunctions::StateMachine"
    Properties:
      DefinitionS3Location:
        Bucket: !Ref DeployBucket
        Key: !Ref StepFunctionsS3
      RoleArn: !GetAtt StepFunctionsRole.Arn
      StateMachineType: !Ref StateMachineType
  StepFunctionsRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: states.amazonaws.com
            Action: sts:AssumeRole
      Policies:
        - PolicyName: StepFunctionsPolicy
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: arn:aws:logs:*:*:*
              - Effect: Allow
                Action:
                  - lambda:InvokeFunction
                Resource: 
<% resources[:functions].each_with_index do |resource, index| %>                
                  - !GetAtt LambdaFunction<%= index %>.Arn
<% end %>                  
<% end %>

Outputs:
  DeployBucket:
    Value: !Ref DeployBucket

<% if resources[:functions] %>    
  LambdaCount:
    Value: <%= resources[:functions].length %>
<% resources[:functions].each_with_index do |resource, index| %>    
  LambdaRoleARN<%= index %>:
    Value: !GetAtt MyLambdaRole<%= index %>.Arn
  LambdaFunctionARN<%= index %>:
    Value: !GetAtt LambdaFunction<%= index %>.Arn
  LambdaFunctionName<%= index %>:
    Value: !Ref LambdaFunction<%= index %>
<% end %>    
<% end %>  

<% if resources[:state_machine] %>
  StepFunctionsStateMachineARN:
    Value: !GetAtt StepFunctionsStateMachine.Arn
  StateMachineType:
    Value: !Ref StateMachineType
  StepFunctionsRoleARN:
    Value: !GetAtt StepFunctionsRole.Arn
<% end %>    

<% if resources[:functions] %>
<% resources[:functions].each_with_index do |resource, index| %>
<% if resource["queue"] %>
  <%= resource["env"]["task"] %>Queue:
    Value: !Ref MyQueue<%= index %>
<% end %>  
<% end %>    
<% end %>    

YAML

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cloudformation_yaml(resources) ⇒ Object



179
180
181
182
# File 'lib/simplerubysteps/cloudformation.rb', line 179

def self.cloudformation_yaml(resources)
  template = ERB.new(CLOUDFORMATION_ERB_TEMPLATE)
  template.result(binding)
end

Instance Method Details

#action(&action_block) ⇒ Object



85
86
87
# File 'lib/simplerubysteps/dsl.rb', line 85

def action(&action_block)
  $tasks.last.action &action_block
end

#branchObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simplerubysteps/dsl.rb', line 21

def branch
  sm_backup = $sm
  tasks_backup = $tasks

  $sm = $tasks.last.new_branch
  $tasks = []

  yield if block_given?

  $sm = sm_backup
  $tasks = tasks_backup
end

#callback(name) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/simplerubysteps/dsl.rb', line 58

def callback(name)
  t = $sm.add Callback.new(name)

  $tasks.last.next = t if $tasks.last

  $tasks.push t
  yield if block_given?
  $tasks.pop
end

#choice(name) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/simplerubysteps/dsl.rb', line 116

def choice(name)
  t = $sm.add Choice.new(name)

  $tasks.last.next = t if $tasks.last

  $tasks.push t
  yield if block_given?
  $tasks.pop
end

#defaultObject



139
140
141
142
143
# File 'lib/simplerubysteps/dsl.rb', line 139

def default
  $tasks.push $tasks.last
  yield if block_given?
  $tasks.pop
end

#default_transition_to(state) ⇒ Object



106
107
108
109
110
# File 'lib/simplerubysteps/dsl.rb', line 106

def default_transition_to(state)
  choice = $tasks.last.implicit_choice

  choice.default = state
end

#iam_permissions(permissions) ⇒ Object



112
113
114
# File 'lib/simplerubysteps/dsl.rb', line 112

def iam_permissions(permissions)
  $tasks.last.iam_permissions = permissions
end

#kind(k) ⇒ Object



7
8
9
# File 'lib/simplerubysteps/dsl.rb', line 7

def kind(k)
  $sm.kind = k
end

#parallel(name) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/simplerubysteps/dsl.rb', line 11

def parallel(name)
  t = $sm.add Parallel.new(name)

  $tasks.last.next = t if $tasks.last

  $tasks.push t
  yield if block_given?
  $tasks.pop
end

#pop_function_arnObject



4
5
6
7
8
9
# File 'lib/simplerubysteps/model.rb', line 4

def pop_function_arn
  return "unknown" unless $LAMBDA_FUNCTION_ARNS
  arn = $LAMBDA_FUNCTION_ARNS.first
  $LAMBDA_FUNCTION_ARNS.delete arn
  arn
end

#pop_function_nameObject



11
12
13
14
# File 'lib/simplerubysteps/model.rb', line 11

def pop_function_name
  return "unknown" unless pop_function_arn =~ /.+\:function\:(.+)/
  $1
end

#seconds(s) ⇒ Object



44
45
46
# File 'lib/simplerubysteps/dsl.rb', line 44

def seconds(s)
  $tasks.last.seconds = s
end

#sqs_callback(name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/simplerubysteps/dsl.rb', line 68

def sqs_callback(name)
  t = $sm.add Callback.new(name)
  t.queue = true

  $tasks.last.next = t if $tasks.last

  $tasks.push t
  action do |input, token, queue_client|
    queue_client.send({
      input: input,
      token: token,
    })
  end
  yield if block_given?
  $tasks.pop
end

#string_matches(var, match) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/simplerubysteps/dsl.rb', line 126

def string_matches(var, match)
  c = ChoiceItem.new({
    :Variable => var,
    :StringMatches => match,
  })

  $tasks.last.add c

  $tasks.push c
  yield if block_given?
  $tasks.pop
end

#task(name) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/simplerubysteps/dsl.rb', line 48

def task(name)
  t = $sm.add Task.new(name)

  $tasks.last.next = t if $tasks.last

  $tasks.push t
  yield if block_given?
  $tasks.pop
end

#transition(state) ⇒ Object



89
90
91
# File 'lib/simplerubysteps/dsl.rb', line 89

def transition(state)
  $tasks.last.next = state
end

#transition_to(state, &condition_block) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/simplerubysteps/dsl.rb', line 93

def transition_to(state, &condition_block)
  choice = $tasks.last.implicit_choice

  c = ChoiceItem.new({
    :Variable => "$.#{choice.name}_#{state}",
    :StringMatches => "yes",
  })
  c.next = state
  c.implicit_condition_block = condition_block

  choice.add c
end

#wait(name) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/simplerubysteps/dsl.rb', line 34

def wait(name)
  t = $sm.add Wait.new(name)

  $tasks.last.next = t if $tasks.last

  $tasks.push t
  yield if block_given?
  $tasks.pop
end