AWSTemplateFormatVersion: "2010-09-09"
Description: An AWS function.
Resources:
  ###################################################################
  # Lambda Function
  myiotlm:
    Type: "AWS::Lambda::Function"
    Properties:
      Description: ""
      # file: cloudapp.py
      Handler: cloudapp.lambda_handler
      # DO NOT EDIT THIS 1 LINE
      Role: "arn:aws:iam::${AWS::AccountId}:role/sip-sample-lambda-role"
      Runtime: python3.7
      MemorySize: 128
      Timeout: 30
      FunctionName: "myiot-lm"
      Environment:
        Variables:
          # DO NOT EDIT THIS 1 LINE
          ES_ACCESS_LAMBDA: myiot-rel-es-access-lambda
          MQTT_LAMBDA: myiot-rel-publish-mqtt-lambda
          SUBSCRIPTION: 'ex/{}/alert'

  ###################################################################
  # ApiGateway
  RestAPI:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Description: "myiot RestApi"
      Name: "myiot-apigw"

  RestAPIDeployment:
    Type: AWS::ApiGateway::Deployment
    Properties:
      Description: ""
      RestApiId:
        Ref: RestAPI
    DependsOn:
      - RestAPIMethod
      - RestAPIResource

  RestAPIStage:
    Type: AWS::ApiGateway::Stage
    Properties:
      RestApiId:
        Ref: RestAPI
      DeploymentId:
        Ref: RestAPIDeployment
      StageName: "myiot-stage"

  RestAPIResource:
    Type: AWS::ApiGateway::Resource
    Properties:
      PathPart: "{proxy+}"
      ParentId:
        Fn::GetAtt: RestAPI.RootResourceId
      RestApiId:
        Ref: RestAPI

  RestAPIMethod:
    Type: AWS::ApiGateway::Method
    Properties:
      HttpMethod: ANY
      AuthorizationType: NONE
      RequestParameters:
        "method.request.path.proxy": true
      Integration:
        CacheKeyParameters:
          - "method.request.path.proxy"
        CacheNamespace:
          Ref: RestAPIResource
        IntegrationHttpMethod: POST
        Type: AWS_PROXY
        Uri:
          Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${myiotlm.Arn}/invocations"
      ResourceId:
        Ref: RestAPIResource
      RestApiId:
        Ref: RestAPI

  ###################################################################
  # LambdaPermission
  LambdaPermission:
    Type: "AWS::Lambda::Permission"
    Properties:
      Action: "lambda:InvokeFunction"
      Principal: "apigateway.amazonaws.com"
      FunctionName: !GetAtt myiotlm.Arn

#EOF