AWSTemplateFormatVersion: '2010-09-09'
Description: 'stack: {{stackName}} | stage: {{stage}} | deployed by Kes'

Resources:

  # role for Lambdas that are NOT used with APIGateway
  LambdaRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action:
          - sts:AssumeRole
      Path: "/"
      Policies:
        - PolicyName: KesLambdaRole
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              # Allow Lambda logging
              - Effect: Allow
                Action:
                - logs:DescribeLogStreams
                - logs:CreateLogGroup
                - logs:CreateLogStream
                - logs:PutLogEvents
                Resource: arn:aws:logs:*:*:*

              # Allow lambdas to call other lambdas
              - Effect: Allow
                Action:
                - lambda:GetFunction
                - lambda:invokeFunction
                Resource:
                - "*"

  #################################################
  # Lambda config BEGIN
  #################################################
{{#each lambdas}}
  {{name}}LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket: {{bucket}}
        S3Key: {{remote}}
      FunctionName: {{../stackName}}-{{../stage}}-{{name}}
      Environment:
        Variables:
          stage: {{../stage}}
          stackName: {{../stackName}}
        {{#each envs}}
          {{@key}}: {{this}}
        {{/each}}
      Handler: {{handler}}
      MemorySize: {{memory}}
      Role:
        Fn::GetAtt:
          - LambdaRole
          - Arn
      Runtime: {{# if runtime}}{{runtime}}{{else}}nodejs6.10{{/if}}
      Timeout: {{# if timeout}}{{timeout}}{{else}}300{{/if}}
{{/each}}
  #################################################
  # Lambda config END
  #################################################

