AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: For <%= functionName %> lambda
Resources:
  <%= functionName %>Function:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      CodeUri: ./dist/api.zip
      Runtime: nodejs8.10
      Role: !GetAtt <%= functionName %>LambdaFunctionRole.Arn
      Events:
        CollectionApi:
          Type: Api
          Properties:
            RestApiId: !Ref <%= functionName %>API
            Method: any
            Path: /<%= resource %>s
        ItemApi:
          Type: Api
          Properties:
            RestApiId: !Ref <%= functionName %>API
            Method: any
            Path: /<%= resource %>s/{id}
      Environment:
        Variables:
          MongoUser: "{{resolve:ssm:mongo_user:1}}"
          MongoPwd: "{{resolve:ssm:mongo_password:1}}"
          MongoCluster: "{{resolve:ssm:mongo_cluster:1}}"
          MongoDBName: "{{resolve:ssm:mongo_db_name:1}}"
  ApiKey:
    Type: 'AWS::ApiGateway::ApiKey'
    Properties:
      Name: !Sub '${AWS::StackName}-apiKey'
      Description: Api Key
      Enabled: true
      GenerateDistinctId: false
  ApiUsagePlan:
    Type: 'AWS::ApiGateway::UsagePlan'
    DependsOn:
      - <%= functionName %>APIStage
    Properties:
      ApiStages:
      - ApiId: !Ref <%= functionName %>API
        Stage: !Sub '${AWS::StackName}'
      UsagePlanName: !Sub '${AWS::StackName}-apiUsagePlan'
  ApiUsagePlanKey:
    Type: 'AWS::ApiGateway::UsagePlanKey'
    Properties:
      KeyId: !Ref ApiKey
      KeyType: API_KEY
      UsagePlanId: !Ref ApiUsagePlan
  <%= functionName %>API:
    Type: 'AWS::Serverless::Api'
    Properties:
      StageName: !Sub '${AWS::StackName}'
      DefinitionBody:
        swagger: '2.0'
        info:
          title: !Sub '${AWS::StackName}-<%= functionName %>API'
        basePath: /
        schemes:
          - https
        paths:
          /<%= resource %>s:
            get:
              response: {}
              security:
                - api_key: []
              x-amazon-apigateway-integration:
                uri: !Sub >-
                  arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${<%= functionName %>Function.Arn}/invocations
                passthroughBehavior: when_no_match
                httpMethod: POST
                type: aws_proxy
          /<%= resource %>s/{id}:
            get:
              response: {}
              security:
                - api_key: []
              x-amazon-apigateway-integration:
                uri: !Sub >-
                  arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${<%= functionName %>Function.Arn}/invocations
                passthroughBehavior: when_no_match
                httpMethod: POST
                type: aws_proxy
        securityDefinitions: 
         api_key: 
          type: apiKey
          name: x-api-key
          in: header
  <%= functionName %>LambdaFunctionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument: 
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action:
          - sts:AssumeRole
      ManagedPolicyArns:
      - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      - arn:aws:iam::aws:policy/AmazonSSMReadOnlyAccess