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: Callback, Choice, ChoiceItem, State, StateMachine, Task, Tool

Constant Summary collapse

VERSION =
"0.0.10"
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



48
49
50
# File 'lib/simplerubysteps/dsl.rb', line 48

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

#callback(name) ⇒ Object



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

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



79
80
81
82
83
84
85
86
87
# File 'lib/simplerubysteps/dsl.rb', line 79

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



102
103
104
105
106
# File 'lib/simplerubysteps/dsl.rb', line 102

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

#default_transition_to(state) ⇒ Object



69
70
71
72
73
# File 'lib/simplerubysteps/dsl.rb', line 69

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

  choice.default = state
end

#iam_permissions(permissions) ⇒ Object



75
76
77
# File 'lib/simplerubysteps/dsl.rb', line 75

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

#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

#sqs_callback(name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/simplerubysteps/dsl.rb', line 31

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



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/simplerubysteps/dsl.rb', line 89

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



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

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



52
53
54
# File 'lib/simplerubysteps/dsl.rb', line 52

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

#transition_to(state, &condition_block) ⇒ Object



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

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